Array Length and Loop Traversal Fundamentals Quiz Quiz

  1. Loop Boundary Basics

    Given an array nums with some length, which loop condition ensures each element is visited exactly once from start to end using index i that starts at 0?

    1. i u003C nums.length
    2. i u003C= nums.length
    3. i u003C= length
    4. i u003C nums.length - 1
    5. i u003C= nums.lenght
  2. Access Last Element

    If array a has length 5, which expression accesses its last element using the length property?

    1. a[a.length - 1]
    2. a[a.length]
    3. a[5]
    4. a[last]
    5. a[a.lenght - 1]
  3. Meaning of For-Each Variable

    In the construct 'for each value v in arr', what does v represent during traversal?

    1. Each element value
    2. The current index
    3. The array length
    4. A reference to the entire array
    5. The loop counter i
  4. Empty Array Iterations

    How many iterations does the loop 'for i = 0; i u003C arr.length; i++' perform when arr is empty (length 0)?

    1. 0
    2. 1
    3. 2
    4. It runs forever
    5. It throws an error
  5. Valid Indices

    For an array values of length 4, which single index is out of bounds for direct access?

    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  6. Summing Elements

    To compute the total of numeric elements with 'sum = ?; for i = 0; i u003C a.length; i++: sum = sum + a[i]', which initial value for sum is correct?

    1. 0
    2. 1
    3. a.length
    4. a[0]
    5. null
  7. Breaking Early

    Given 'for i = 0; i u003C arr.length; i++: if arr[i] == target: break', what happens when target is found at index 2?

    1. Stops after reaching index 2
    2. Skips index 2 and continues
    3. Restarts from the beginning
    4. Counts how many targets exist
    5. Throws an exception
  8. Reverse Traversal

    Which loop header correctly traverses an array arr from the last element to the first using index i?

    1. for i = arr.length - 1; i u003E= 0; i--
    2. for i = arr.length; i u003E= 0; i--
    3. for i = arr.length - 1; i u003E 0; i--
    4. for i = arr.length; i u003E 0; i--
    5. for i = arr.lenght - 1; i u003E= 0; i--
  9. 2D Array Inner Loop

    To visit every element of a 2D array grid where each row may have a different length, which inner loop condition is correct for row r?

    1. for c = 0; c u003C grid[r].length; c++
    2. for c = 0; c u003C grid.length; c++
    3. for c = 0; c u003C= grid[r].length; c++
    4. for c = 0; c u003C columns; c++
    5. for c = 0; c u003C grid[r].lenght; c++
  10. Index and Value Output

    Given arr = [10, 20, 30] and the loop 'for i = 0; i u003C arr.length; i++: print(i, arr[i])', which pairs are printed?

    1. (0, 10) (1, 20) (2, 30)
    2. (1, 10) (2, 20) (3, 30)
    3. (0, 20) (1, 30) (2, 40)
    4. (0, 10) (1, 20)
    5. It prints the same number for both values