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
In the case of the sentinel-controlled while loop, the first item is read before the while loop is entered.
True
2
A loop is a control structure that causes certain statements to be executed over and over until certain conditions are met.
True
3
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);
False
4
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);
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
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
6
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
7
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
8
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);
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
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
10
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
11
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
12
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
13
If the while expression becomes false in the middle of the while loop body, the loop terminates immediately.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
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
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
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
17
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
18
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');
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
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
20
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();
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
The above code is an example of a(n) ____ loop.
A) flag-controlled
C) EOF-controlled
B) counter-controlled
D) sentinel-controlled
A) flag-controlled
C) EOF-controlled
B) counter-controlled
D) sentinel-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
The above code is an example of a(n) ____ while loop.
A) flag-controlled
C) EOF-controlled
B) counter-controlled
D) sentinel-controlled
A) flag-controlled
C) EOF-controlled
B) counter-controlled
D) sentinel-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following is the logical expression in the for loop above?
A) i = 0;
B) i <= 10;
C) i++
D) System.out.println("*");
A) i = 0;
B) i <= 10;
C) i++
D) System.out.println("*");
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
C) for
B) while
D) loop
A) do
C) for
B) while
D) loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
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
C) 179
B) 58
D) 226
A) 18
C) 179
B) 58
D) 226
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
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
27
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
28
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
29
What is the output of the following Java code? int num = 15; while (num > 0) num = num - 3; System.out.println(num);
A) 0
C) 12
B) 3
D) 15
A) 0
C) 12
B) 3
D) 15
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
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
C) 45
B) 28
D) 60
A) 17
C) 45
B) 28
D) 60
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following is the update expression in the for loop above?
A) i = 0;
B) i <= 10;
C) i++;
D) System.out.println("*");
A) i = 0;
B) i <= 10;
C) i++;
D) System.out.println("*");
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
What is the value of counter after the following statements execute? counter = 1; while (counter < 30) counter = 2 * counter;
A) 16
C) 64
B) 32
D) 53
A) 16
C) 64
B) 32
D) 53
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
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
34
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
35
What is the tenth Fibonacci number in the sequence above?
A) 21
C) 55
B) 34
D) 89
A) 21
C) 55
B) 34
D) 89
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
The above code is an example of a(n) ____ loop.
A) sentinel-controlled
C) EOF-controlled
B) flag-controlled
D) counter-controlled
A) sentinel-controlled
C) EOF-controlled
B) flag-controlled
D) counter-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
The above code is an example of a(n) ____ while loop.
A) flag-controlled
C) EOF-controlled
B) counter-controlled
D) sentinel-controlled
A) flag-controlled
C) EOF-controlled
B) counter-controlled
D) sentinel-controlled
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
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
39
Which of the following is the initial expression in the for loop above?
A) i = 0;
B) i <= 10;
C) i++
D) System.out.println("*");
A) i = 0;
B) i <= 10;
C) i++
D) System.out.println("*");
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 < 2; count++) {num = num + 3; System.out.print(num + " ");} System.out.println();
A) 5
C) 2 5 8
B) 5 8
D) 5 8 11
A) 5
C) 2 5 8
B) 5 8
D) 5 8 11
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
Which executes first in a do...while loop?
A) statement
C) initial statement
B) logical expression
D) update expression
A) statement
C) initial statement
B) logical expression
D) update expression
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
Which of the following loops is guaranteed to execute at least once?
A) counter-controlled while loop
C) do...while loop
B) for loop
D) sentinel-controlled while loop
A) counter-controlled while loop
C) do...while loop
B) for loop
D) sentinel-controlled while loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
Where is the continue statement NOT usually used?
A) while loops
C) switch structures
B) for loops
D) do...while loops
A) while loops
C) switch structures
B) for loops
D) do...while loops
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
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
45
Which executes immediately after a continue statement in a for loop?
A) initial statement
C) loop condition
B) update statement
D) the body of the loop
A) initial statement
C) loop condition
B) update statement
D) the body of the loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
If y = 0, how many times would the loop above execute?
A) 1
C) 3
B) 2
D) 4
A) 1
C) 3
B) 2
D) 4
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
Which of the following does not have an entry condition?
A) EOF-controlled while loop
C) do...while loop
B) sentinel-controlled while loop
D) for loop
A) EOF-controlled while loop
C) do...while loop
B) sentinel-controlled while loop
D) for loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
What is the final value of x in the code above?
A) 0
C) 9
B) 3
D) 27
A) 0
C) 9
B) 3
D) 27
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
How many times does the statement above execute?
A) none
C) twice
B) once
D) three times
A) none
C) twice
B) once
D) three times
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
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