AP Computer Science a Flashcards: Array Traversals

Study Array Traversals in AP Computer Science a with focused flashcards that help you recognize the idea, recall the key rule, and apply it in practice-style prompts.

AP Computer Science a

Array Traversals

0 mastered0 still learning

0% Complete

QUESTION
1/ 78

What is the default value of an int array element in Java?

Tap card or press Space to flip

ANSWER
  1. Java initializes int arrays with zero values.

How well did you know it?

Card 1 / 78

What this deck covers

This deck focuses on Array Traversals, giving you a quick way to review the definitions, rules, and examples that matter most for AP Computer Science a.

How to use these flashcards

Work through these flashcards in short sessions. Try to answer each prompt before flipping the card, then revisit any cards you miss until the explanation feels automatic.

All flashcards

Flashcard 1: What is the default value of an int array element in Java?

Answer:

  1. Java initializes int arrays with zero values.

Flashcard 2: What is the output of 'int[] arr = {10, 20}; System.out.println(arr[1]);'?

Answer:

  1. Index 1 accesses the second element (value 20).

Flashcard 3: What error occurs when accessing an invalid array index?

Answer: ArrayIndexOutOfBoundsException. Thrown when index is negative or >= array length.

Flashcard 4: What does the condition 'i < arr.length' ensure?

Answer: Prevents accessing out-of-bounds indices. Stops loop before accessing invalid array positions.

Flashcard 5: What is the output of 'int[] arr = {10, 20}; System.out.println(arr[1]);'?

Answer:

  1. Index 1 accesses the second element (value 20).

Flashcard 6: What loop construct is ideal for backward traversal?

Answer: For loop with decrementing index. Start from last index and decrement to zero.

Flashcard 7: How do you update the first element of 'arr' to 99?

Answer: arr[0] = 99;. Assigns value 99 to the first array position.

Flashcard 8: Identify the loop type often used for array traversal.

Answer: For loop. Provides precise index control for element access.

Flashcard 9: Identify the error: 'for(int i = 0; i < arr.length; i--);'

Answer: Change 'i--' to 'i++'. Decrementing creates infinite loop since condition never false.

Flashcard 10: What does 'for (int x : arr)' represent?

Answer: Enhanced for loop for array 'arr'. For-each syntax for iterating over array elements.

Flashcard 11: What does the condition 'i < arr.length' ensure?

Answer: Prevents accessing out-of-bounds indices. Stops loop before accessing invalid array positions.

Flashcard 12: What does 'for(int i = arr.length-1; i >= 0; i--)' do?

Answer: Traverses the array in reverse order. Backward iteration from last to first element.

Flashcard 13: Identify the loop variable in 'for(int i = 0; i < arr.length; i++)'.

Answer: i. The counter variable controlling loop iterations.

Flashcard 14: In an array, what does 'arr[i] = value;' do?

Answer: Assigns 'value' to the element at index 'i'. Updates the array element at the specified index.

Flashcard 15: What is a common use of array traversal?

Answer: Summing all elements. Accumulates values by visiting each array element.

Flashcard 16: What is the output of 'int[] arr = {4, 5}; System.out.println(arr[0]);'?

Answer:

  1. Index 0 accesses the first element (value 4).

Flashcard 17: What does 'arr.length - 1' access?

Answer: The last element's index of 'arr'. Calculates the index of the final array element.

Flashcard 18: Find and correct the error: 'for(int i = 0; i <= arr.length; i++)'.

Answer: Change '<=' to '<'. Using <= causes access beyond array bounds.

Flashcard 19: What does 'arr[i]' represent in a traversal loop?

Answer: The current element of the array. Uses index 'i' to access the element at that position.

Flashcard 20: Which keyword is used to exit a loop prematurely?

Answer: break. Terminates loop execution immediately.

Flashcard 21: How to access the second element of an array 'arr'?

Answer: arr[1]. Index 1 refers to the second array position.

Flashcard 22: What is the result of accessing 'arr[arr.length]'?

Answer: ArrayIndexOutOfBoundsException. Index equals length, which exceeds valid range.

Flashcard 23: What does 'for(int i = arr.length-1; i >= 0; i--)' do?

Answer: Traverses the array in reverse order. Backward iteration from last to first element.

Flashcard 24: Which method is used to find the length of an array in Java?

Answer: array.length. A property that returns the number of elements.

Flashcard 25: Identify the error: 'for(int i = 0; i < arr.length; i--);'

Answer: Change 'i--' to 'i++'. Decrementing creates infinite loop since condition never false.

Flashcard 26: Which loop is preferable for known array sizes?

Answer: For loop. Provides precise control over iteration count.

Flashcard 27: What does 'for (int x : arr)' represent?

Answer: Enhanced for loop for array 'arr'. For-each syntax for iterating over array elements.

Flashcard 28: What is the main advantage of using an enhanced for loop?

Answer: Simplifies syntax when iterating over arrays. No index management or bounds checking required.

Flashcard 29: Which keyword is used to exit a loop prematurely?

Answer: break. Terminates loop execution immediately.

Flashcard 30: What is the index of the last element in an array of length nn?

Answer: n1n - 1. Last valid index is one less than array length.

Flashcard 31: How do you update the first element of 'arr' to 99?

Answer: arr[0] = 99;. Assigns value 99 to the first array position.

Flashcard 32: What is the complexity of traversing an array of size nn?

Answer: O(n)O(n). Must visit each of the n elements once.

Flashcard 33: Which exception is thrown by 'arr[-1]'?

Answer: ArrayIndexOutOfBoundsException. Negative indices are invalid in Java arrays.

Flashcard 34: Identify the error in 'arr[arr.length] = 5;'.

Answer: Index out of bounds, should be 'arr.length - 1'. Accessing beyond array bounds; maximum index is length-1.

Flashcard 35: What is the purpose of array traversal?

Answer: To access each element of an array sequentially. Allows systematic processing of data stored in arrays.

Flashcard 36: Which loop allows traversal without explicitly using indices?

Answer: Enhanced for loop. Also called for-each loop, no index management needed.

Flashcard 37: What is the purpose of array traversal?

Answer: To access each element of an array sequentially. Allows systematic processing of data stored in arrays.

Flashcard 38: What is the benefit of using a loop for array traversal?

Answer: Efficiently processes each element. Avoids repetitive code for accessing individual elements.

Flashcard 39: Which data type is returned by 'arr.length'?

Answer: int. Returns integer count of array elements.

Flashcard 40: Identify the loop variable in 'for(int i = 0; i < arr.length; i++)'.

Answer: i. The counter variable controlling loop iterations.

Flashcard 41: How do you print each element of 'arr' on a new line?

Answer: Use a loop: 'for (int i = 0; i < arr.length; i++)'. Iterates through array printing each element sequentially.

Flashcard 42: Identify the loop type often used for array traversal.

Answer: For loop. Provides precise index control for element access.

Flashcard 43: What is the benefit of using a loop for array traversal?

Answer: Efficiently processes each element. Avoids repetitive code for accessing individual elements.

Flashcard 44: What term describes processing each element of an array?

Answer: Iteration. The process of visiting each array element.

Flashcard 45: What does 'arr.length - 1' access?

Answer: The last element's index of 'arr'. Calculates the index of the final array element.

Flashcard 46: Which loop is preferable for known array sizes?

Answer: For loop. Provides precise control over iteration count.

Flashcard 47: What is a common use of array traversal?

Answer: Summing all elements. Accumulates values by visiting each array element.

Flashcard 48: What is the index of the last element in an array of length nn?

Answer: n1n - 1. Last valid index is one less than array length.

Flashcard 49: What is the index of the first element in an array?

Answer:

  1. Arrays use zero-based indexing in Java.

Flashcard 50: What loop construct is ideal for backward traversal?

Answer: For loop with decrementing index. Start from last index and decrement to zero.

Flashcard 51: What is the default value of an int array element in Java?

Answer:

  1. Java initializes int arrays with zero values.

Flashcard 52: In an array, what does 'arr[i] = value;' do?

Answer: Assigns 'value' to the element at index 'i'. Updates the array element at the specified index.

Flashcard 53: What is the result of 'int[] arr = {1,2,3}; System.out.println(arr[2]);'?

Answer:

  1. Index 2 accesses the third element (value 3).

Flashcard 54: What is the output of 'int[] arr = {4, 5}; System.out.println(arr[0]);'?

Answer:

  1. Index 0 accesses the first element (value 4).

Flashcard 55: What error occurs when accessing an invalid array index?

Answer: ArrayIndexOutOfBoundsException. Thrown when index is negative or >= array length.

Flashcard 56: What is the index of the first element in an array?

Answer:

  1. Arrays use zero-based indexing in Java.

Flashcard 57: Identify the error in 'arr[arr.length] = 5;'.

Answer: Index out of bounds, should be 'arr.length - 1'. Accessing beyond array bounds; maximum index is length-1.

Flashcard 58: Which data type is returned by 'arr.length'?

Answer: int. Returns integer count of array elements.

Flashcard 59: Find and correct the error: 'for(int i = 0; i <= arr.length; i++)'.

Answer: Change '<=' to '<'. Using <= causes access beyond array bounds.

Flashcard 60: How do you access the last element of an array 'arr'?

Answer: arr[arr.length - 1]. Subtracts 1 from length to get valid last index.

Flashcard 61: What is the result of 'int[] arr = {1,2,3}; System.out.println(arr[2]);'?

Answer:

  1. Index 2 accesses the third element (value 3).

Flashcard 62: Which loop allows traversal without explicitly using indices?

Answer: Enhanced for loop. Also called for-each loop, no index management needed.

Flashcard 63: What is the result of accessing 'arr[arr.length]'?

Answer: ArrayIndexOutOfBoundsException. Index equals length, which exceeds valid range.

Flashcard 64: What does 'arr[i]' represent in a traversal loop?

Answer: The current element of the array. Uses index 'i' to access the element at that position.

Flashcard 65: What term describes processing each element of an array?

Answer: Iteration. The process of visiting each array element.

Flashcard 66: How do you print each element of 'arr' on a new line?

Answer: Use a loop: 'for (int i = 0; i < arr.length; i++)'. Iterates through array printing each element sequentially.

Flashcard 67: What is the main advantage of using an enhanced for loop?

Answer: Simplifies syntax when iterating over arrays. No index management or bounds checking required.

Flashcard 68: What type of loop is 'for (int i = 0; i < arr.length; i++)'?

Answer: For loop. Standard for loop with initialization, condition, increment.

Flashcard 69: What is the output type of 'arr.length'?

Answer: int. Returns an integer representing array size.

Flashcard 70: How to access the second element of an array 'arr'?

Answer: arr[1]. Index 1 refers to the second array position.

Flashcard 71: What is the output type of 'arr.length'?

Answer: int. Returns an integer representing array size.

Flashcard 72: What does 'arr[i] == 0' check in an array traversal?

Answer: If the current element is zero. Tests if current element equals zero.

Flashcard 73: Which exception is thrown by 'arr[-1]'?

Answer: ArrayIndexOutOfBoundsException. Negative indices are invalid in Java arrays.

Flashcard 74: What does 'arr[i] == 0' check in an array traversal?

Answer: If the current element is zero. Tests if current element equals zero.

Flashcard 75: What is the complexity of traversing an array of size nn?

Answer: O(n)O(n). Must visit each of the n elements once.

Flashcard 76: Which method is used to find the length of an array in Java?

Answer: array.length. A property that returns the number of elements.

Flashcard 77: How do you access the last element of an array 'arr'?

Answer: arr[arr.length - 1]. Subtracts 1 from length to get valid last index.

Flashcard 78: What type of loop is 'for (int i = 0; i < arr.length; i++)'?

Answer: For loop. Standard for loop with initialization, condition, increment.