Introduction to Using Data Sets - AP Computer Science A
- What is an alternative to arrays for dynamic data storage?
- ArrayList. Provides dynamic resizing capabilities that arrays lack.
- Identify the error: int[] arr = new int[5]{1, 2, 3, 4, 5};
- Remove size: int[] arr = {1, 2, 3, 4, 5};. Cannot specify size when using array literal initialization syntax.
- How do you declare a String array with specific values?
- String[] array = {"value1", "value2"};. Array literal syntax works for String objects like other types.
- Identify the error: int[] arr = new int[3]; arr[3] = 10;
- Access beyond bounds; valid indices are 0 to 2. Index 3 exceeds bounds since valid indices are 0, 1, 2.
- What is the default value for elements in a boolean array?
- false. Boolean arrays initialize to false, unlike numeric arrays (zero).
- What is the time complexity of accessing an element in an array?
- . Direct index-based access provides constant time complexity.
- What method is used to sort an array in Java?
- Arrays.sort(array);. Utility method that sorts elements in ascending order using quicksort.
- What keyword is used to create an instance of an array?
- new. Required keyword for dynamic memory allocation of array objects.
- Which class provides static methods for manipulating arrays?
- java.util.Arrays. Contains utility methods like sort(), toString(), and copyOf().
- What is the default value of an int array element in Java?
- 0. Java initializes numeric array elements to zero by default.
- State the syntax to access the third element of an array named 'arr'.
- arr[2]. Uses zero-based indexing, so index 2 represents the third element.
- What is an array in Java?
- A data structure to store multiple values of the same type. Allows efficient storage and access to multiple values using indices.
- How do you declare a 2D integer array with 3 rows and 4 columns?
- int[][] array = new int[3][4];. Allocates space for a rectangular grid with specified dimensions.
- How do you declare an integer array of size 10 in Java?
- int[] array = new int[10];. Creates an array with 10 integer slots, all initially zero.
- Identify the index of the first element in an array.
- 0. Java uses zero-based indexing for all arrays.
- What is the primary advantage of using arrays?
- Efficient access and storage of elements of the same type. Constant-time access and contiguous memory allocation improve performance.
- What is a common use case for 2D arrays?
- Representing matrices or grids. Ideal for tabular data like game boards or spreadsheets.
- Which method converts an array to a list?
- Arrays.asList(array). Returns fixed-size list backed by the original array.
- Which exception is thrown for accessing an invalid array index?
- ArrayIndexOutOfBoundsException. Specific runtime exception for array bounds violations.
- How do you copy an array 'arr' into a new array 'copy'?
- int[] copy = Arrays.copyOf(arr, arr.length);. Creates a shallow copy with the same length as original.
- How do you determine the length of a string in Java?
- string.length(). String method (with parentheses) unlike array.length property.
- How do you reverse an array 'arr' in Java?
- Use a loop to swap elements or Collections.reverse(Arrays.asList(arr));. Multiple approaches exist since arrays lack built-in reverse method.
- How do you initialize an array with values 1, 2, 3 in Java?
- int[] array = {1, 2, 3};. Array literal syntax that creates and initializes in one step.
- State the purpose of the 'Arrays.toString()' method.
- Converts array to a readable string format. Formats array contents as [element1, element2, ...] for debugging.
- How do you find the maximum value in an integer array 'arr'?
- Use a loop or Arrays.stream(arr).max().getAsInt();. Manual loop or stream API provides maximum value efficiently.
- State the purpose of the 'System.arraycopy()' function.
- Copies elements from one array to another. System method for efficient bulk array element transfer.
- How do you check if two arrays 'a' and 'b' are equal?
- Arrays.equals(a, b). Compares elements pairwise rather than reference equality.
- Identify the output: int[] arr = {10, 20}; System.out.println(arr[1]);
- 20. Accesses the second element (index 1) which contains value 20.
- How do you iterate over an array 'arr' using a for-each loop?
- for (int element : arr) { }. Enhanced for loop syntax that iterates without index management.
- How do you access the element at row 2, column 3 in a 2D array 'array'?
- array[1][2]. First index is row, second index is column in 2D arrays.
- Which data type can an array hold?
- Arrays can hold any primitive or object type. Supports primitives (int, char) and reference types (String, Object).
- What is the syntax to declare a char array in Java?
- char[] array = new char[size];. Character array declaration follows same pattern as other types.
- State the method to fill an array with a specific value.
- Arrays.fill(array, value);. Efficiently sets all array elements to the specified value.
- What is a 2D array?
- An array of arrays, often used for matrices. Each element is itself an array, creating rows and columns.
- What does 'array.length' return for a 2D array?
- The number of rows. First dimension represents rows; use array[i].length for columns.
- How do you create a reference to an existing array 'arr'?
- int[] newRef = arr;. Creates alias pointing to same memory location, not a copy.
- What is the difference between an array and an ArrayList?
- ArrayList can dynamically resize; arrays have fixed size. ArrayList grows automatically while arrays have fixed capacity.
- What is the time complexity of sorting an array using Arrays.sort()?
- . Uses efficient dual-pivot quicksort algorithm for optimal performance.
- What is an ArrayIndexOutOfBoundsException?
- An error when accessing an index outside the array's bounds. Runtime exception thrown when index is negative or >= length.