Loops and Arrays: Fundamentals of Iteration Quiz

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.

  1. Identifying a for loop structure

    Which of the following options shows a correct structure for a basic for loop that counts from 1 to 5?

    1. for int i = 1; i u003E 5; i--
    2. for (i = 1; i u003C= 5;)
    3. foreach (int i = 1; i u003C= 5; i++)
    4. for (int i = 1; i u003C= 5; i++)

    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.

  2. Choosing the right loop type

    If you want to repeat a block of code an exact number of times, which loop is most suitable?

    1. while loop
    2. do-when loop
    3. case loop
    4. for loop

    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.

  3. Loop control with arrays

    Given an array of five elements, which statement best iterates over each element in order?

    1. for (int i = 1; i u003C= 5; i++)
    2. for (int i = 0; i u003C 5; i++)
    3. while (i u003C= 5)
    4. if (i u003C array.length)

    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.

  4. Preventing infinite loops

    What can commonly cause an infinite loop when using a while loop?

    1. Declaring the variable outside the loop
    2. Using too many variables in the loop
    3. The loop has no print statement
    4. The loop condition never becomes false

    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.

  5. Accessing array elements with loops

    Which loop variable value should you use to access the third element in a zero-based array?

    1. 1
    2. 3
    3. 2
    4. 0

    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.

  6. Printing all elements

    To print each number in an array called numList, which loop is most suitable?

    1. case numList:
    2. if (numList has values)
    3. while (i == numList.length)
    4. for (int i = 0; i u003C numList.length; i++)

    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.

  7. Exiting early from a loop

    Which statement can be used to stop a loop before it finishes its usual number of iterations?

    1. continue
    2. exit if
    3. break
    4. next

    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.

  8. Using while loops with user input

    Which use case is most appropriate for a while loop instead of a for loop?

    1. Running code a known number of times
    2. Printing elements from a fixed-length array
    3. Repeating input requests until the user enters a valid value
    4. Counting from 1 to 10

    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.

  9. Updating values in an array with a loop

    When using a for loop to double each value in an integer array called data, which operation should occur inside the loop?

    1. print(i);
    2. data = data * 2;
    3. data[i] = data[i] * 2;
    4. int sum = 0;

    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.

  10. Understanding continue statement

    What does the 'continue' statement do in a loop?

    1. Ends the loop entirely
    2. Pauses the loop until a key is pressed
    3. Skips the rest of the current loop iteration and continues with the next
    4. Repeats the current statement indefinitely

    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.