A program uses a while loop whose condition is false before the first test. What happens to the loop body?
4 Loops and Repetition Online Quiz Questions
Use this free practice quiz with 20 questions to review 4 Loops and Repetition, test your knowledge, and prepare for your next test or exam.
A menu must be displayed once before the program checks whether the user's choice is valid. Which loop form is most appropriate?
- A
Use a
forloop because all menu choices are numeric. - B
Use a
whileloop because it always executes once. - C
Use a
do...whileloop because the menu must be displayed before validation. - D
Use a nested loop because the menu may repeat.
A program must print every value in the array colors, but it does not need the index. Which loop is the clearest choice?
- A
for...of - B
for...in - C
An indexed
forloop is always required. - D
A
whileloop with no condition
Consider nested loops where row runs from 1 through 3 and column runs from 1 through 4. Select all statements that are correct.
- A
The inner loop body runs 12 times.
- B
The outer loop body runs 12 times.
- C
The outer loop controls the rows.
- D
The inner loop runs only once overall.
Select all statements that correctly describe break and continue in loops.
- A
breakcan stop the nearest enclosing loop. - B
continueexits every enclosing loop. - C
continueskips the remainder of the current iteration. - D
breakonly skips the current iteration and then always continues.
True or false: When an accumulator is used to calculate a sum, initializing it to 0 is generally appropriate.
- A
True
- B
False
True or false: This loop terminates after printing five values: let count = 0; while (count < 5) { console.log(count); }
- A
True
- B
False
In the array-search pattern described in the material, what numeric sentinel represents “not found” before or when a match is absent?
For let total = 0; for (let i = 1; i <= 4; i++) { total += i; }, what is the value of total after the loop finishes?
Complete the three-part loop design: establishes the starting state, determines whether another iteration occurs, and moves the loop toward termination.
A do...while loop checks its condition executing the body.
Explain how to choose among for, for...of, while, and do...while loops. Include at least two practices that help make the chosen loop terminate correctly and handle boundary cases.
What does for (let i = 0; i < values.length; i++) do when used to process an array named values?
- A
It skips the first element.
- B
It visits every valid array index exactly once.
- C
It visits one index beyond the end.
- D
It runs only when the array has at least two elements.
Which loop form best fits this task: print the integers from 0 through 7 exactly once, with no input and no early exit?
- A
Use a
whileloop because all repetition should be condition-controlled. - B
Use a
forloop because the loop has a known range of eight values. - C
Use a
do...whileloop because its body must execute at least once. - D
Use a
for...inloop because the values are numeric.
True or false: If a JavaScript while loop's condition is false before the first test, its body executes zero times.
- A
True
- B
False
A program must ask for a menu choice at least once and repeat the prompt only when the choice is outside the range 1 through 3. Which loop is the best fit?
- A
A
forloop because it always runs once before testing its condition. - B
A
whileloop because it validates input before requesting it. - C
A
do...whileloop because the prompt occurs before validation. - D
A
for...inloop because it iterates over the possible choices.
What is the final value of number after this JavaScript loop finishes? let number = 1; while (number < 10) { number *= 2; }
In a loop that examines the integers from 1 through 20 and increments evenCount only when the current integer is even, which description is correct?
- A
evenCountis a counter, whilenumberis the loop-control variable. - B
numberis an accumulator, whileevenCountis the loop condition. - C
Both variables are accumulators because both change during the loop.
- D
evenCountis the loop-control variable because it determines when the loop ends.
In the search pattern that initializes position to -1, what term describes the special value representing “not found”?
How many total times does the inner loop body execute in this code? for (let row = 1; row <= 3; row++) { for (let column = 1; column <= 4; column++) { console.log(row, column); } }
- A
4
- B
7
- C
10
- D
12