Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 215

Review Questions

Оглавление

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 in which it is declared.Flow scoping means a pattern variable is only accessible if the compiler can discern its type.Pattern matching can be used to declare a variable with an else statement.

19 What is the output of the following code snippet?2: double iguana = 0; 3: do { 4: int snake = 1; 5: System.out.print(snake++ + " "); 6: iguana--; 7: } while (snake <= 5); 8: System.out.println(iguana); 1 2 3 4 -4.01 2 3 4 -5.01 2 3 4 5 -4.00 1 2 3 4 5 -5.0The code does not compile.The code compiles but produces an infinite loop at runtime.None of the above

20 Which statements, when inserted into the following blanks, allow the code to compile and run without entering an infinite loop? (Choose all that apply.)4: int height = 1; 5: L1: while(height++ <10) { 6: long humidity = 12; 7: L2: do { 8: if(humidity-- % 12 == 0) ________________; 9: int temperature = 30; 10: L3: for( ; ; ) { 11: temperature++; 12: if(temperature>50) ________________; 13: } 14: } while (humidity > 4); 15: }break L2 on line 8; continue L2 on line 12continue on line 8; continue on line 12break L3 on line 8; break L1 on line 12continue L2 on line 8; continue L3 on line 12continue L2 on line 8; continue L2 on line 12None of the above, as the code contains a compiler error

21 A minimum of how many lines need to be corrected before the following method will compile?21: void findZookeeper(Long id) { 22: System.out.print(switch(id) { 23: case 10 -> {"Jane"} 24: case 20 -> {yield "Lisa";}; 25: case 30 -> "Kelly"; 26: case 30 -> "Sarah"; 27: default -> "Unassigned"; 28: }); 29: }ZeroOneTwoThreeFourFive

22 What is the output of the following code snippet? (Choose all that apply.)2: var tailFeathers = 3; 3: final var one = 1; 4: switch (tailFeathers) { 5: case one: System.out.print(3 + " "); 6: default: case 3: System.out.print(5 + " "); 7: } 8: while (tailFeathers> 1) { 9: System.out.print(--tailFeathers + " "); }35 15 23 5 15 2 1The code will not compile because of lines 3–5.The code will not compile because of line 6.

23 What is the output of the following code snippet?15: int penguin = 50, turtle = 75; 16: boolean older = penguin>= turtle; 17: if (older = true) System.out.println("Success"); 18: else System.out.println("Failure"); 19: else if(penguin != 50) System.out.println("Other");SuccessFailureOtherThe code will not compile because of line 17.The code compiles but throws an exception at runtime.None of the above

24 Which of the following are possible data types for friends that would allow the code to compile? (Choose all that apply.) for(var friend in friends) { System.out.println(friend); }SetMapStringint[]CollectionStringBuilderNone of the above

25 What is the output of the following code snippet?6: String instrument = "violin"; 7: final String CELLO = "cello"; 8: String viola = "viola"; 9: int p = -1; 10: switch(instrument) { 11: case "bass" : break; 12: case CELLO : p++; 13: default: p++; 14: case "VIOLIN": p++; 15: case "viola" : ++p; break; 16: } 17: System.out.print(p); -10123The code does not compile.

26 What is the output of the following code snippet? (Choose all that apply.)9: int w = 0, r = 1; 10: String name = ""; 11: while(w < 2) { 12: name += "A"; 13: do { 14: name += "B"; 15: if(name.length()>0) name += "C"; 16: else break; 17: } while (r <=1); 18: r++; w++; } 19: System.out.println(name);ABCABCABCABCABCABCLine 15 contains a compilation error.Line 18 contains a compilation error.The code compiles but never terminates at runtime.The code compiles but throws a NullPointerException at runtime.

27 What is printed by the following code snippet?23: byte amphibian = 1; 24: String name = "Frog"; 25: String color = switch(amphibian) { 26: case 1 -> { yield "Red"; } 27: case 2 -> { if(name.equals("Frog")) yield "Green"; } 28: case 3 -> { yield "Purple"; } 29: default -> throw new RuntimeException(); 30: }; 31: System.out.print(color);RedGreenPurpleRedPurpleAn exception is thrown at runtime.The code does not compile.

28 What is the output of calling getFish("goldie")?40: void getFish(Object fish) { 41: if (!(fish instanceof String guppy)) 42: System.out.print("Eat!"); 43: else if (!(fish instanceof String guppy)) { 44: throw new RuntimeException(); 45: } 46: System.out.print("Swim!"); 47: }Eat!Swim!Eat! followed by an exception.Eat!Swim!An exception is printed.None of the above

29 What is the result of the following code?1: public class PrintIntegers { 2: public static void main(String[] args) { 3: int y = -2; 4: do System.out.print(++y + " "); 5: while(y <= 5); 6: } }-2 -1 0 1 2 3 4 5-2 -1 0 1 2 3 4-1 0 1 2 3 4 5 6-1 0 1 2 3 4 5The code will not compile because of line 5.The code contains an infinite loop and does not terminate.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

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