Chapter 4 Flashcards

1
Q

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
A

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

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

True or false

The following switch statement is valid

int month = 5;
switch (month) { }

A

A switch statement may contain 0 or more case branches, with an optional default branch, making this switch statement valid.

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

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");
A

“Just Right”

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

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;
    }
}
A
  • “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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

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.

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

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 + “ “);

A

0 1 2 3 4 5

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

What is the output of the following code?

int x = 0;
for (int x = 4; x < 5; x++) {
System.out.println(x + “ “);
}

A

x has already been declared and therefore this code does not compile.

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

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

A

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.

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

What is the output of the following code?

for (int i = 0; i < 10; i++)
i = 0;

A

This created an infitine loop.

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

What Object types can be used in the right side of the condition in a for-each loop?

A
  • A built-in Java array

- An object whose type implements Iterable.

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

Does the following code compile?

int minute = 1;
WATCH: while(minute > 2) {
    if (minute++>2) {
        continue WATCH;
   }
}
A

Even though it is not logically posibble for the if statement to evaluate to true, this compiles fine.

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

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

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

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

D.

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