Array Traversals - AP Computer Science A

  1. What is the default value of an int array element in Java?
    0. Java initializes int arrays with zero values.
  2. What error occurs when accessing an invalid array index?
    ArrayIndexOutOfBoundsException. Thrown when index is negative or >= array length.
  3. What is the output of 'int[] arr = {10, 20}; System.out.println(arr[1]);'?
    20. Index 1 accesses the second element (value 20).
  4. What loop construct is ideal for backward traversal?
    For loop with decrementing index. Start from last index and decrement to zero.
  5. Identify the loop type often used for array traversal.
    For loop. Provides precise index control for element access.
  6. What does 'for (int x : arr)' represent?
    Enhanced for loop for array 'arr'. For-each syntax for iterating over array elements.
  7. What does the condition 'i < arr.length' ensure?
    Prevents accessing out-of-bounds indices. Stops loop before accessing invalid array positions.
  8. What does 'for(int i = arr.length-1; i >= 0; i--)' do?
    Traverses the array in reverse order. Backward iteration from last to first element.
  9. Find and correct the error: 'for(int i = 0; i <= arr.length; i++)'.
    Change '<=' to '<'. Using <= causes access beyond array bounds.
  10. Which keyword is used to exit a loop prematurely?
    break. Terminates loop execution immediately.
  11. Identify the error: 'for(int i = 0; i < arr.length; i--);'
    Change 'i--' to 'i++'. Decrementing creates infinite loop since condition never false.
  12. What is the index of the last element in an array of length nn?
    n−1n - 1. Last valid index is one less than array length.
  13. How do you update the first element of 'arr' to 99?
    arr[0] = 99;. Assigns value 99 to the first array position.
  14. What is the complexity of traversing an array of size nn?
    O(n)O(n). Must visit each of the n elements once.
  15. Which exception is thrown by 'arr[-1]'?
    ArrayIndexOutOfBoundsException. Negative indices are invalid in Java arrays.
  16. Identify the error in 'arr[arr.length] = 5;'.
    Index out of bounds, should be 'arr.length - 1'. Accessing beyond array bounds; maximum index is length-1.
  17. What is the purpose of array traversal?
    To access each element of an array sequentially. Allows systematic processing of data stored in arrays.
  18. What is the benefit of using a loop for array traversal?
    Efficiently processes each element. Avoids repetitive code for accessing individual elements.
  19. Which data type is returned by 'arr.length'?
    int. Returns integer count of array elements.
  20. Identify the loop variable in 'for(int i = 0; i < arr.length; i++)'.
    i. The counter variable controlling loop iterations.
  21. What does 'arr.length - 1' access?
    The last element's index of 'arr'. Calculates the index of the final array element.
  22. Which loop is preferable for known array sizes?
    For loop. Provides precise control over iteration count.
  23. What is a common use of array traversal?
    Summing all elements. Accumulates values by visiting each array element.
  24. In an array, what does 'arr[i] = value;' do?
    Assigns 'value' to the element at index 'i'. Updates the array element at the specified index.
  25. What is the result of 'int[] arr = {1,2,3}; System.out.println(arr[2]);'?
    3. Index 2 accesses the third element (value 3).
  26. What is the output of 'int[] arr = {4, 5}; System.out.println(arr[0]);'?
    4. Index 0 accesses the first element (value 4).
  27. What is the index of the first element in an array?
    0. Arrays use zero-based indexing in Java.
  28. Which loop allows traversal without explicitly using indices?
    Enhanced for loop. Also called for-each loop, no index management needed.
  29. What is the result of accessing 'arr[arr.length]'?
    ArrayIndexOutOfBoundsException. Index equals length, which exceeds valid range.
  30. What does 'arr[i]' represent in a traversal loop?
    The current element of the array. Uses index 'i' to access the element at that position.
  31. What term describes processing each element of an array?
    Iteration. The process of visiting each array element.
  32. How do you print each element of 'arr' on a new line?
    Use a loop: 'for (int i = 0; i < arr.length; i++)'. Iterates through array printing each element sequentially.
  33. What is the main advantage of using an enhanced for loop?
    Simplifies syntax when iterating over arrays. No index management or bounds checking required.
  34. What is the output type of 'arr.length'?
    int. Returns an integer representing array size.
  35. How to access the second element of an array 'arr'?
    arr[1]. Index 1 refers to the second array position.
  36. What does 'arr[i] == 0' check in an array traversal?
    If the current element is zero. Tests if current element equals zero.
  37. Which method is used to find the length of an array in Java?
    array.length. A property that returns the number of elements.
  38. How do you access the last element of an array 'arr'?
    arr[arr.length - 1]. Subtracts 1 from length to get valid last index.
  39. What type of loop is 'for (int i = 0; i < arr.length; i++)'?
    For loop. Standard for loop with initialization, condition, increment.

Study these flashcards