15 How many lines of the magic() method contain compilation errors?10: public void magic() { 11: do { 12: int trick = 0; 13: LOOP: do { 14: trick++; 15: } while (trick < 2--); 16: continue LOOP; 17: } while (1> 2); 18: System.out.println(trick); 19: }ZeroOneTwoThreeFour
16 How many of these statements can be inserted after the println to have the code flow follow the arrow in this diagram?break; break letters; break numbers; continue; continue letters; continue numbers;OneTwoThreeFourFiveNone of above
17 What is the output of the following application?package dessert; public class IceCream { public final static void main(String… args) { var flavors = 30; int eaten = 0; switch(flavors) { case 30: eaten++; case 40: eaten+=2; default: eaten--; } System.out.print(eaten); } }123The code does not compile because var cannot be used in a switch statement.The code does not compile for another reason.None of the above.
18 Which of the following statements compile and create infinite loops at runtime? (Choose two.)while (!false) {}do {}for( : ) {}do {} while (true);while {}for( ; ; ) {}
19 Which of the following iterates a different number of times than the others?for (int k=0; k < 5; k++) {}for (int k=1; k <= 5; k++) {}int k=0; do { } while(k++ < 5);int k=0; while (k++ < 5) {}All of these iterate the same number of times.
20 What is the output of the following code snippet?int count = 0; var stops = new String[] { "Washington", "Monroe", "Jackson", "LaSalle" }; while (count < stops.length) if (stops[++count].length() < 8) break; else continue; System.out.println(count);0123The code does not compile.None of the above.
21 What is the output of the following code snippet?int hops = 0; int jumps = 0; jumps = hops++; if(jumps) System.out.print("Jump!"); else System.out.print("Hop!");Jump!Hop!The code does not compile.The code compiles but throws an exception at runtime.None of the above.
22 Which of the following best describes the flow of execution in this for loop if beta always returns false?for (alpha; beta; gamma) { delta; }alphaalpha, betaalpha, beta, gammaalpha, gammaalpha, gamma, betaNone of the above
23 What is the output of the following code snippet?boolean balloonInflated = false; do { if (!balloonInflated) { balloonInflated = true; System.out.print("inflate-"); } } while (! balloonInflated); System.out.println("done");doneinflate‐doneThe code does not compile.This is an infinite loop.None of the above.
24 Which of these code snippets behaves differently from the others?if (numChipmunks == 1) System.out.println("One chipmunk"); if (numChipmunks == 2) System.out.println("Two chipmunks"); if (numChipmunks == 3) System.out.println("Three chipmunks");switch (numChipmunks) { case 1: System.out.println("One chipmunk"); case 2: System.out.println("Two chipmunks"); case 3: System.out.println("Three chipmunks"); }if (numChipmunks == 1) System.out.println("One chipmunk"); else if (numChipmunks == 2) System.out.println("Two chipmunks"); else if (numChipmunks == 3) System.out.println("Three chipmunks");All three code snippets do the same thing.
25 Which statements about loops are correct? (Choose three.)A do/while loop requires a body.A while loop cannot be exited early with a return statement.A while loop requires a conditional expression.A do/while loop executes the body (if present) at least once.A do/while loop cannot be exited early with a return statement.A while loop executes the body (if present) at least once.
26 Given the following enum and class, which option fills in the blank and allows the code to compile?enum Season { SPRING, SUMMER, WINTER } public class Weather { public int getAverageTemperate(Season s) { switch (s) { default: ______________ return 30; } } }case Season.WINTER:case WINTER, SPRING:case SUMMER | WINTER:case SUMMER ‐>case FALL:None of the above
27 Fill in the blank with the line of code that causes the application to compile and print exactly one line at runtime.package nyc; public class TourBus { public static void main(String… args) { var nycTour = new String[] { "Downtown", "Uptown", "Brooklyn" }; var times = new String[] { "Day", "Night" }; for (_______________ i<nycTour.length && j<times.length; i++, j++) System.out.println(nycTour[i] + "-" + times[j]); } }int i=1; j=1;int i=0, j=1;int i=1; int j=0;int i=1, int j=0;int i=1, j=0;None of the above
28 The code contains six pairs of curly braces. How many pairs can be removed without changing the behavior?12: public static void main(String[] args) { 13: int secret = 0; 14: for (int i = 0; i < 10; i++) { 15: while (i < 10) { 16: if (i == 5) { 17: System.out.println("if"); 18: } else { 19: System.out.println("in"); 20: System.out.println("else"); 21: } 22: } 23: } 24: switch (secret) { 25: case 0: System.out.println("zero"); 26: } 27: }OneTwoThreeFourFiveSix
29 Which of the following can replace the body of the travel() method to produce the same output on any nonempty input?public void travel(List<Integer> roads) { for (int w = 1; w <= roads.size(); w++) System.out.print(roads.get(w-1)); }for (int r = 0; r < roads.size(); r += 1) System.out.print(roads.get(0));for(var z : roads) System.out.print(z);for (int t = roads.size(); t> 0; t--) System.out.print(roads.get(t));for (var var : roads) System.out.print(roads);for (int q = roads.size(); q>= 0; q++) System.out.print(roads.get(q));None of the above
30 Which statement about the following code snippet is correct?3: final var javaVersions = List.of(9,10,11); 4: var exams = List.of("1Z0-811", "1Z0-819"); 5: V: for (var e1 : javaVersions) { 6: E: for (String e2 : exams) 7: System.out.println(e1 + "_" + e2); 8: break; 9: }One line does not compile.Two lines do not compile.Three lines do not compile.It compiles and prints two lines at runtime.It compiles and prints three lines at runtime.None of the above.
Chapter 3 Java Object‐Oriented Approach
THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:
Java Object‐Oriented ApproachDeclare and instantiate Java objects including nested class objects, and explain objects' lifecycles (including creation, dereferencing by reassignment, and garbage collection)Define and use fields and methods, including instance, static and overloaded methodsInitialize objects and their members using instance and static initialiser statements and constructorsUnderstand variable scopes, apply encapsulation and make objects immutableCreate and use subclasses and superclasses, including abstract classesUtilize polymorphism and casting to call methods, differentiate object type versus reference typeCreate and use interfaces, identify functional interfaces, and utilize private, static, and default methodsCreate and use enumerations
1 What is the output of the following application?package dnd; final class Story { void recite(int chapter) throws Exception {} } public class Adventure extends Story { final void recite(final int chapter) { // g1 switch(chapter) { // g2 case 2: System.out.print(9); default: System.out.print(3); } } public static void main(String… u) { var bedtime = new Adventure(); bedtime.recite(2); } }3993The code does not compile because of line g1.The code does not compile because of line g2.None of the above.
2 Which of the following lines of code are not permitted as the first line of a Java class file? (Choose two.)import widget.*;// Widget Managerint facilityNumber;package sprockets;/** Author: Cid **/void produce() {}
3 Which of the following modifiers can be applied to an abstract method? (Choose two.)finalprivatepublicdefaultprotectedconcrete
4 What is the result of compiling and executing the following class?1: СКАЧАТЬ