logo

APSU Notes


Quiz 5

Question 1

Given the following Java code:

1
2
3
4
5
boolean flag = number < 12
if (!flag)
    System.out.println("walrus");
else
    System.out.println("narwhal");

What is printed to the screen if the value of number is 18?

walrus

Question 2

What is the correct fully parenthesized version of the following Java expression?

1
25 + 3 * 2 < 50 || 88 == 3 - 2 && 21 < 99

((25 + (3 * 2)) < 50 || ((88 == (3 - 2)) && (21 < 99))

Question 3

Assuming you have the following declaration:

1
Scanner keyboard;

Which the correct way to read a boolean value?

boolean flag = keyboard.nextBoolean();

Question 4

What output is generated by the following code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int value = 2;
switch(value)
{
    case 1:
        System.out.println("Kamchatka");
        break;
    case 2:
        System.out.println("Yakutsk");
    case 3:
        System.out.println("Irkutsk");
        break;
    default:
        System.out.println("Siberia");
}
1
2
Yakutsk
Irkutsk

Question 5

Given the following java code, what is displayed to the screen?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char letter = 'a';
switch(letter)
{
    case 'a':
    case 'b':
        System.out.println("Alaska");
        break;
    case 'c':
    case 'd':
        System.out.println("Northwest Territory");
        break;
    default:
        System.out.println("Greenland");
}

Alaska

Question 6

Which of the following best describes how a while loop works?

The loop body is repeated until the controlling boolean expression is false.

Question 7

If the body of a while loop needs to contains multiple statements, those statements must be surrounded by which of the following symbols?

{ }

Question 8

What is the difference between a while loop and a do-while loop?

A whileloop evaluates its controlling boolean expression before the body is executed. A do-whileloop evaluates its controlling boolean expression after the body is executed.

Question 9

What does the following code display to the screen?

1
2
3
4
5
6
int number = 0;
while (number < 5)
{
    System.out.println(number);
    number++;
}
1
2
3
4
5
0
1
2
3
4

Question 10

What does the following code display to the screen?

1
2
3
4
5
6
int number = 10;
do
{
    System.out.println(number);
    number--;
} while (number < 5);

10