Chapter 4 Review Questions Flashcards

1
Q
  1. Which of the following data types can be used in a switch statement? (Choose all that apply.)
    A. enum
    B. int
    C. Byte
    D. long
    E. String
    F.char
    G.var
    H.double
A

A,B,C,E,F

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

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

A

B

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

3.What is the output of the following code snippet?
~~~
List<Integer> 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.</Integer>

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

4.Which statements about decision structures are true? (Choose all that apply.)
A. A for-each loop can be executed on any Collections Framework object.
B. The body of a while loop is guaranteed to be executed at least once.
C. The conditional expression of a for loop is evaluated before the first execution of the loop body.
D. A switch statement with no matching case statement
requires a default statement.
E. The body of a do/while loop is guaranteed to be executed at least once.
F.An if statement can have multiple corresponding else
statements.

A

C, E

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

5.Assuming weather is a well-formed nonempty array, which code snippet, when inserted independently into the blank in the following code, prints all of the elements of weather? (Choose all that apply.)

private void print(int[] weather) {
    for(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_) {
        System.out.println(weather[i]);
    }
}

A. int i=weather.length; i>0; i--
B. int i=0; i<=weather.length-1; ++i
C. var w : weather
D. int i=weather.length-1; i>=0; i–
E. int i=0, int j=3; i<weather.length; ++i
F. int i=0; ++i<10 && i<weather.length;
G. None of the above

A

A,B,D,E

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

6.Which statements, when inserted independently into the following blank, will cause the code to print 2 at runtime? (Choose all that apply.)
~~~
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++;
}
System.out.println(count);
~~~
A. break BUNNY
B. break RABBIT
C. continue BUNNY
D. continue RABBIT
E. break
F.continue
G.None of the above, as the code contains a compiler error

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

7.Given the following method, how many lines contain compilation errors? (Choose all that apply.)
~~~
private DayOfWeek getWeekDay(int day, final int thursday) {
int otherDay = day;
int Sunday = 0;
switch(otherDay) {
default:
case 1: continue;
case thursday: return DayOfWeek.THURSDAY;
case 2: break;
case Sunday: return DayOfWeek.SUNDAY;
case DayOfWeek.MONDAY: return DayOfWeek.MONDAY;
}
return DayOfWeek.FRIDAY;
}
~~~
A. None, the code compiles without issue.
B. 1
C. 2
D. 3
E. 4
F.5
G.6
H.The code compiles but may produce an error at runtime.

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

8.What is the result of the following code snippet?
~~~
3: int sing = 8, squawk = 2, notes = 0;
4: while(sing > squawk) {
5: sing–;
6: squawk += 2;
7: notes += sing + squawk;
8: }
9: System.out.println(notes);
~~~
A. 11
B. 13
C. 23
D. 33
E. 50
F.The code will not compile because of line 7.

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

9.What is the output of the following code snippet?
~~~
2: boolean keepGoing = true;
3: int result = 15, meters = 10;
4: do {
5: meters–;
6: if(meters==8) keepGoing = false;
7: result -= 2;
8: } while keepGoing;
9: System.out.println(result);
~~~
A. 7
B. 9
C. 10
D. 11
E. 15
F.The code will not compile because of line 6.
G.The code does not compile for a different reason.

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

10.Which statements about the following code snippet are correct? (Choose all that apply.)
~~~
for(var penguin : new int[2])
System.out.println(penguin);
var ostrich = new Character[3];
for(var emu : ostrich)
System.out.println(emu);
List parrots = new ArrayList();
for(var macaw : parrots)
System.out.println(macaw);
~~~
A. The data type of penguin is Integer.
B. The data type of penguin is int.
C. The data type of emu is undefined.
D. The data type of emu is Character.
E. The data type of macaw is undefined.
F.The data type of macaw is Object.
G.None of the above, as the code does not compile

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

11.What is the result of the following code snippet?
~~~
final char a = ‘A’, e = ‘E’;
char grade = ‘B’;
switch (grade) {
default:
case a:
case ‘B’: ‘C’: System.out.print(“great “);
case ‘D’: System.out.print(“good “); break;
case e:
case ‘F’: System.out.print(“not good “);
}
~~~
A. great
B. great good
C. good
D. not good
E. The code does not compile because the data type of one or more case statements does not match the data type of the switch variable.
F.None of the above

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

12.Given the following array, which code snippets print the elements in reverse order from how they are declared? (Choose all that apply.)
char[] wolf = {‘W’, ‘e’, ‘b’, ‘b’, ‘y’};
7.
int q = wolf.length;
for( ; ; ) {
System.out.print(wolf[–q]);
if(q==0) break;
13. }
7.
for(int m=wolf.length-1; m>=0; –m)
14. System.out.print(wolf[m]);
7.
for(int z=0; z<wolf.length; z++)
15. System.out.print(wolf[wolf.length-z]);
7.
int x = wolf.length-1;
for(int j=0; x>=0 && j==0; x–)
16. System.out.print(wolf[x]);
7.
final int r = wolf.length;
for(int w = r-1; r>-1; w = r-1)
17. System.out.print(wolf[w]);
7.
for(int i=wolf.length; i>0; –i)
18. System.out.print(wolf[i]);
7. None of the above

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

13.What distinct numbers are printed when the following method is executed? (Choose all that apply.)
~~~
private void countAttendees() {
int participants = 4, animals = 2, performers = -1;
while((participants = participants+1) < 10) {}
do {} while (animals++ <= 1);
for( ; performers<2; performers+=2) {}
System.out.println(participants);
System.out.println(animals);
System.out.println(performers);
}
~~~
A. 6
B. 3
C. 4
D. 5
E. 10
F.9
G.The code does not compile.
H.None of the above

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

14.What is the output of the following code snippet?
~~~
2: double iguana = 0;
3: do {
4: int snake = 1;
5: System.out.print(snake++ + “ “);
6: iguana–;
7: } while (snake <= 5);
8: System.out.println(iguana);
~~~

A. 1 2 3 4 -4.0
B. 1 2 3 4 -5.0
C. 1 2 3 4 5 -4.0
D. 0 1 2 3 4 5 -5.0
E. The code does not compile.
F.The code compiles but produces an infinite loop at runtime.
G.None of the above

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

15.Which statements, when inserted into the following blanks, allow the code to compile and run without entering an infinite loop? (Choose all that apply.)
~~~
4: int height = 1;
5: L1: while(height++ <10) {
6: long humidity = 12;
7: L2: do {
8: if(humidity– % 12 == 0) ________________;
9: int temperature = 30;
10: L3: for( ; ; ) {
11: temperature++;
12: if(temperature>50) _________________;
13: }
14: } while (humidity > 4);
15: }
~~~
A. break L2 on line 8; continue L2 on line 12
B. continue on line 8; continue on line 12
C. break L3 on line 8; break L1 on line 12
D. continue L2 on line 8; continue L3 on line 12
E. continue L2 on line 8; continue L2 on line 12
F.None of the above, as the code contains a compiler error.

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

16.What is the output of the following code snippet? (Choose all that apply.)
~~~
2: var tailFeathers = 3;
3: final var one = 1;
4: switch (tailFeathers) {
5: case one: System.out.print(3 + “ “);
6: default: case 3: System.out.print(5 + “ “);
7: }
8: while (tailFeathers > 1) {
9: System.out.print(–tailFeathers + “ “); }
~~~
A. 3
B. 5 1
C. 5 2
D. 3 5 1
E. 5 2 1
F.The code will not compile because of lines 3–5.
G.The code will not compile because of line 6.

A
17
Q

17.What is the output of the following code snippet?
~~~
15: int penguin = 50, turtle = 75;
16: boolean older = penguin >= turtle;
17: if (older = true) System.out.println(“Success”);
18: else System.out.println(“Failure”);
19: else if(penguin != 50) System.out.println(“Other”);
~~~
A. Success
B. Failure
C. Other
D. The code will not compile because of line 17.
E. The code compiles but throws an exception at runtime.
F.None of the above

A
18
Q

18.Which of the following are possible data types for olivia that would allow the code to compile? (Choose all that apply.)
~~~
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

19
Q

19.What is the output of the following code snippet?
~~~
6: String instrument = “violin”;
7: final String CELLO = “cello”;
8: String viola = “viola”;
9: int p = -1;
10: switch(instrument) {
11: case “bass” : break;
12: case CELLO : p++;
13: default: p++;
14: case “VIOLIN”: p++;
15: case “viola” : ++p; break;
16: }
17: System.out.print(p);
~~~

A. -1
B. 0
C. 1
D. 2
E. 3
F.The code does not compile.

A
20
Q

20.What is the output of the following code snippet? (Choose all that apply.)
~~~
9: int w = 0, r = 1;
10: String name = “”;
11: while(w < 2) {
12: name += “A”;
13: do {
14: name += “B”;
15: if(name.length()>0) name += “C”;
16: else break;
17: } while (r <=1);
18: r++; w++; }
19: System.out.println(name);
~~~
A. ABC
B. ABCABC
C. ABCABCABC
D. Line 15 contains a compilation error.
E. Line 18 contains a compilation error.
F.The code compiles but never terminates at runtime.
G.The code compiles but throws a NullPointerException at
runtime.

A
21
Q
A