logo

APSU Notes


Quiz 6

Question 1

Which of the following examples does not result in an infinite loop?

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

Question 2

The body of a loop can not contain another loop statement.

False

Question 3

Given the following for loop:

1
2
for (count = 0; count < 4; count++)
    System.out.println(count);

Which of the following is an equivalent while loop?

1
2
3
4
5
6
count = 0;
while (count < 4)
{
    Syystem.out.println(count);
    count++;
}

Question 4

A variable can be declared in the initialization part of a for statement.

True

Question 5

Which of the following kinds of loops is most useful for repeating an action for each value in an enumeration.

A for-each loop.

Question 6

Consider the following loop that adds up a sequence of numbers:

1
2
3
4
5
for (int count = 1; count < n; count++)
{
    next = keyboard.nextInt();
    sum = sum + next;
}

What should the initial value of sum be?

0

Question 7

When is it a good idea to use a count-controlled loop?

When the number of times the loop should repeat is known ahead of time.

Question 8

A boolean variable can not be used the controlling boolean expression in a loop.

False

Question 9

What does the following code display?

1
2
3
4
5
6
for (int i = 1; i <= 5; i++)
{
    if (i == 4)
        break;
    System.out.println("boing");
}
1
2
3
boing
boing
boing

Question 10

What does the following code display?

1
2
3
4
5
6
for (int i = 1; i <= 5; i++)
{
    if (i == 4)
        continue;
    System.out.println("boing");
}
1
2
3
4
boing
boing
boing
boing

Question 11

A loop might terminate for some input data values, but repeat infinitely for other values.

A: True

Question 12

Why is it a good idea to use DEBUG flag when tracing variables?

The DEBUG flag allows the programmer to easily turn the tracing messages on and off.