Chapter 4 Review Questions Flashcards
- 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,B,C,E,F
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.
B
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>
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.
C, E
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,B,D,E
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
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.
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.
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.
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
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
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
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
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
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.