OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky
Чтение книги онлайн.
Читать онлайн книгу OCP Oracle Certified Professional Java SE 11 Developer Practice Tests - Jeanne Boyarsky страница 11
21 What is the result of the following code?var sb = new StringBuilder("radical") .insert(sb.length(), "robots"); System.out.println(sb);radicarobotsradicalrobotsThe code does not compile.The code compiles but throws an exception at runtime.
22 Given the following code snippet, what is the value of dinner after it is executed?int time = 9; int day = 3; var dinner = ++time>= 10 ? day-- <= 2 ? "Takeout" : "Salad" : "Leftovers";TakeoutLeftoversSaladThe code does not compile but would compile if parentheses were added.None of the above.
23 What is the output of the following?var teams = new String("694"); teams.concat(" 1155"); teams.concat(" 2265"); teams.concat(" 2869"); System.out.println(teams);694694 1155 2265 2869The code compiles but outputs something else.The code does not compile.
24 How many of the following lines compile?bool b = null; Bool bl = null; int i = null; Integer in = null; String s = null;NoneOneTwoThreeFourFive
25 What is the output of the following code snippet?int height = 2, length = 3; boolean w = height> 1 | --length < 4; var x = height!=2 ? length++ : height; boolean z = height % length == 0; System.out.println(w + "-" + x + "-" + z);true‐2‐truefalse‐2‐falsetrue‐2‐falsetrue‐3‐falsetrue‐3‐truefalse‐3‐false
26 What is the output of the following?1: public class Legos { 2: public static void main(String[] args) { 3: var sb = new StringBuilder(); 4: sb.append("red"); 5: sb.deleteCharAt(0); 6: sb.delete(1, 2); 7: System.out.println(sb); 8: } 9: }ededNone of the above
27 Which is a true statement?If s.contains("abc") is true, then s.equals("abc") is also true.If s.contains("abc") is true, then s.startsWith("abc") is also true.If s.startsWith("abc") is true, then s.equals("abc") is also true.If s.startsWith("abc") is true, then s.contains("abc") is also true.
28 What is the output of the following code snippet?boolean carrot = true; Boolean potato = false; var broccoli = true; carrot = carrot & potato; broccoli = broccoli ? !carrot : potato; potato = !broccoli ^ carrot; System.out.println(carrot + "," + potato + "," + broccoli);true,false,truetrue,true,truefalse,false,falsefalse,true,truefalse,false,trueThe code does not compile.
29 What does this code output?var babies = Arrays.asList("chick", "cygnet", "duckling"); babies.replaceAll(x -> { var newValue = "baby"; return newValue; }); System.out.println(babies);[baby][baby, baby, baby][chick, cygnet, duckling]None of the above.The code does not compile.
30 What is the output of the following class?1: package rocket; 2: public class Countdown { 3: public static void main(String[] args) { 4: var builder = new StringBuilder("54321"); 5: builder.substring(2); 6: System.out.println(builder.charAt(1)); 7: } 8: }1234Does not compile
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