OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky. Читать онлайн. Newlib. NEWLIB.NET

Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Программы
Год издания: 0
isbn: 9781119864592
Скачать книгу
and for-each loops and know how to write and evaluate them. Each loop has its own special properties and structures. You should know how to use for-each loops to iterate over lists and arrays.

      Understand how break, continue, and return can change flow control. Know how to change the flow control within a statement by applying a break, continue, or return statement. Also know which control statements can accept break statements and which can accept continue statements. Finally, you should understand how these statements work inside embedded loops or switch statements.

      The answers to the chapter review questions can be found in the Appendix.

      1 Which of the following data types can be used in a switch expression? (Choose all that apply.)enumintBytelongStringcharvardouble

      2 What is the output of the following code snippet? (Choose all that apply.)3: int temperature = 4; 4: long humidity = -temperature + temperature * 3; 5: if (temperature>=4) 6: if (humidity < 6) System.out.println("Too Low"); 7: else System.out.println("Just Right"); 8: else System.out.println("Too High");Too LowJust RightToo HighA NullPointerException is thrown at runtime.The code will not compile because of line 7.The code will not compile because of line 8.

      3 Which of the following data types are permitted on the right side of a for-each expression? (Choose all that apply.)Double[][]ObjectMapListStringchar[]ExceptionSet

      4 What is the output of calling printReptile(6)?void printReptile(int category) { var type = switch(category) { case 1,2 -> "Snake"; case 3,4 -> "Lizard"; case 5,6 -> "Turtle"; case 7,8 -> "Alligator"; }; System.out.print(type); }SnakeLizardTurtleAlligatorTurtleAlligatorNone of the above

      5 What is the output of the following code snippet? List<Integer> myFavoriteNumbers = new ArrayList<>(); myFavoriteNumbers.add(10); myFavoriteNumbers.add(14); for (var a : myFavoriteNumbers) { System.out.print(a + ", "); break; } for (int b : myFavoriteNumbers) { continue; System.out.print(b + ", "); } for (Object c : myFavoriteNumbers) System.out.print(c + ", ");It compiles and runs without issue but does not produce any output.10, 14,10, 10, 14,10, 10, 14, 10, 14,Exactly one line of code does not compile.Exactly two lines of code do not compile.Three or more lines of code do not compile.The code contains an infinite loop and does not terminate.

      6 Which statements about decision structures are true? (Choose all that apply.)A for-each loop can be executed on any Collections Framework object.The body of a while loop is guaranteed to be executed at least once.The conditional expression of a for loop is evaluated before the first execution of the loop body.A switch expression that takes a String and assigns the result to a variable requires a default branch.The body of a do/while loop is guaranteed to be executed at least once.An if statement can have multiple corresponding else statements.

      7 Assuming weather is a well-formed nonempty array, which code snippet, when inserted independently into the blank in the following code, prints all of the elements of weather? (Choose all that apply.) private void print(int[] weather) { for(__________________) { System.out.println(weather[i]); } }int i=weather.length; i>0; i--int i=0; i<=weather.length-1; ++ivar w : weatherint i=weather.length-1; i>=0; i--int i=0, int j=3; i<weather.length; ++iint i=0; ++i<10 && i<weather.length;None of the above

      8 What is the output of calling printType(11)?31: void printType(Object o) { 32: if(o instanceof Integer bat) { 33: System.out.print("int"); 34: } else if(o instanceof Integer bat && bat < 10) { 35: System.out.print("small int"); 36: } else if(o instanceof Long bat || bat <= 20) { 37: System.out.print("long"); 38: } default { 39: System.out.print("unknown"); 40: } 41: }intsmall intlongunknownNothing is printed.The code contains one line that does not compile.The code contains two lines that do not compile.None of the above

      9 Which statements, when inserted independently into the following blank, will cause the code to print 2 at runtime? (Choose all that apply.) int count = 0; BUNNY: for(int row = 1; row <=3; row++) RABBIT: for(int col = 0; col <3 ; col++) { if((col + row) % 2 == 0) ___________________; count++; } System.out.println(count); break BUNNYbreak RABBITcontinue BUNNYcontinue RABBITbreakcontinueNone of the above, as the code contains a compiler error.

      10 Given the following method, how many lines contain compilation errors? (Choose all that apply.)10: private DayOfWeek getWeekDay(int day, final int thursday) { 11: int otherDay = day; 12: int Sunday = 0; 13: switch(otherDay) { 14: default: 15: case 1: continue; 16: case thursday: return DayOfWeek.THURSDAY; 17: case 2,10: break; 18: case Sunday: return DayOfWeek.SUNDAY; 19: case DayOfWeek.MONDAY: return DayOfWeek.MONDAY; 20: } 21: return DayOfWeek.FRIDAY; 22: }None, the code compiles without issue.123456The code compiles but may produce an error at runtime.

      11 What is the output of calling printLocation(Animal.MAMMAL)?10: class Zoo { 11: enum Animal {BIRD, FISH, MAMMAL} 12: void printLocation(Animal a) { 13: long type = switch(a) { 14: case BIRD -> 1; 15: case FISH -> 2; 16: case MAMMAL -> 3; 17: default -> 4; 18: }; 19: System.out.print(type); 20: } }3434The code does not compile because of line 13.The code does not compile because of line 17.None of the above

      12 What is the result of the following code snippet?3: int sing = 8, squawk = 2, notes = 0; 4: while(sing> squawk) { 5: sing--; 6: squawk += 2; 7: notes += sing + squawk; 8: } 9: System.out.println(notes);1113233350The code will not compile because of line 7.

      13 What is the output of the following code snippet?2: boolean keepGoing = true; 3: int result = 15, meters = 10; 4: do { 5: meters--; 6: if(meters==8) keepGoing = false; 7: result -= 2; 8: } while keepGoing; 9: System.out.println(result);79101115The code will not compile because of line 6.The code does not compile for a different reason.

      14 Which statements about the following code snippet are correct? (Choose all that apply.) for(var penguin : new int[2]) System.out.println(penguin); var ostrich = new Character[3]; for(var emu : ostrich) System.out.println(emu); List<Integer> parrots = new ArrayList<Integer>(); for(var macaw : parrots) System.out.println(macaw);The data type of penguin is Integer.The data type of penguin is int.The data type of emu is undefined.The data type of emu is Character.The data type of macaw is List.The data type of macaw is Integer.None of the above, as the code does not compile.

      15 What is the result of the following code snippet? final char a = 'A', e = 'E'; char grade = 'B'; switch (grade) { default: case a: case 'B': 'C': System.out.print("great "); case 'D': System.out.print("good "); break; case e: case 'F': System.out.print("not good "); }greatgreat goodgoodnot goodThe code does not compile because the data type of one or more case statements does not match the data type of the switch variable.None of the above

      16 Given the following array, which code snippets print the elements in reverse order from how they are declared? (Choose all that apply.) char[] wolf = {'W', 'e', 'b', 'b', 'y'}; int q = wolf.length; for( ; ; ) { System.out.print(wolf[--q]); if(q==0) break; } for(int m=wolf.length-1; m>=0; --m) System.out.print(wolf[m]); for(int z=0; z<wolf.length; z++) System.out.print(wolf[wolf.length-z]); int x = wolf.length-1; for(int j=0; x>=0 && j==0; x--) System.out.print(wolf[x]); final int r = wolf.length; for(int w = r-1; r>-1; w = r-1) System.out.print(wolf[w]); for(int i=wolf.length; i>0; --i) System.out.print(wolf[i]);None of the above

      17 What distinct numbers are printed when the following method is executed? (Choose all that apply.) private void countAttendees() { int participants = 4, animals = 2, performers = -1; while((participants = participants+1) < 10) {} do {} while (animals++ <= 1); for( ; performers<2; performers+=2) {} System.out.println(participants); System.out.println(animals); System.out.println(performers); }6345109The code does not compile.None of the above

      18 Which statements about pattern matching and flow scoping are correct? (Choose all that apply.)Pattern matching with an if statement is implemented using the instance operator.Pattern matching with an if statement is implemented using the instanceon operator.Pattern matching with an if statement is implemented using the instanceof operator.The pattern variable cannot be accessed after the if statement