Explore core concepts of looping constructs such as for and while loops, and learn how to iterate over arrays or collections using control structures. This quiz focuses on helping you understand essential programming fundamentals in iteration and array traversal.
Which of the following options shows a correct structure for a basic for loop that counts from 1 to 5?
Explanation: The correct syntax for a for loop in this context is 'for (int i = 1; i u003C= 5; i++)' which initializes the counter, checks the condition, and increments the counter. The second option has a decreasing counter and incorrect logic, while the third is missing part of the increment expression. The fourth option uses an incorrect keyword and structure for such usage.
If you want to repeat a block of code an exact number of times, which loop is most suitable?
Explanation: A for loop is designed for use when you know in advance how many times you want to repeat a block of code. A while loop can also repeat code but is best for unknown counts. Do-when and case loops are either incorrect constructs or unrelated to iterative looping.
Given an array of five elements, which statement best iterates over each element in order?
Explanation: The correct answer uses 'for (int i = 0; i u003C 5; i++)' since array indices start from zero. The second option is a while loop but lacks initialization, while the third is just a conditional, not a loop. The fourth option would skip the first element due to its index starting at one.
What can commonly cause an infinite loop when using a while loop?
Explanation: An infinite loop happens if the loop condition is always true and never becomes false. A missing print statement does not cause a loop to be infinite. Using many variables or declaring variables outside the loop does not necessarily affect whether the loop will terminate.
Which loop variable value should you use to access the third element in a zero-based array?
Explanation: In zero-based arrays, the first element is at index 0, so the third element is at index 2. Index 3 would access the fourth element, 1 accesses the second, and 0 accesses the first element.
To print each number in an array called numList, which loop is most suitable?
Explanation: A for loop that starts at index 0 and continues until it reaches the array's length is the most reliable method. The If statement only checks for values and doesn't loop, the case statement is not used for iteration, and the last option would never start properly with i == numList.length.
Which statement can be used to stop a loop before it finishes its usual number of iterations?
Explanation: The 'break' statement exits the loop immediately, while 'continue' skips to the next iteration without stopping the loop. 'Next' and 'exit if' are either not valid loop controls or are not standard in common languages for immediate loop termination.
Which use case is most appropriate for a while loop instead of a for loop?
Explanation: A while loop is ideal when you don't know in advance how many times you'll need to repeat, such as waiting for valid user input. Counting, printing fixed arrays, and running code a known number of times are all better suited to for loops.
When using a for loop to double each value in an integer array called data, which operation should occur inside the loop?
Explanation: This statement multiplies each element by 2 and updates it in place. Declaring sum or printing i does not update the elements, while multiplying the entire array as in 'data = data * 2' is not valid for array operations.
What does the 'continue' statement do in a loop?
Explanation: 'Continue' tells the loop to skip the remaining code in the current iteration and proceed to the next one. It does not end the loop like 'break', repeat statements, or pause for user input.