AP Computer Science a Flashcards: Implementing Array Algorithms

Study Implementing Array Algorithms 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

Implementing Array Algorithms

0 mastered0 still learning

0% Complete

QUESTION
1/ 60

Identify the error: String[] names = new String[3]{"A", "B", "C"};

Tap card or press Space to flip

ANSWER

Correct: String[] names = {"A", "B", "C"};. Cannot specify array size when using initializer list syntax.

How well did you know it?

Card 1 / 60

What this deck covers

This deck focuses on Implementing Array Algorithms, 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: Identify the error: String[] names = new String[3]{"A", "B", "C"};

Answer: Correct: String[] names = {"A", "B", "C"};. Cannot specify array size when using initializer list syntax.

Flashcard 2: What is the default value of an integer array element in Java?

Answer:

  1. Numeric arrays initialize all elements to zero by default.

Flashcard 3: Identify the error: int[] arr = {1, 2, 3;}

Answer: Correct: int[] arr = {1, 2, 3};. Missing closing brace in array initialization syntax.

Flashcard 4: How do you iterate over all elements of an array using a for-each loop?

Answer: for (int element : arr) { }. Enhanced for loop automatically iterates through each element.

Flashcard 5: What is the result of 'Arrays.equals(arr1, arr2)'?

Answer: True if arr1 and arr2 have the same elements in order. Compares arrays element by element for content equality.

Flashcard 6: What is the purpose of the 'Arrays.asList()' method?

Answer: Converts an array to a list. Creates a fixed-size list backed by the original array.

Flashcard 7: What is the time complexity of inserting an element in the middle of an array?

Answer: O(n)O(n). Requires shifting all subsequent elements to make space.

Flashcard 8: What is an array in Java?

Answer: A collection of elements, all of the same type, stored in contiguous memory. Arrays store homogeneous data with fixed size and zero-based indexing.

Flashcard 9: Identify the error: int[] nums = new [5]int;

Answer: Correct: int[] nums = new int[5];. Incorrect syntax - type should come before array brackets.

Flashcard 10: What is the syntax to initialize a string array with 'a', 'b', 'c'?

Answer: String[] arr = {"a", "b", "c"};. Array literal syntax initializes elements at declaration time.

Flashcard 11: What is the time complexity of the 'Arrays.sort()' method?

Answer: O(n×logn)O(n \times \text{log} \thinspace n). Dual-pivot quicksort provides efficient average-case performance.

Flashcard 12: What is the purpose of the 'Arrays.deepToString()' method?

Answer: Returns a string of a multi-dimensional array. Formats nested arrays with proper string representation.

Flashcard 13: What is the index of the first element in a Java array?

Answer:

  1. Java arrays use zero-based indexing like most programming languages.

Flashcard 14: What is the default value of a boolean array element?

Answer: false. Boolean arrays initialize all elements to false by default.

Flashcard 15: How do you declare an integer array of size 10 in Java?

Answer: int[] arr = new int[10];. Uses the 'new' keyword to allocate memory for 10 integer elements.

Flashcard 16: What is the syntax for declaring a 3x3 matrix of integers?

Answer: int[][] matrix = new int[3][3];. Two-dimensional array declaration with specific row and column size.

Flashcard 17: Identify the error: int[] arr = new int[]{1, 2, 3};

Answer: No error; this is a valid declaration. This syntax correctly declares and initializes an array.

Flashcard 18: Identify the error: int[] nums = new [5]int;

Answer: Correct: int[] nums = new int[5];. Incorrect syntax - type should come before array brackets.

Flashcard 19: What is the default value of a boolean array element?

Answer: false. Boolean arrays initialize all elements to false by default.

Flashcard 20: What is the time complexity of finding an element in an unsorted array?

Answer: O(n)O(n). Linear search requires checking each element sequentially.

Flashcard 21: What does the 'Arrays.sort()' method do?

Answer: Sorts the specified array into ascending order. Uses an efficient sorting algorithm to arrange elements in order.

Flashcard 22: How do you copy an array in Java?

Answer: Arrays.copyOf(arr, arr.length). Creates a new array with copied elements from the original.

Flashcard 23: How do you find the length of an array named 'arr' in Java?

Answer: arr.length. The 'length' property returns the number of elements in the array.

Flashcard 24: What is the difference between length and length() in Java?

Answer: length is for arrays; length() is for strings. Arrays use property; strings use method for length access.

Flashcard 25: What is the purpose of the 'Arrays.fill()' method?

Answer: Fills the array with the specified value. Sets every element in the array to the same specified value.

Flashcard 26: Find the error: double[] vals = new double[5]{1.0, 2.0};

Answer: Correct: double[] vals = {1.0, 2.0};. Cannot specify size and initializer list together in Java.

Flashcard 27: What is the syntax for declaring a 3x3 matrix of integers?

Answer: int[][] matrix = new int[3][3];. Two-dimensional array declaration with specific row and column size.

Flashcard 28: Which method checks for the presence of an element in an array?

Answer: Arrays.binarySearch(arr, key). Returns the index of the element or negative value if not found.

Flashcard 29: How do you declare a two-dimensional array in Java?

Answer: int[][] matrix = new int[rows][cols];. Creates a matrix with specified rows and columns dimensions.

Flashcard 30: What is the purpose of the 'Arrays.deepToString()' method?

Answer: Returns a string of a multi-dimensional array. Formats nested arrays with proper string representation.

Flashcard 31: How do you declare and initialize an empty array?

Answer: int[] arr = new int[0];. Creates an array with zero elements and length of 0.

Flashcard 32: What is the index of the first element in a Java array?

Answer:

  1. Java arrays use zero-based indexing like most programming languages.

Flashcard 33: What is the time complexity of accessing an element in an array?

Answer: O(1)O(1). Direct memory address calculation provides constant time access.

Flashcard 34: Identify the error: int[] arr = new int[]{1, 2, 3};

Answer: No error; this is a valid declaration. This syntax correctly declares and initializes an array.

Flashcard 35: What is the result of 'Arrays.equals(arr1, arr2)'?

Answer: True if arr1 and arr2 have the same elements in order. Compares arrays element by element for content equality.

Flashcard 36: What is the result of accessing an array at an invalid index?

Answer: ArrayIndexOutOfBoundsException. Runtime exception thrown when index is negative or >= array length.

Flashcard 37: What is the time complexity of accessing an element in an array?

Answer: O(1)O(1). Direct memory address calculation provides constant time access.

Flashcard 38: What is the time complexity of finding an element in an unsorted array?

Answer: O(n)O(n). Linear search requires checking each element sequentially.

Flashcard 39: What does the 'Arrays.sort()' method do?

Answer: Sorts the specified array into ascending order. Uses an efficient sorting algorithm to arrange elements in order.

Flashcard 40: What is the purpose of the 'Arrays.fill()' method?

Answer: Fills the array with the specified value. Sets every element in the array to the same specified value.

Flashcard 41: How do you iterate over all elements of an array using a for-each loop?

Answer: for (int element : arr) { }. Enhanced for loop automatically iterates through each element.

Flashcard 42: What happens if you assign a value outside the array's bounds?

Answer: Throws ArrayIndexOutOfBoundsException. Arrays have fixed size; cannot expand beyond declared bounds.

Flashcard 43: What is the purpose of the 'Arrays.asList()' method?

Answer: Converts an array to a list. Creates a fixed-size list backed by the original array.

Flashcard 44: What does 'System.arraycopy()' do?

Answer: Copies a source array to a destination array. Native method for efficient array copying between arrays.

Flashcard 45: What is the difference between length and length() in Java?

Answer: length is for arrays; length() is for strings. Arrays use property; strings use method for length access.

Flashcard 46: How do you declare a two-dimensional array in Java?

Answer: int[][] matrix = new int[rows][cols];. Creates a matrix with specified rows and columns dimensions.

Flashcard 47: What is the result of accessing an array at an invalid index?

Answer: ArrayIndexOutOfBoundsException. Runtime exception thrown when index is negative or >= array length.

Flashcard 48: How do you declare and initialize an empty array?

Answer: int[] arr = new int[0];. Creates an array with zero elements and length of 0.

Flashcard 49: Identify the error: String[] names = new String[3]{"A", "B", "C"};

Answer: Correct: String[] names = {"A", "B", "C"};. Cannot specify array size when using initializer list syntax.

Flashcard 50: Find the error: double[] vals = new double[5]{1.0, 2.0};

Answer: Correct: double[] vals = {1.0, 2.0};. Cannot specify size and initializer list together in Java.

Flashcard 51: What is the time complexity of inserting an element in the middle of an array?

Answer: O(n)O(n). Requires shifting all subsequent elements to make space.

Flashcard 52: What is an array in Java?

Answer: A collection of elements, all of the same type, stored in contiguous memory. Arrays store homogeneous data with fixed size and zero-based indexing.

Flashcard 53: Which method converts an array to a string representation in Java?

Answer: Arrays.toString(arr). Static method from Arrays class formats array contents as a string.

Flashcard 54: Which method checks for the presence of an element in an array?

Answer: Arrays.binarySearch(arr, key). Returns the index of the element or negative value if not found.

Flashcard 55: What does 'System.arraycopy()' do?

Answer: Copies a source array to a destination array. Native method for efficient array copying between arrays.

Flashcard 56: How do you find the length of an array named 'arr' in Java?

Answer: arr.length. The 'length' property returns the number of elements in the array.

Flashcard 57: What is the time complexity of the 'Arrays.sort()' method?

Answer: O(n×logn)O(n \times \text{log} \thinspace n). Dual-pivot quicksort provides efficient average-case performance.

Flashcard 58: How do you declare an integer array of size 10 in Java?

Answer: int[] arr = new int[10];. Uses the 'new' keyword to allocate memory for 10 integer elements.

Flashcard 59: What happens if you assign a value outside the array's bounds?

Answer: Throws ArrayIndexOutOfBoundsException. Arrays have fixed size; cannot expand beyond declared bounds.

Flashcard 60: What is the syntax to initialize a string array with 'a', 'b', 'c'?

Answer: String[] arr = {"a", "b", "c"};. Array literal syntax initializes elements at declaration time.