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

Chapter 1 Working with Java Data Types

Оглавление

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

 Working with Java Data TypesUse primitives and wrapper classes, including, operators, parentheses, type promotion and castingHandle text using String and StringBuilder classesUse local variable type inference, including as lambda parameters

1 Which of the following are not valid variable names? (Choose two.)__blue2blueblue$Blue

2 What is the value of tip after executing the following code snippet?int meal = 5; int tip = 2; var total = meal + (meal>6 ? tip++ : tip--);1237None of the above

3 Which is equivalent to var q = 4.0f;?float q = 4.0f;Float q = 4.0f;double q = 4.0f;Double q = 4.0f;Object q = 4.0f;

4 What is the output of the following?12: var b = "12"; 13: b += "3"; 14: b.reverse(); 15: System.out.println(b.toString());12123321The code does not compile.

5 What is the output of the following?5: var line = new StringBuilder("-"); 6: var anotherLine = line.append("-"); 7: System.out.print(line == anotherLine); 8: System.out.print(" "); 9: System.out.print(line.length());false 1false 2true 1true 2It does not compile.

6 Given the following Venn diagram and the boolean variables, apples, oranges, and bananas, which expression most closely represents the filled‐in region of the diagram?apples && oranges && !bananasorange || (oranges && !bananas)(apples || bananas) && orangesoranges && apples(apples || oranges) && !bananasapples ^ oranges

7 What is the output of the following?5: var line = new String("-"); 6: var anotherLine = line.concat("-"); 7: System.out.print(line == anotherLine); 8: System.out.print(" "); 9: System.out.print(line.length());false 1false 2true 1true 2Does not compile

8 Which can fill in the blank? (Choose two.)public void math() { _____ pi = 3.14; }bytedoublefloatshortvar

9 Fill in the blanks: The operators !=, _______, _______, _______, and ++ are listed in the same or increasing level of operator precedence. (Choose two.)==, *, !/, %, **, ‐‐, /!, *, %+=, &&, **, <, /

10 How many of these compile?18: Comparator<String> c1 = (j, k) -> 0; 19: Comparator<String> c2 = (String j, String k) -> 0; 20: Comparator<String> c3 = (var j, String k) -> 0; 21: Comparator<String> c4 = (var j, k) -> 0; 22: Comparator<String> c5 = (var j, var k) -> 0;012345

11 The author of this method forgot to include the data type. Which of the following reference types can best fill in the blank to complete this method?public static void secret(___________ mystery) { char ch = mystery.charAt(3); 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

OCP Oracle Certified Professional Java SE 11 Developer Practice Tests

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