Deck 5: Control Structures II: Repetition
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
Play
Full screen (f)
Deck 5: Control Structures II: Repetition
1
A loop is a control structure that causes certain statements to be executed over and over until certain conditions are met.
True
2
If the while expression becomes false in the middle of the while loop body, the loop terminates immediately.
False
3
The output of the Java code, assuming that all variables are properly declared, is 32.num = 10;
while (num<= 32);
num = num + 5;
System.out.println(num);
while (num<= 32);
num = num + 5;
System.out.println(num);
False
4
A while loop is a post-test loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
Control variables are automatically initialized in a loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
Assume that all variables in the following code are properly declared and that the input is 3 7 4 -1. The output is 13.
num = console.nextInt();
sum = num;
while (num != -1)
{
num = console.nextInt();
sum = sum + num;
}System.out.println(sum);
num = console.nextInt();
sum = num;
while (num != -1)
{
num = console.nextInt();
sum = sum + num;
}System.out.println(sum);
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
In the for statement, if the logical expression is omitted, it is assumed to be false.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
To read data from a file of unspecified length, an EOF-controlled while loop is a better choice than a counter-controlled while loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
The following for loop executes 21 times. (Assume all variables are properly declared.)for (i = 1; i <= 20; i = i + 1): System.out.println(i);
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
Like a do...while loop, the body of a while loop executes at least once.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
In the case of an infinite while loop, the while expression (that is, the loop condition) is always true.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
The output of the following Java code is: Stoor.int count = 5;
System.out.print("Sto");do
{
System.out.print('o');
count--;
}
while (count >= 5);System.out.println('r');
System.out.print("Sto");do
{
System.out.print('o');
count--;
}
while (count >= 5);System.out.println('r');
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
The control statements in the for loop include the initial expression, logical expression, and update expression.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
A counter-controlled loop is used when the exact number of data entries is known.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
The loop condition of a while loop is reevaluated before every iteration of the loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
EOF-controlled while loop is another name for sentinel-controlled while loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
A loop that continues to execute endlessly is called an endless loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
The output of the Java code, assuming that all variables are properly declared, is: 2 3 4 5 6.n = 2;
while (n >= 6)
{
System.out.print(n + " ");
n++;
}
System.out.println();
while (n >= 6)
{
System.out.print(n + " ");
n++;
}
System.out.println();
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
A syntax error will result if the control statements of a for loop are omitted.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
boolean found = false;
Int num;
Int square;while (!found)
{
Num = console.nextInt();
Square = num * num;
If (square > 40)
Found = true;
}The above code is an example of a(n) ____ loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
Int num;
Int square;while (!found)
{
Num = console.nextInt();
Square = num * num;
If (square > 40)
Found = true;
}The above code is an example of a(n) ____ loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
static final int EndVal = -1;
Int double;
Int num = console.nextInt();while (num != EndVal)
{
Double = num * 2;
System.out.println(double);
Num = console.nextInt();
}The above code is an example of a(n) ____ while loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
Int double;
Int num = console.nextInt();while (num != EndVal)
{
Double = num * 2;
System.out.println(double);
Num = console.nextInt();
}The above code is an example of a(n) ____ while loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
1, 1, 2, 3, 5, 8, 13, 21, ...What is the tenth Fibonacci number in the sequence above?
A) 21
B) 34
C) 55
D) 89
A) 21
B) 34
C) 55
D) 89
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
Which of the following is NOT a reserved word in Java?
A) do
B) while
C) for
D) loop
A) do
B) while
C) for
D) loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following is true about a while loop?
A) The body of the loop is executed at least once.
B) The logical expression controlling the loop is evaluated before the loop is entered and after the loop exists.
C) The body of the loop may not execute at all.
D) It is a post-test loop
A) The body of the loop is executed at least once.
B) The logical expression controlling the loop is evaluated before the loop is entered and after the loop exists.
C) The body of the loop may not execute at all.
D) It is a post-test loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
Because a do...while loop is a post-test loop, the body of the loop may not execute at all.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
If a continue statement is placed in a do...while structure, the loop-continue test is evaluated immediately after the continue statement.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
int sum = 0;int limit = console.nextInt();
Int counter = 0;while (counter <= limit){
Entry = console.nextInt();
Sum = sum + entry;
Counter++;
}The above code is an example of a(n) ____ while loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
Int counter = 0;while (counter <= limit){
Entry = console.nextInt();
Sum = sum + entry;
Counter++;
}The above code is an example of a(n) ____ while loop.
A) flag-controlled
B) counter-controlled
C) EOF-controlled
D) sentinel-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
int i;for (i = 0; i <= 10; i++)
System.out.println("*");
System.out.println("!");Which of the following is the initial expression in the for loop above?
A) i = 0;
B) i
C) i++
D) System.out.println("*");
System.out.println("*");
System.out.println("!");Which of the following is the initial expression in the for loop above?
A) i = 0;
B) i
C) i++
D) System.out.println("*");
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
int i;for (i = 0; i <= 10; i++)
System.out.println("*");
System.out.println("!");Which of the following is the update expression in the for loop above?
A) i = 0;
B) i
C) i++
D) System.out.println("*");
System.out.println("*");
System.out.println("!");Which of the following is the update expression in the for loop above?
A) i = 0;
B) i
C) i++
D) System.out.println("*");
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
After a break statement executes, the program continues to execute with the first statement after the structure.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
What is the output of the following Java code?int num = 15;
While (num > 0)
Num = num - 3;
System.out.println(num);
A) 0
B) 3
C) 12
D) 15
While (num > 0)
Num = num - 3;
System.out.println(num);
A) 0
B) 3
C) 12
D) 15
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
The do...while loop has an exit condition but no entry condition.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
A break statement is legal in a while loop, but not in a for loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
Suppose sum and num are int variables, and the input is
20 25 10 18 -1 What is the output of the following code? (Assume that console is a Scanner object initialized to the standard input device.)sum = 0;
Num = console.nextInt();
While (num != -1)
{
If (num >= 20)
Sum = sum + num;
Else
Sum = sum - num;
Num = console.nextInt();
}
System.out.println(sum);
A) 17
B) 28
C) 45
D) 60
20 25 10 18 -1 What is the output of the following code? (Assume that console is a Scanner object initialized to the standard input device.)sum = 0;
Num = console.nextInt();
While (num != -1)
{
If (num >= 20)
Sum = sum + num;
Else
Sum = sum - num;
Num = console.nextInt();
}
System.out.println(sum);
A) 17
B) 28
C) 45
D) 60
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
ch = inFile.next().charAt();while (inFile.hasNext())
{
System.out.println(ch);
Ch = inFile.next().charAt();
}The above code is an example of a(n) ____ loop.
A) sentinel-controlled
B) flag-controlled
C) EOF-controlled
D) counter-controlled
{
System.out.println(ch);
Ch = inFile.next().charAt();
}The above code is an example of a(n) ____ loop.
A) sentinel-controlled
B) flag-controlled
C) EOF-controlled
D) counter-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
What is the value of counter after the following statements execute?counter = 1;
While (counter<30)
Counter = 2 * counter;
A) 16
B) 32
C) 64
D) 53
While (counter<30)
Counter = 2 * counter;
A) 16
B) 32
C) 64
D) 53
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
int i;for (i = 0; i <= 10; i++)
System.out.println("*");
System.out.println("!");Which of the following is the logical expression in the for loop above?
A) i = 0;
B) i
C) i++
D) System.out.println("*");
System.out.println("*");
System.out.println("!");Which of the following is the logical expression in the for loop above?
A) i = 0;
B) i
C) i++
D) System.out.println("*");
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
What is value of x after the following code executes?int x = 0;
Int i;for (i = 0; i < 5; i++)
X = 3 * x + i;
A) 18
B) 58
C) 179
D) 226
Int i;for (i = 0; i < 5; i++)
X = 3 * x + i;
A) 18
B) 58
C) 179
D) 226
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
What is the output of the following code?int count;
Int num = 2;for (count = 1; count " ");
}
System.out.println();
A) 5
B) 5 8
C) 2 5 8
D) 5 8 11
Int num = 2;for (count = 1; count " ");
}
System.out.println();
A) 5
B) 5 8
C) 2 5 8
D) 5 8 11
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
int x = 27;
Int y = 10;do
X = x / 3;
While (x >= y);What is the final value of x in the code above?
A) 0
B) 3
C) 9
D) 27
Int y = 10;do
X = x / 3;
While (x >= y);What is the final value of x in the code above?
A) 0
B) 3
C) 9
D) 27
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
Which executes first in a do...while loop?
A) statement
B) logical expression
C) initial statement
D) update expression
A) statement
B) logical expression
C) initial statement
D) update expression
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
Which of the following is true about a do...while loop?
A) The body of the loop is executed at least once.
B) The logical expression controlling the loop is evaluated before the loop is entered.
C) The body of the loop may not execute at all.
D) It is a pretest loop.
A) The body of the loop is executed at least once.
B) The logical expression controlling the loop is evaluated before the loop is entered.
C) The body of the loop may not execute at all.
D) It is a pretest loop.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following is not a function of the break statement?
A) To exit early from a loop
B) To skip the remainder of a switch structure
C) To eliminate the use of certain boolean variables in a loop
D) To ignore certain values for variables and continue with the next iteration of a loop
A) To exit early from a loop
B) To skip the remainder of a switch structure
C) To eliminate the use of certain boolean variables in a loop
D) To ignore certain values for variables and continue with the next iteration of a loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
Which of the following loops is guaranteed to execute at least once?
A) counter-controlled while loop
B) for loop
C) do...while loop
D) sentinel-controlled while loop
A) counter-controlled while loop
B) for loop
C) do...while loop
D) sentinel-controlled while loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
int x = 27;
Int y = 10;do
X = x / 3;
While (x >= y);How many times does the statement above execute?
A) none
B) once
C) twice
D) three times
Int y = 10;do
X = x / 3;
While (x >= y);How many times does the statement above execute?
A) none
B) once
C) twice
D) three times
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
Which executes immediately after a continue statement in a for loop?
A) initial statement
B) update statement
C) loop condition
D) the body of the loop
A) initial statement
B) update statement
C) loop condition
D) the body of the loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
Which of the following does not have an entry condition?
A) EOF-controlled while loop
B) sentinel-controlled while loop
C) do...while loop
D) for loop
A) EOF-controlled while loop
B) sentinel-controlled while loop
C) do...while loop
D) for loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
Where is the continue statement NOT usually used?
A) while loops
B) for loops
C) switch structures
D) do...while loops
A) while loops
B) for loops
C) switch structures
D) do...while loops
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
int x = 27;
Int y = 10;do
X = x / 3;
While (x >= y);If y = 0, how many times would the loop above execute?
A) 1
B) 2
C) 3
D) 4
Int y = 10;do
X = x / 3;
While (x >= y);If y = 0, how many times would the loop above execute?
A) 1
B) 2
C) 3
D) 4
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck