Implementing Array Algorithms - AP Computer Science A
Card 1 of 30
What is the index of the first element in a Java array?
What is the index of the first element in a Java array?
Tap to reveal answer
- Java arrays use zero-based indexing like most programming languages.
- Java arrays use zero-based indexing like most programming languages.
← Didn't Know|Knew It →
What is the purpose of the 'Arrays.fill()' method?
What is the purpose of the 'Arrays.fill()' method?
Tap to reveal answer
Fills the array with the specified value. Sets every element in the array to the same specified value.
Fills the array with the specified value. Sets every element in the array to the same specified value.
← Didn't Know|Knew It →
What does the 'Arrays.sort()' method do?
What does the 'Arrays.sort()' method do?
Tap to reveal answer
Sorts the specified array into ascending order. Uses an efficient sorting algorithm to arrange elements in order.
Sorts the specified array into ascending order. Uses an efficient sorting algorithm to arrange elements in order.
← Didn't Know|Knew It →
What is the time complexity of accessing an element in an array?
What is the time complexity of accessing an element in an array?
Tap to reveal answer
$O(1)$. Direct memory address calculation provides constant time access.
$O(1)$. Direct memory address calculation provides constant time access.
← Didn't Know|Knew It →
How do you find the length of an array named 'arr' in Java?
How do you find the length of an array named 'arr' in Java?
Tap to reveal answer
arr.length. The 'length' property returns the number of elements in the array.
arr.length. The 'length' property returns the number of elements in the array.
← Didn't Know|Knew It →
What is the result of accessing an array at an invalid index?
What is the result of accessing an array at an invalid index?
Tap to reveal answer
ArrayIndexOutOfBoundsException. Runtime exception thrown when index is negative or >= array length.
ArrayIndexOutOfBoundsException. Runtime exception thrown when index is negative or >= array length.
← Didn't Know|Knew It →
How do you iterate over all elements of an array using a for-each loop?
How do you iterate over all elements of an array using a for-each loop?
Tap to reveal answer
for (int element : arr) { }. Enhanced for loop automatically iterates through each element.
for (int element : arr) { }. Enhanced for loop automatically iterates through each element.
← Didn't Know|Knew It →
Which method converts an array to a string representation in Java?
Which method converts an array to a string representation in Java?
Tap to reveal answer
Arrays.toString(arr). Static method from Arrays class formats array contents as a string.
Arrays.toString(arr). Static method from Arrays class formats array contents as a string.
← Didn't Know|Knew It →
How do you declare an integer array of size 10 in Java?
How do you declare an integer array of size 10 in Java?
Tap to reveal answer
int[] arr = new int[10];. Uses the 'new' keyword to allocate memory for 10 integer elements.
int[] arr = new int[10];. Uses the 'new' keyword to allocate memory for 10 integer elements.
← Didn't Know|Knew It →
What is an array in Java?
What is an array in Java?
Tap to reveal 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.
A collection of elements, all of the same type, stored in contiguous memory. Arrays store homogeneous data with fixed size and zero-based indexing.
← Didn't Know|Knew It →
What is the time complexity of inserting an element in the middle of an array?
What is the time complexity of inserting an element in the middle of an array?
Tap to reveal answer
$O(n)$. Requires shifting all subsequent elements to make space.
$O(n)$. Requires shifting all subsequent elements to make space.
← Didn't Know|Knew It →
What is the default value of an integer array element in Java?
What is the default value of an integer array element in Java?
Tap to reveal answer
- Numeric arrays initialize all elements to zero by default.
- Numeric arrays initialize all elements to zero by default.
← Didn't Know|Knew It →
Identify the error: int[] arr = {1, 2, 3;}
Identify the error: int[] arr = {1, 2, 3;}
Tap to reveal answer
Correct: int[] arr = {1, 2, 3};. Missing closing brace in array initialization syntax.
Correct: int[] arr = {1, 2, 3};. Missing closing brace in array initialization syntax.
← Didn't Know|Knew It →
What is the syntax to initialize a string array with 'a', 'b', 'c'?
What is the syntax to initialize a string array with 'a', 'b', 'c'?
Tap to reveal answer
String[] arr = {"a", "b", "c"};. Array literal syntax initializes elements at declaration time.
String[] arr = {"a", "b", "c"};. Array literal syntax initializes elements at declaration time.
← Didn't Know|Knew It →
How do you copy an array in Java?
How do you copy an array in Java?
Tap to reveal answer
Arrays.copyOf(arr, arr.length). Creates a new array with copied elements from the original.
Arrays.copyOf(arr, arr.length). Creates a new array with copied elements from the original.
← Didn't Know|Knew It →
Identify the error: String[] names = new String[3]{"A", "B", "C"};
Identify the error: String[] names = new String[3]{"A", "B", "C"};
Tap to reveal answer
Correct: String[] names = {"A", "B", "C"};. Cannot specify array size when using initializer list syntax.
Correct: String[] names = {"A", "B", "C"};. Cannot specify array size when using initializer list syntax.
← Didn't Know|Knew It →
What is the time complexity of the 'Arrays.sort()' method?
What is the time complexity of the 'Arrays.sort()' method?
Tap to reveal answer
$O(n \times \text{log} \thinspace n)$. Dual-pivot quicksort provides efficient average-case performance.
$O(n \times \text{log} \thinspace n)$. Dual-pivot quicksort provides efficient average-case performance.
← Didn't Know|Knew It →
What is the time complexity of finding an element in an unsorted array?
What is the time complexity of finding an element in an unsorted array?
Tap to reveal answer
$O(n)$. Linear search requires checking each element sequentially.
$O(n)$. Linear search requires checking each element sequentially.
← Didn't Know|Knew It →
What is the purpose of the 'Arrays.asList()' method?
What is the purpose of the 'Arrays.asList()' method?
Tap to reveal answer
Converts an array to a list. Creates a fixed-size list backed by the original array.
Converts an array to a list. Creates a fixed-size list backed by the original array.
← Didn't Know|Knew It →
What is the syntax for declaring a 3x3 matrix of integers?
What is the syntax for declaring a 3x3 matrix of integers?
Tap to reveal answer
int[][] matrix = new int[3][3];. Two-dimensional array declaration with specific row and column size.
int[][] matrix = new int[3][3];. Two-dimensional array declaration with specific row and column size.
← Didn't Know|Knew It →
What happens if you assign a value outside the array's bounds?
What happens if you assign a value outside the array's bounds?
Tap to reveal answer
Throws ArrayIndexOutOfBoundsException. Arrays have fixed size; cannot expand beyond declared bounds.
Throws ArrayIndexOutOfBoundsException. Arrays have fixed size; cannot expand beyond declared bounds.
← Didn't Know|Knew It →
Identify the error: int[] arr = new int[]{1, 2, 3};
Identify the error: int[] arr = new int[]{1, 2, 3};
Tap to reveal answer
No error; this is a valid declaration. This syntax correctly declares and initializes an array.
No error; this is a valid declaration. This syntax correctly declares and initializes an array.
← Didn't Know|Knew It →
What is the time complexity of inserting an element in the middle of an array?
What is the time complexity of inserting an element in the middle of an array?
Tap to reveal answer
$O(n)$. Requires shifting all subsequent elements to make space.
$O(n)$. Requires shifting all subsequent elements to make space.
← Didn't Know|Knew It →
Find the error: double[] vals = new double[5]{1.0, 2.0};
Find the error: double[] vals = new double[5]{1.0, 2.0};
Tap to reveal answer
Correct: double[] vals = {1.0, 2.0};. Cannot specify size and initializer list together in Java.
Correct: double[] vals = {1.0, 2.0};. Cannot specify size and initializer list together in Java.
← Didn't Know|Knew It →
How do you declare a two-dimensional array in Java?
How do you declare a two-dimensional array in Java?
Tap to reveal answer
int[][] matrix = new int[rows][cols];. Creates a matrix with specified rows and columns dimensions.
int[][] matrix = new int[rows][cols];. Creates a matrix with specified rows and columns dimensions.
← Didn't Know|Knew It →
What does 'System.arraycopy()' do?
What does 'System.arraycopy()' do?
Tap to reveal answer
Copies a source array to a destination array. Native method for efficient array copying between arrays.
Copies a source array to a destination array. Native method for efficient array copying between arrays.
← Didn't Know|Knew It →
What is the time complexity of the 'Arrays.sort()' method?
What is the time complexity of the 'Arrays.sort()' method?
Tap to reveal answer
$O(n \times \text{log} \thinspace n)$. Dual-pivot quicksort provides efficient average-case performance.
$O(n \times \text{log} \thinspace n)$. Dual-pivot quicksort provides efficient average-case performance.
← Didn't Know|Knew It →
Which method checks for the presence of an element in an array?
Which method checks for the presence of an element in an array?
Tap to reveal answer
Arrays.binarySearch(arr, key). Returns the index of the element or negative value if not found.
Arrays.binarySearch(arr, key). Returns the index of the element or negative value if not found.
← Didn't Know|Knew It →
What is the difference between length and length() in Java?
What is the difference between length and length() in Java?
Tap to reveal answer
length is for arrays; length() is for strings. Arrays use property; strings use method for length access.
length is for arrays; length() is for strings. Arrays use property; strings use method for length access.
← Didn't Know|Knew It →
Identify the error: int[] arr = new int[]{1, 2, 3};
Identify the error: int[] arr = new int[]{1, 2, 3};
Tap to reveal answer
No error; this is a valid declaration. This syntax correctly declares and initializes an array.
No error; this is a valid declaration. This syntax correctly declares and initializes an array.
← Didn't Know|Knew It →