In Java, what is normally true of the variable declared in for (int i = 0; i < 3; i++)?
Iteration and Algorithmic Patterns Online Quiz Questions
Use this free practice quiz with 30 questions to review Iteration and Algorithmic Patterns, test your knowledge, and prepare for your next test or exam.
Select all statements that are true about Java while and do-while loops.
- A
A do-while loop executes its body at least once.
- B
A while loop always executes its body at least once.
- C
A while loop can execute its body zero times.
- D
A do-while loop tests its condition before its body.
Select all correct statements about these nested-loop patterns.
- A
A 3-by-4 nested loop executes its inner body 7 times.
- B
A 3-by-4 nested loop executes its inner body 12 times.
- C
A triangular loop with row values 1 through 4 executes its inner body 16 times.
- D
A triangular loop with row values 1 through 4 executes its inner body 10 times.
True or false: In a sentinel-controlled loop, the sentinel should normally be tested before it is processed.
- A
True
- B
False
Which Java loop keyword names a pretest loop that may execute its body zero times? Enter the keyword in lowercase.
Complete the statement with the final valid index expression for a string of length n: the valid indexes range from 0 through .
In the digit-reversal algorithm, complete both blanks: number % extracts the last decimal digit, and reversed * + digit appends that digit to the reversed number.
Explain how to implement a correct linear search for a target in an array. Your answer must describe the stopping conditions and explain how an optional break should be used.
What is printed by this code? int number = 10; do { System.out.println(number); number++; } while (number < 10);
- A
The body executes zero times because the condition is false.
- B
The body executes twice because the condition is tested after the body.
- C
The body executes once because the condition is tested after the body.
- D
The program fails to compile because the condition is initially false.
True or false: In for (int i = 0; i < 3; i++) { System.out.println(i); }, the variable i can normally be used in a statement after the loop.
- A
True
- B
False
What is the final value of result after this code executes? int result = 0; for (int i = 2; i <= 6; i += 2) { result += i; }
- A
6
- B
12
- C
14
- D
18
True or false: In a sentinel-controlled loop that adds input values to a sum, the sentinel should normally be checked before it is added.
- A
True
- B
False
How many times does the counter become incremented in this code? String word = "banana"; int count = 0; for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == 'a') { count++; } } Enter the whole-number count.
An iterative algorithm typically contains initialization, condition, body, and .
Select all statements that correctly describe nested loops.
- A
The inner loop completes all of its iterations before the outer loop advances.
- B
The inner-loop variable keeps its final value from the previous outer iteration.
- C
The inner-loop variable is reinitialized when the outer loop starts a new iteration.
- D
If the inner bound depends on the outer variable, the total may be triangular rather than a simple product.
What algorithm examines array elements one at a time until it finds a target or reaches the end? Enter the algorithm name.
True or false: In while (x < 5); { x++; }, the statement x++ executes as the body of the while loop.
- A
True
- B
False
What is the informal run-time growth of this algorithm as n increases? for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { /* work */ } }
- A
Constant growth
- B
Linear growth
- C
Logarithmic growth
- D
Quadratic growth
What whole number is printed by this code? int number = 472; int reversed = 0; while (number > 0) { int digit = number % 10; reversed = reversed * 10 + digit; number /= 10; } System.out.println(reversed);
In the pattern that initializes a result with values[0] and replaces it whenever a later value is larger, which quantity is being computed? Enter the algorithm term.
True or false: Initializing a maximum-finding variable to 0 is always correct, regardless of the values in the array.
- A
True
- B
False
What happens when this loop processes the target value? boolean found = false; for (int value : values) { if (value == target) { found = true; break; } }
- A
The loop exits before checking the target, so
foundremains false. - B
The loop sets
foundto true and then exits immediately. - C
The loop skips the matching value and continues through all later values.
- D
The loop restarts from the first array element after the match.
What is printed by this Java code?
int x = 5;
while (x < 5) {
System.out.println(x);
x++;
}
- A
It prints 5 once.
- B
It prints nothing.
- C
It prints the numbers 1 through 5.
- D
It enters an infinite loop.
What is the final value of result?
int result = 0;
for (int i = 2; i <= 6; i += 2) {
result += i;
}
- A
6
- B
8
- C
12
- D
14
What does this code print?
int number = 10;
do {
System.out.println(number);
number++;
} while (number < 10);
- A
It prints nothing.
- B
It prints 10 repeatedly.
- C
It prints 11 once.
- D
It prints 10 once.
What is the final value of vowels?
String text = "Computer";
int vowels = 0;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
vowels++;
}
}
- A
3
- B
2
- C
4
- D
8
How many times does System.out.print("* ") execute?
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 4; column++) {
System.out.print("* ");
}
}
- A
7
- B
12
- C
16
- D
9
Consider this algorithm as n increases:
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
// work
}
}
What is the best informal description of its run-time growth?
- A
Constant growth
- B
Linear growth
- C
Quadratic growth
- D
Exponential growth
What integer is printed? Enter the exact integer; no tolerance applies.
int number = 472;
int reversed = 0;
while (number > 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
System.out.println(reversed);
A method must print every integer from 1 through 20, including both endpoints, exactly once. Which Java loop accomplishes this?
- A
for (int i = 0; i < 20; i++) { System.out.println(i); }
- B
for (int i = 1; i <= 20; i++) { System.out.println(i); }
- C
for (int i = 1; i < 20; i++) { System.out.println(i); }
- D
for (int i = 0; i <= 20; i++) { System.out.println(i); }