Chapter 3: Making Decisions Flashcards
What types can be used in a switch expression?
enum
int
Byte
String
char
var
all of them
what data types are allowed on the right of a for-each expression?
Double[][]
List
char[]
Set
output of this?
void printSomething( int x) {
var y = switch(x) {
case 1,2 -> “test1,2”;
case 3,4 -> “test3,4”;
case 5,6 -> “ok you get the picture”;
}
print(y);
}
won’t compile - a switch EXPRESSION
requires all possible cases to be accounted for
it would work if there was a default case put in there.
List<Integer> xyz = new ArrayList<>();
xyz.add(10);
xyz.add(14);
for ( var a : xyz ) {
print a;
break;
}</Integer>
for ( int b : xyz ) {
continue;
print b
}
for ( Object c : xyz )
print c
1 line doesn’t compile
it’s the continue statement in b’s for loop
you’ll never reach ‘print b’
unreachable code -> compilation error
why are the following statements true?
- 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 one
- a for-loop works on any Collections object that implements java.lang.Iterable, such as List or Set, but not all Collection classes such as Map.
- switch expressions have to be exhaustive because they’re assigning a variable, you can’t have the variable = condition not met.
- the body of a do/while operates at least once, versus 0+ times for a plain old while loop. Due to when the condition is checked
What is the output of calling printType(11)?
31: void printType( Object o) {
32: if (o instanceof Integer b) {
33: print(“int”);
34: } else if (o instanceof Integer b
&& b<10) {
35: print(“small int”);
36: } else if (o instanceof Long b
|| b <= 20) {
37: print(“long”);
38: } default {
39: print(“unknown”)
40: }
41:}
The default keyword is used incorrectly. It is not used in if/else statements but in switch statements.
The condition o instanceof Integer b && b < 10 will never be true because if o is an instance of Integer, it would have already entered the first if block.
In the line } else if (o instanceof Long b || b <= 20) {, the variable b will be out of scope if o is not an instance of Long. So, trying to use b in the b <= 20 comparison will result in a compile-time error.
What lets the following code print 2 at a 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++;
}
print count;
pro-tip, exclude an answer where count exceeds 2
break; or continue BUNNY;
or break RABBIT;
for break:
row: 1, col: 0, count: 0
row: 1, col: 1, count: 1
row: 2, col: 0, count: 1
row: 3, col: 0, count: 1
row: 3, col: 1, count: 2
2
the only time count gets incremented is when col + row % 2 is 0, which will only be at 2 and 4 (6 is barred by col < 3)
How many lines contain compilation errors?
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: }
Line 15: continue can’t be used inside a switch statement like that;
Line 16: not a compile-time constant (marking it as final in the params doesn’t change that any int can be passed in so it won’t compile.)
Line 18: won’t compile because Sunday is not marked as final - being effectively final isn’t sufficient.
Line 19: does not compile because DayOfWeek.MONDAY is not an int. While switch statements do support enum values, each case statement must have the same data type as the switch variable (here, it’s otherDay).
Do you need a default statement for switch statements takes enums, if all enums are accounted for?
e.g.
enum Animal {BIRD, FISH, BAT}
void testEnumSwitch(Animal a) {
long type = switch(a) {
case BIRD -> 1;
case FISH -> 2;
case BAT -> 3;
// default -> 4;
};
System.out.println(type);
} }
No.
the default is optional is all enums are accounted for