OCP Oracle Certified Professional Java SE 11 Developer Practice Tests. Jeanne Boyarsky. Читать онлайн. Newlib. NEWLIB.NET

Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Программы
Год издания: 0
isbn: 9781119696148
Скачать книгу
mystery = mystery.insert(1, "more"); int num = mystery.length(); }StringStringBuilderBothNeither

      12 What is the output of the following?var teams = new StringBuilder("333"); teams.append(" 806"); teams.append(" 1601"); System.out.print(teams);333333 806 1601The code compiles but outputs something else.The code does not compile.

      13 Which of the following declarations does not compile?double num1, int num2 = 0;int num1, num2;int num1, num2 = 0;int num1 = 0, num2 = 0;All of the aboveNone of the above

      14 Given the file Magnet.java shown, which of the marked lines can you independently insert the line var color; into and still have the code compile?// line a1 public class Magnet { // line a2 public void attach() { // line a3 } // line a4 }a2a3a2 and a3a1, a2, a3, and a4None of the above

      15 Which is one of the lines output by this code?10: var list = new ArrayList<Integer>(); 11: list.add(10); 12: list.add(9); 13: list.add(8); 14: 15: var num = 9; 16: list.removeIf(x -> {int keep = num; return x != keep;}); 17: System.out.println(list); 18: 19: list.removeIf(x -> {int keep = num; return x == keep;}); 20: System.out.println(list);[][8, 10][8, 9, 10][10, 8]The code does not compile.

      16 Which of the following can fill in the blank so the code prints true?var happy = " :) - (: "; var really = happy.trim(); var question = _____________________; System.out.println(really.equals(question));happy.substring(0, happy.length() ‐ 1)happy.substring(0, happy.length())happy.substring(1, happy.length() ‐ 1)happy.substring(1, happy.length())

      17 How many of the following lines contain a compiler error?double num1 = 2.718; double num2 = 2._718; double num3 = 2.7_1_8; double num4 = _2.718;01234

      18 What is the output of the following application?public class Airplane { static int start = 2; final int end; public Airplane(int x) { x = 4; end = x; } public void fly(int distance) { System.out.print(end-start+" "); System.out.print(distance); } public static void main(String… start) { new Airplane(10).fly(5); } }2 58 56 5The code does not compile.None of the above.

      19 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 = "54321"; 5: builder = builder.substring(4); 6: System.out.println(builder.charAt(2)); 7: } 8: }234None of the above

      20 What is the output of the following application?package transporter; public class Rematerialize { public static void main(String[] input) { int init = 11; int split = 3; int partA = init / split; int partB = init % split; int result = split * (partB + partA); System.out.print(result); } }9111215The code does not compile.None of the above.

      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

       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