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