1. What is the output of the following code snippet?
1. int temperature = 4;
2. long humidity = -temperature + temperature * 3;
3. if (temperature>=4)
4. if (humidity < 6) System.out.println(“Too Low”);
5. else System.out.println(“Just Right”);
6. else System.out.println(“Too High”);
Just Right
2. What is the output of calling printType(11)?
1. void printType(Object o) {
2. if(o instanceof Integer bat) {
3. System.out.print(“int”);
4. } else if(o instanceof Integer bat && bat < 10) {
5. System.out.print(“small int”);
6. } else if(o instanceof Long bat || bat <= 20) {
7. System.out.print(“long”);
8. } default {
9. System.out.print(“unknown”);
10. }
11. }
The code contains two lines that do not compile.
3. Which statements about pattern matching and flow scoping are correct?
Each correct answer represents a complete solution. Choose all that apply.
A) Pattern matching with an if statement is implemented using the instanceof operator.
Flow scoping means a pattern variable is only accessible if the compiler can discern its type.
4. What is the output of the following code snippet?
int penguin = 50, turtle = 75;
boolean older = penguin>= turtle;
if (older = true) System.out.println(“Success”);
else System.out.println(“Failure”);
else if(penguin != 50) System.out.println(“Other”);
A) None of these
5. What is the output of calling getFish(“goldie”)?
void getFish(Object fish) {
if (!(fish instanceof String guppy))
System.out.print(“Eat!”);
else if (!(fish instanceof String guppy)) {
throw new RuntimeException();
}
System.out.print(“Swim!”);
}
A) None of these
6. Given the following method, how many lines contain compilation errors?
1. private DayOfWeek getWeekDay(int day, final int thursday) { 2. int otherDay = day;
3. int Sunday = 0;
4. switch(otherDay) {
5. default:
6. case 1: continue;
7. case thursday: return DayOfWeek.THURSDAY;
8. case 2,10: break;
9. case Sunday: return DayOfWeek.SUNDAY;
10. case DayOfWeek.MONDAY: return DayOfWeek.MONDAY; 11. }
12. return DayOfWeek.FRIDAY;
13. }
A) 4
7. Which of the following data types can be used in a switch expression?
Each correct answer represents a complete solution. Choose all that apply.
A)
Enum
Int
Byte
String
Char
Var
8. 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);
}
A) None of these
9. What is the output of calling printLocation(Animal.MAMMAL)?
1. class Zoo {
2. enum Animal {BIRD, FISH, MAMMAL}
3. void printLocation(Animal a) {
4. long type = switch(a) {
5. case BIRD -> 1;
6. case FISH -> 2;
7. case MAMMAL -> 3;
8. default -> 4;
9. };
10. System.out.print(type);
11. } }
A) 3
10. 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 “);
}
A) None of these
11. A minimum of how many lines need to be corrected before the following method will compile?
1. void findZookeeper(Long id) {
2. System.out.print(switch(id) {
3. case 10 -> {“Jane”}
4. case 20 -> {yield “Lisa”;};
5. case 30 -> “Kelly”;
6. case 30 -> “Sarah”;
7. default -> “Unassigned”;
8. });
9. }
A) Four
12. What is the output of the following code snippet?
1. String instrument = “violin”;
2. final String CELLO = “cello”;
3. String viola = “viola”;
4. int p = -1;
5. switch(instrument) {
6. case “bass” : break;
7. case CELLO : p++;
8. default: p++;
9. case “VIOLIN”: p++;
10. case “viola” : ++p; break;
11. }
12. System.out.print(p);
A) 2
13. What is printed by the following code snippet?
1. byte amphibian = 1;
2. String name = “Frog”;
3. String color = switch(amphibian) {
4. case 1 -> { yield “Red”; }
5. case 2 -> { if(name.equals(“Frog”)) yield “Green”; } 6. case 3 -> { yield “Purple”; }
7. default -> throw new RuntimeException(); 8. };
9. System.out.print(color);
A) The code does not compile.
14. What is the result of the following code snippet?
1. int sing = 8, squawk = 2, notes = 0;
2. while(sing> squawk) {
3. sing–;
4. squawk += 2;
5. notes += sing + squawk;
6. } 7. System.out.println(notes);
A) 23
15. What is the output of the following code snippet?
1. boolean keepGoing = true;
2. int result = 15, meters = 10;
3. do {
4. meters–;
5. if(meters==8) keepGoing = false;
6. result -= 2;
7. } while keepGoing;
8. System.out.println(result);
A) The code does not compile for a different reason.
16. What is the output of the following code snippet?
1. double iguana = 0;
2. do {
3. int snake = 1;
4. System.out.print(snake++ + ” “);
5. iguana–;
6. } while (snake <= 5);
7. System.out.println(iguana);
A) The code does not compile.
17. What is the output of the following code snippet?
1. var tailFeathers = 3;
2. final var one = 1;
3. switch (tailFeathers) {
4. case one: System.out.print(3 + ” “);
5. default: case 3: System.out.print(5 + ” “); 6. }
7. while (tailFeathers> 1) { 8. System.out.print(–tailFeathers + ” “); }
A) 5 2 1
18. What is the output of the following code snippet?
1. int w = 0, r = 1;
2. String name = “”;
3. while(w < 2) {
4. name += “A”;
5. do {
6. name += “B”;
7. if(name.length()>0) name += “C”;
8. else break;
9. } while (r <=1);
10. r++; w++; } 11. System.out.println(name);
11. System.out.println(name);
A) The code compiles but never terminates at runtime.
19. 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. } }
A) -1 0 1 2 3 4 5 6
20. Which of the following data types are permitted on the right side of a for each expression?
Each correct answer represents a complete solution. Choose all that apply.
A) Double[][]
List
Set
char[]
21. Which statements about decision structures are true?
Each correct answer represents a complete solution. Choose all that apply.
A) 1. The conditional expression of a for loop is evaluated before the first execution of the loop body.
2. A switch expression that takes a String and assigns the result to a variable requires a default branch.
3. The body of a do/while loop is guaranteed to be executed at least once.
22. 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?
private void print(int[] weather) {
for(__________________) {
System.out.println(weather[i]);
}
}
Each correct answer represents a complete solution. Choose all that apply.
A) 1. int i=0; i<=weather.length-1; ++I
2. int i=weather.length-1; i>=0; i—
23. Which statements about the following code snippet are correct?
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);
Each correct answer represents a complete solution. Choose all that apply.
A) 1. The data type of penguin is int.
2. The data type of emu is Character.
3. The data type of macaw is Integer.
24. Given the following array, which code snippets print the elements in reverse order from how they are declared?
char[] wolf = {‘W’, ‘e’, ‘b’, ‘b’, ‘y’};
Each correct answer represents a complete solution. Choose all that apply.
A)
1. int q = wolf.length;for( ; ; ) { System.out.print(wolf[–q]); if(q==0) break;}
2. for(int m=wolf.length-1; m>=0; –m) System.out.print(wolf[m]);
3. int x = wolf.length-1;for(int j=0; x>=0 && j==0; x–) System.out.print(wolf [x]);
25. What distinct numbers are printed when the following method is executed?
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);
}
Each correct answer represents a part of the solution. Choose all that apply.
A)
3
10
26. Which of the following are possible data types for friends that would allow the code to compile?
for(var friend in friends) {
System.out.println(friend);
}
A) None of these
27. What is the output of the following code snippet?
List<Integer> myFavoriteNumbers = new ArrayList<>();
myFavoriteNumbers.add(10);
myFavoriteNumbers.add(14);
System.out.print(a + “, “);
break;
}
for (int b : myFavoriteNumbers) {
continue;
System.out.print(b + “, “);
}
for (Object c : myFavoriteNumbers)
System.out.print(c + “, “);
A) Exactly one line of code does not compile.
28. Which statements, when inserted independently into the following blank, will cause the code to print 2 at runtime?
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);
Each correct answer represents a complete solution. Choose all that apply.
A)
1. break RABBIT
2. continue BUNNY
3. break
29. Which statements, when inserted into the following blanks, allow the code to compile and run without entering an infinite loop?
1. int height = 1;
2. L1: while(height++ <10) {
3. long humidity = 12;
4. L2: do {
5. if(humidity– % 12 == 0) ________________;
6. int temperature = 30;
7. L3: for( ; ; ) {
8. temperature++;
9. if(temperature>50) ________________;
10. }
11. } while (humidity > 4);
12. }
Each correct answer represents a part of the solution. Choose all that apply.
A)
1. break L2 on line 8; continue L2 on line 12
2. continue L2 on line 8; continue L2 on line 12
Other Links:
See other websites for quiz:
Check on QUIZLET