Array Traversals - AP Computer Science A
- What is the default value of an int array element in Java?
- 0. Java initializes int arrays with zero values.
- What error occurs when accessing an invalid array index?
- ArrayIndexOutOfBoundsException. Thrown when index is negative or >= array length.
- What is the output of 'int[] arr = {10, 20}; System.out.println(arr[1]);'?
- 20. Index 1 accesses the second element (value 20).
- What loop construct is ideal for backward traversal?
- For loop with decrementing index. Start from last index and decrement to zero.
- Identify the loop type often used for array traversal.
- For loop. Provides precise index control for element access.
- What does 'for (int x : arr)' represent?
- Enhanced for loop for array 'arr'. For-each syntax for iterating over array elements.
- What does the condition 'i < arr.length' ensure?
- Prevents accessing out-of-bounds indices. Stops loop before accessing invalid array positions.
- 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.
- Find and correct the error: 'for(int i = 0; i <= arr.length; i++)'.
- Change '<=' to '<'. Using <= causes access beyond array bounds.
- Which keyword is used to exit a loop prematurely?
- break. Terminates loop execution immediately.
- Identify the error: 'for(int i = 0; i < arr.length; i--);'
- Change 'i--' to 'i++'. Decrementing creates infinite loop since condition never false.
- What is the index of the last element in an array of length ?
- . Last valid index is one less than array length.
- How do you update the first element of 'arr' to 99?
- arr[0] = 99;. Assigns value 99 to the first array position.
- What is the complexity of traversing an array of size ?
- . Must visit each of the n elements once.
- Which exception is thrown by 'arr[-1]'?
- ArrayIndexOutOfBoundsException. Negative indices are invalid in Java arrays.
- 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.
- What is the purpose of array traversal?
- To access each element of an array sequentially. Allows systematic processing of data stored in arrays.
- What is the benefit of using a loop for array traversal?
- Efficiently processes each element. Avoids repetitive code for accessing individual elements.
- Which data type is returned by 'arr.length'?
- int. Returns integer count of array elements.
- Identify the loop variable in 'for(int i = 0; i < arr.length; i++)'.
- i. The counter variable controlling loop iterations.
- What does 'arr.length - 1' access?
- The last element's index of 'arr'. Calculates the index of the final array element.
- Which loop is preferable for known array sizes?
- For loop. Provides precise control over iteration count.
- What is a common use of array traversal?
- Summing all elements. Accumulates values by visiting each array element.
- 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.
- What is the result of 'int[] arr = {1,2,3}; System.out.println(arr[2]);'?
- 3. Index 2 accesses the third element (value 3).
- What is the output of 'int[] arr = {4, 5}; System.out.println(arr[0]);'?
- 4. Index 0 accesses the first element (value 4).
- What is the index of the first element in an array?
- 0. Arrays use zero-based indexing in Java.
- Which loop allows traversal without explicitly using indices?
- Enhanced for loop. Also called for-each loop, no index management needed.
- What is the result of accessing 'arr[arr.length]'?
- ArrayIndexOutOfBoundsException. Index equals length, which exceeds valid range.
- 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.
- What term describes processing each element of an array?
- Iteration. The process of visiting each array element.
- 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.
- What is the main advantage of using an enhanced for loop?
- Simplifies syntax when iterating over arrays. No index management or bounds checking required.
- What is the output type of 'arr.length'?
- int. Returns an integer representing array size.
- How to access the second element of an array 'arr'?
- arr[1]. Index 1 refers to the second array position.
- What does 'arr[i] == 0' check in an array traversal?
- If the current element is zero. Tests if current element equals zero.
- Which method is used to find the length of an array in Java?
- array.length. A property that returns the number of elements.
- How do you access the last element of an array 'arr'?
- arr[arr.length - 1]. Subtracts 1 from length to get valid last index.
- What type of loop is 'for (int i = 0; i < arr.length; i++)'?
- For loop. Standard for loop with initialization, condition, increment.