OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
eggs; 6: } 7: public static void main(String[] r) { 8: var c1 = new Chicken(1); 9: var c2 = new Chicken(2); 10: var c3 = new Chicken(3); 11: c1.eggs = c2.eggs; 12: c2 = c1; 13: c3.eggs = null; 14: } }Option A.Option B.Option C.Option D.The code does not compile.None of the above.
23 What is the output of the following application?package musical; interface Speak { default int talk() { return 7; } } interface Sing { default int talk() { return 5; } } public class Performance implements Speak, Sing { public int talk(String… x) { return x.length; } public static void main(String[] notes) { System.out.print(new Performance().talk()); } }75The code does not compile.The code compiles without issue, but the output cannot be determined until runtime.None of the above.
24 What is the output of the following application?package ai; interface Pump { void pump(double psi); } interface Bend extends Pump { void bend(double tensileStrength); } public class Robot { public static final void apply( Bend instruction, double input) { instruction.bend(input); } public static void main(String… future) { final Robot r = new Robot(); r.apply(x -> System.out.print(x+" bent!"), 5); } }5 bent!5.0 bent!The code does not compile because Bend is not a functional interface.The code does not compile because of the apply() method declaration.None of the above.
25 Which statement is true about encapsulation while providing the broadest access allowed?Variables are private, and methods are private.Variables are private, and methods are public.Variables are public, and methods are private.Variables are public, and methods are public.None of the above.
26 Fill in the blanks: The ___________________ access modifier allows access to everything the ___________________ access modifier does and more.package‐private, privateprivate, protectedprotected, publicprivate, package‐privateNone of the above
27 Which statement about the following interface is correct?public interface Swimming { String DEFAULT = "Diving!"; // k1 abstract int breath(); private static void stroke() { if(breath()==1) { // k2 System.out.print("Go!"); } else { System.out.print(dive()); // k3 } } static String dive() { return DEFAULT; // k4 } }The code compiles without issue.The code does not compile because of line k1.The code does not compile because of line k2.The code does not compile because of line k3.The code does not compile because of line k4.None of the above.
28 Which is the first line to fail to compile?class Tool { private void repair() {} // r1 void use() {} // r2 } class Hammer extends Tool { private int repair() { return 0; } // r3 private void use() {} // r4 public void bang() {} // r5 }r1r2r3r4r5None of the above
29 Which modifier can be applied to an abstract interface method?finalinterfaceprotectedvoidNone of the above
30 What is the output of the Plant program?class Bush extends Plant { String type = "bush"; } public class Plant { String type = "plant"; public static void main(String[] args) { Plant w1 = new Bush(); Bush w2 = new Bush(); Plant w3 = w2; System.out.print(w1.type+","+w2.type+","+w3.type); } }plant,bush,plantplant,bush,bushbush,plant,bushbush,bush,bushThe code does not compile.None of the above.
31 Which statements can accurately fill in the blanks in this table? (Choose two.)Variable TypeCan Be Called Within the Class from What Type of Method?InstanceBlank 1: _____________staticBlank 2: _____________Blank 1: an instance method onlyBlank 1: a static method onlyBlank 1: an instance or static methodBlank 2: an instance method onlyBlank 2: a static method onlyBlank 2: an instance or static method
32 What is the correct order of statements for a Java class file?import statements, package statement, class declarationpackage statement, class declaration, import statementsclass declaration, import statements, package statementpackage statement, import statements, class declarationimport statements, class declaration, package statementclass declaration, package statement, import statements
33 What is true of the following code? (Choose three.)1: class Penguin { 2: enum Baby { EGG } 3: static class Chick { 4: enum Baby { EGG } 5: } 6: public static void main(String[] args) { 7: boolean match = false; 8: Baby egg = Baby.EGG; 9: switch (egg) { 10: case EGG: 11: match = true; 12: } 13: } 14: }It compiles as is.It does not compile as is.Removing line 2 would create an additional compiler error.Removing line 2 would not create an additional compiler error.Removing the static modifier on line 3 would create an additional compiler error.Removing the static modifier on line 3 would not create an additional compiler error.
34 Which are true of the following? (Choose two.)package beach; public class Sand { private static int numShovels; private int numRakes; public static int getNumShovels() { return numShovels; } public static int getNumRakes() { return numRakes; } public Sand() { System.out.print("a"); } public void Sand() { System.out.print("b"); } public void run() { new Sand(); Sand(); } public static void main(String… args) { new Sand().run(); } }The code compiles.One line doesn't compile.Two lines don't compile.If any constructors and/or methods that do not compile are removed, the remaining code prints a.If the code compiles or if any constructors/methods that do not compile are removed, the remaining code prints ab.If the code compiles or if any constructors/methods that do not compile are removed, the remaining code prints aab.
35 Which of the following class types cannot be marked final or abstract?static nested class.Local class.Anonymous class.Member inner class.All of the above can be marked final or abstract.
36 Fill in the blanks: The ___________________ access modifier allows access to everything the ___________________ access modifier does and more. (Choose three.)package‐private, protectedpackage‐private, publicprotected, package‐privateprotected, publicpublic, package‐privatepublic, protected
37 Which is the first line containing a compiler error?var title = "Weather"; // line x1 var hot = 100, var cold = 20; // line x2 var f = 32, int c = 0; // line x3x1x2x3None of the above
38 How many of the following members of Telephone interface are public?public interface Telephone { static int call() { return 1; } default void dial() {} long answer(); String home = "555-555-5555"; }Zero.One.Two.Three.Four.The code does not compile.
39 Which best describes what the new keyword does?Creates a copy of an existing object and treats it as a new one.Creates a new primitive.Instantiates a new object.Switches an object reference to a new one.The behavior depends on the class implementation.
40 How many lines will not compile?12: public void printVarargs(String… names) { 13: System.out.println(Arrays.toString(names)); 14: } 15: public void printArray(String[] names) { 16: System.out.println(Arrays.toString(names)); 17: } 18: public void stormy() { 19: printVarargs("Arlene"); 20: printVarargs(new String[]{"Bret"}); 21: printVarargs(null); 22: printArray ("Cindy"); 23: printArray (new String[]{"Don"}); 24: printArray (null); 25: }ZeroOneTwoThreeFourFive
41 Which of the following can include a static method in its definition? (Choose three.)InterfaceAnonymous classAbstract classMember inner classLocal classstatic nested class
42 What is the minimum number of lines that need to be removed to make this code compile?@FunctionalInterface public interface Play { public static void baseball() {} private static void soccer() {} default void play() {} void fun(); }1.2.3.4.The code compiles as is.
43 Fill in the blanks: A class that defines an instance variable with the same name as a variable in the parent class is referred to as ___________________ a variable, while a class that defines a static method with the same signature as a static method in a parent class is referred to as ___________________ a method.hiding, overridingoverriding, hidingmasking, maskinghiding, maskingreplacing, overridinghiding, hiding
44 What change is needed to make Secret well encapsulated?import java.util.*; public class Secret { private int number = new Random().nextInt(10); public boolean guess(int candidate) { return number == candidate; } }Change number to use a protected access modifier.Change number to use a public access modifier.Declare a private constructor.Declare a public constructor.Remove the guess method.None. It is already well encapsulated.
45 Which of the following are the best reasons for creating a public static interface method? (Choose two.)Allow static methods to access instance methods.Allow an interface to define a method at the class level.Provide an implementation that a class implementing the interface can override.Improve code reuse within the interface.Add backward compatibility to existing interfaces.Improve encapsulation of the interface.
46 What