Chapter 4 Flashcards
Which of the following data types can be used in a switch statement?
A) enum B) int C) Byte D) long E) String F) char G) var H) double
All primitive numeric data types that can be promoted to an int + their wrapper class representatives can be used in a switch statement. Enums, Strings and var can also be used.
A, B, C, E, F, G
True or false
The following switch statement is valid
int month = 5;
switch (month) { }
A switch statement may contain 0 or more case branches, with an optional default branch, making this switch statement valid.
What is the output of the following code snippet?
int temperature = 4; long humidity = -temperature + temperature * 3; if (1 > 0) if (temperature >= 4) if (humidity < 6) System.out.println("Too Low"); else System.out.println("Just Right"); else System.out.println("Too High"); else System.out.println("lol");
“Just Right”
Which of the following cases are valid and which arent?
private void test(String firstName, final String lastName) {
String middleName = “Jose”;
final String suffix = “W”;
switch(firstName) { case "Test": case middleName: break; case suffix: break; case lastName: break; case 5: break; case 'J': break; case java.time.DayOfWeek.SUNDAY: break; } }
- “Test” compiles without issue since it’s a String literal.
- middleName does not compile because it is not a constant value (final missing).
- suffix compiles without issue because it is a final constant variable.
- lastName does not compile because it is not a constant as it is passed to the function.
- The last thee cases do not compile because they are not of type String.
Which of the following cases are valid and which arent?
short size = 4;
final int small = 15;
final int big = 1_000_000;
switch (size) { case small: case 1+2: case big: }
Switch statements support numeric promotion that does not require an explicit cast. The compiler can easily cast the variable small from int to short at compile time because the value 15 is small enough to fit inside a short and it is a constant variable.
1+2 can also be converted to a short because it is small enough.
1_000_000 on the other hand is too large to fit inside a short without an explicit cast, so this case does not compile.
What is the output of the following code?
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.println(y + “ “); }
System.out.println(x + “ “);
0 1 2 3 4 5
What is the output of the following code?
int x = 0;
for (int x = 4; x < 5; x++) {
System.out.println(x + “ “);
}
x has already been declared and therefore this code does not compile.
What is the output of the following code?
int x = 0;
for (long y = 0, int z = 4; x < 5; x++) {
System.out.println(y + “ “);
}
The variables in the initialization block must all be of the same type. y is a long while z is an int, which are not compatible. This code will not compile.
What is the output of the following code?
for (int i = 0; i < 10; i++)
i = 0;
This created an infitine loop.
What Object types can be used in the right side of the condition in a for-each loop?
- A built-in Java array
- An object whose type implements Iterable.
Does the following code compile?
int minute = 1; WATCH: while(minute > 2) { if (minute++>2) { continue WATCH; } }
Even though it is not logically posibble for the if statement to evaluate to true, this compiles fine.
Which of the following are possible data types for olivia that would allow the code to compile?
for (var sophia : olivia) {
System.out.println(sophia);
}
A. Set B. Map C. String D. int[] E. Collection F. StringBuilder G. None of the above.
A, D, E
The following can be in the right side of a for-each loop:
- A built-in Java array
- An object whose type implements Iterable.
What is the output of the following code?
String instrument = "violin"; final String CELLO = "cello"; String viola = "viola"; int p = -1; switch(instrument) { case "bass" : break; case CELLO: p++; default: p++; case "VIOLIN": p++; case "viola": ++p; break; }
A. -1 B. 0 C. 1 D. 2 E. 3 F. This code does not compile.
D.