Chapter 3: Making Decisions Flashcards

1
Q

What types can be used in a switch expression?

enum
int
Byte
String
char
var

A

all of them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what data types are allowed on the right of a for-each expression?

A

Double[][]
List
char[]
Set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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);
}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

why are the following statements true?

  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

The body of a do/while loop is guaranteed to be executed at least one

A
  1. 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.
  2. switch expressions have to be exhaustive because they’re assigning a variable, you can’t have the variable = condition not met.
  3. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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:}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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: }

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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);
} }

A

No.
the default is optional is all enums are accounted for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly