Читать книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky - Страница 23

Chapter 2 Controlling Program Flow

Оглавление

THE OCP EXAM TOPICS COVERED IN THIS PRACTICE TEST INCLUDE THE FOLLOWING:

 Controlling Program FlowCreate and use loops, if/else, and switch statements

1 Variables declared as which of the following are never permitted in a switch statement? (Choose two.)vardoubleintStringcharObject

2 What happens when running the following code snippet?3: var gas = true; 4: do ( 5: System.out.println("helium"); 6: gas = gas ^ gas; 7: gas = !gas; 8: ) while (!gas);It completes successfully without output.It outputs helium once.It outputs helium repeatedly.Line 6 does not compile.None of the above.

3 What is output by the following?10: int m = 0, n = 0; 11: while (m < 5) { 12: n++; 13: if (m == 3) 14: continue; 15: 16: switch (m) { 17: case 0: 18: case 1: 19: n++; 20: default: 21: n++; 22: } 23: m++; 24: } 25: System.out.println(m + " " + n);3 103 125 105 12The code does not compile.None of the above.

4 Given the following, which can fill in the blank and allow the code to compile? (Choose three.)var quest = ; for(var zelda : quest) { System.out.print(zelda); }3new int[] {3}new StringBuilder("3")List.of(3)new String[3]"Link"

5 Which of the following rules about a default branch in a switch statement are correct? (Choose two.)A switch statement is required to declare a default statement.A default statement must be placed after all case statements.A default statement can be placed between any case statements.Unlike a case statement, a default statement does not take a parameter value.A switch statement can contain more than one default statement.A default statement can be used only when at least one case statement is present.

6 What does the following method output?void dance() { var singer = 0; while (singer) System.out.print(singer++); }The code does not compile.The method completes with no output.The method prints 0 and then terminates.The method enters an infinite loop.None of the above.

7 Which are true statements comparing for‐each and traditional for loops? (Choose two.)Both can iterate through an array starting with the first element.Only the for‐each loop can iterate through an array starting with the first element.Only the traditional for loop can iterate through an array starting with the first element.Both can iterate through an array starting from the end.Only the for‐each loop can iterate through an array starting from the end.Only the traditional for loop can iterate through an array starting from the end.

8 What is the output of the following application?package planning; public class ThePlan { public static void main(String[] input) { var plan = 1; plan = plan++ + --plan; if(plan==1) { System.out.print("Plan A"); } else { if(plan==2) System.out.print("Plan B"); } else System.out.print("Plan C"); } } }Plan APlan BPlan CThe class does not compile.None of the above.

9 What is true about the following code? (Choose two.)23: var race = ""; 24: loop: 25: do { 26: race += "x"; 27: break loop; 28: } while (true); 29: System.out.println(race);It outputs x.It does not compile.It is an infinite loop.With lines 25 and 28 removed, it outputs x.With lines 25 and 28 removed, it does not compile.With lines 25 and 28 removed, it is an infinite loop.

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.

OCP Oracle Certified Professional Java SE 11 Developer Practice Tests

Подняться наверх