Programmer I Chapter 4: Making decisions Flashcards
what data types are permitted as targets for switch?
- int, short, byte, char + their wrapper classes
- enums
- String
- a var variable if its actual type is one of the above
boolean, long, float, and double are excluded from switch statements, as are their associated Boolean, Long,Float, and Double classes
what will this print?
int dayOfWeek = 5; switch(dayOfWeek) { default: System.out.println("Weekday"); break; case 0: System.out.println("Sunday"); break; case 6: System.out.println("Saturday"); break; }
Weekday
what will this print?
var dayOfWeek = 5; switch(dayOfWeek) { case 0: System.out.println("Sunday"); default: System.out.println("Weekday"); case 6: System.out.println("Saturday"); break; }
Weekday
Saturday
what will this print?
var dayOfWeek = 6; switch(dayOfWeek) { case 0: System.out.println("Sunday"); default: System.out.println("Weekday"); case 6: System.out.println("Saturday"); break; }
Saturday
what case statements compile?
private int getSortOrder(String firstName, final String lastName){ String middleName = "Patricia"; final String suffix = "JR"; int id = 0; switch(firstName) { case "Test": // 1 return 52; case middleName: // 2 id = 5; break; case suffix: // 3 id = 0; break; case lastName: // 4 id = 8; break; case 5: // 5 id = 7; break; case 'J': // 6 id = 10; break; case java.time.DayOfWeek.SUNDAY: // 7 id=15; break; } return id; }
1, 3
what case statements compile?
short size = 4; final int aa = 15; final int bb = 1_000_000; switch(size) { case aa: // 1 case 1+2 : // 2 case bb: // 3 }
1, 2
As you may recall from our discussion of numeric promotion and casting in Chapter 3, the compiler can easily cast small from int to short at compile-time because the value 15 is small enough to fit inside a short. This would not be permitted if small was not a compile-time constant. Likewise, it can convert the expression 1+2 from int to short at compile-time. On the other hand, 1_000_000 is too large to fit inside of short without an explicit cast, so the last case statement does not compile.
what will this print?
int x = 0;
for(long y = 0, z = 4; x < 5 && y < 10; x++, y++) { System.out.print(y + “ “); }
System.out.print(x + “ “);
0 1 2 3 4 5
what will this print?
int x = 0;
for(int x = 4; x < 5; x++) {
System.out.print(x + “ “);
}
it does not compilebecause of the initialization block
what will this print?
int x = 0;
for(long y = 0, int z = 4; x < 5; x++) {
System.out.print(y + “ “);
}
wont compile
The variables in the initialization block must allbe of the same type. In the multiple terms example, y and z were bothlong, so the code compiled without issue, but in this example they havediffering types, so the code will not compile.
what will be the values of i, j, k after running the snippets? 1: for(int i=0; i<10; i++) i = 0; 2: for(int j=1; j<10; j++) j--; 3: for(int k=0; k<10; ) k++;
1 and 2 are infinite loops,
3: k = 10
all snippets compile as Java allows to modify loop variables
what will this code output?
final String[] names = new String[3];
names[0] = "Lisa"; names[1] = "Kevin"; names[2] = "Roger";
for(String name : names) {
System.out.print(name + “, “);
}
Lisa, Kevin, Roger,
what will be the value of chackDate?
int checkDate = 0; while(checkDate<10) { checkDate++; if(checkDate>100) { break; checkDate++; } }
code wont compile because checkDate++ is unreachable
Which of the following data types can be used in a switchstatement? (Choose all that apply.)
A. enum B. int C. Byte D. long E. String F. char G. var H. double
A, B, C, E, F, G. A switch statement supports the primitives int,byte, short, and char, along with their associated wrapper classes Integer, Byte, Short, and Character, respectively, making options B, C, and F correct. It also supports enum and String, making options A and E correct. Finally, switch supports var if the type can be resolved to a supported switch data type, making option Gcorrect. Options D and H are incorrect as long, float, double, andtheir associated wrapped classes Long, Float, and Double,respectively, are not supported in switch statements.
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”);
A. Too Low
B. Just Right
C. Too High
D. A NullPointerException is thrown at runtime.
E. The code will not compile because of line 7.
F. The code will not compile because of line 8.
B. The code compiles and runs without issue, so options D, E, andF are incorrect. Even though the two consecutive else statementson lines 7 and 8 look a little odd, they are associated with separateif statements on lines 5 and 6, respectively. The value of humidityon line 4 is equal to -4 + 12, which is 8. The first if statementevaluates to true on line 5, so line 6 is executed and its associatedelse statement on line 8 is not. The if statement on line 6evaluates to false, causing the else statement on line 7 toactivate. The result is the code prints Just Right, making option Bthe correct answer.
What is the output of the following code snippet?
List 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 + “, “);
A. It compiles and runs without issue but does not produce any output.
B. 10, 14,
C. 10, 10, 14,
D. 10, 10, 14, 10, 14,
E. Exactly one line of code does not compile.
F. Exactly two lines of code do not compile.
G. Three or more lines of code do not compile.
H. The code contains an infinite loop and does not terminate.
E. The second for-each loop contains a continue followed by aprint() statement. Because the continue is not conditional andalways included as part of the body of the for-each loop, theprint() statement is not reachable. For this reason, the print()statement does not compile. As this is the only compilation error,option E is correct. The other lines of code compile without issue.In particular, because the data type for the elements ofmyFavoriteNumbers is Integer, they can be easily unboxed to int orreferenced as Object. For this reason, the lines containing the for-each expressions each compile