10 Which of the following can replace the body of the perform() method to produce the same output on any nonempty input? (Choose two.)public void perform(String[] circus) { for (int i=circus.length-1; i>=0; i--) System.out.print(circus[i]); }for (int i=circus.length; i>0; i--) System.out.print(circus[i-1]);for-reversed (String c = circus) System.out.print(c);for (var c : circus) System.out.print(c);for(var i=0; i<circus.length; i++) System.out.print(circus[circus.length-i-1]);for (int i=circus.length; i>0; i--) System.out.print(circus[i+1]);for-each (String c circus) System.out.print(c);
11 What does the following code snippet output?var bottles = List.of("glass", "plastic", "can"); for (int type = 1; type < bottles.size();) { System.out.print(bottles.get(type) + "-"); if(type < bottles.size()) break; } System.out.print("end");glass‐endglass‐plastic‐can‐endplastic‐endplastic‐can‐endThe code does not compile.None of the above.
12 What is the result of executing the following code snippet?final var GOOD = 100; var score = 10; switch (score) { default: 1 : System.out.print("1-"); -1 : System.out.print("2-"); break; 4,5 : System.out.print("3-"); 6 : System.out.print("4-"); 9 : System.out.print("5-"); }1‐1‐2‐2‐3‐4‐None of the above
13 What is the output of the following application?package dinosaur; public class Park { public final static void main(String… arguments) { int pterodactyl = 8; long triceratops = 3; if(pterodactyl % 3> 1 + 1) triceratops++; triceratops--; System.out.print(triceratops); } }234The code does not compile.The code compiles but throws an exception at runtime.
14 What variable type of red allows the following application to compile?package tornado; public class Kansas { public static void main(String[] args) { int colorOfRainbow = 10; ___________ red = 5; switch(colorOfRainbow) { default: System.out.print("Home"); break; case red: System.out.print("Away"); } } }longdoubleintvarStringNone of the above
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,