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++; }
The body of a loop can not contain another loop statement.
False
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++; }
A variable can be declared in the initialization part of a for
statement.
True
Which of the following kinds of loops is most useful for repeating an action for each value in an enumeration.
A
for-each
loop.
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
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.
A boolean variable can not be used the controlling boolean expression in a loop.
False
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
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
A loop might terminate for some input data values, but repeat infinitely for other values.
A: True
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.