Introduction to Using Data Sets - AP Computer Science A

  1. What is an alternative to arrays for dynamic data storage?
    ArrayList. Provides dynamic resizing capabilities that arrays lack.
  2. 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.
  3. How do you declare a String array with specific values?
    String[] array = {"value1", "value2"};. Array literal syntax works for String objects like other types.
  4. 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.
  5. What is the default value for elements in a boolean array?
    false. Boolean arrays initialize to false, unlike numeric arrays (zero).
  6. What is the time complexity of accessing an element in an array?
    O(1)O(1). Direct index-based access provides constant time complexity.
  7. What method is used to sort an array in Java?
    Arrays.sort(array);. Utility method that sorts elements in ascending order using quicksort.
  8. What keyword is used to create an instance of an array?
    new. Required keyword for dynamic memory allocation of array objects.
  9. Which class provides static methods for manipulating arrays?
    java.util.Arrays. Contains utility methods like sort(), toString(), and copyOf().
  10. What is the default value of an int array element in Java?
    0. Java initializes numeric array elements to zero by default.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. Identify the index of the first element in an array.
    0. Java uses zero-based indexing for all arrays.
  16. 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.
  17. What is a common use case for 2D arrays?
    Representing matrices or grids. Ideal for tabular data like game boards or spreadsheets.
  18. Which method converts an array to a list?
    Arrays.asList(array). Returns fixed-size list backed by the original array.
  19. Which exception is thrown for accessing an invalid array index?
    ArrayIndexOutOfBoundsException. Specific runtime exception for array bounds violations.
  20. 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.
  21. How do you determine the length of a string in Java?
    string.length(). String method (with parentheses) unlike array.length property.
  22. 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.
  23. 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.
  24. State the purpose of the 'Arrays.toString()' method.
    Converts array to a readable string format. Formats array contents as [element1, element2, ...] for debugging.
  25. 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.
  26. State the purpose of the 'System.arraycopy()' function.
    Copies elements from one array to another. System method for efficient bulk array element transfer.
  27. How do you check if two arrays 'a' and 'b' are equal?
    Arrays.equals(a, b). Compares elements pairwise rather than reference equality.
  28. Identify the output: int[] arr = {10, 20}; System.out.println(arr[1]);
    20. Accesses the second element (index 1) which contains value 20.
  29. 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.
  30. 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.
  31. Which data type can an array hold?
    Arrays can hold any primitive or object type. Supports primitives (int, char) and reference types (String, Object).
  32. 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.
  33. State the method to fill an array with a specific value.
    Arrays.fill(array, value);. Efficiently sets all array elements to the specified value.
  34. What is a 2D array?
    An array of arrays, often used for matrices. Each element is itself an array, creating rows and columns.
  35. What does 'array.length' return for a 2D array?
    The number of rows. First dimension represents rows; use array[i].length for columns.
  36. How do you create a reference to an existing array 'arr'?
    int[] newRef = arr;. Creates alias pointing to same memory location, not a copy.
  37. 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.
  38. What is the time complexity of sorting an array using Arrays.sort()?
    O(n×log(n))O(n \times \text{log}(n)). Uses efficient dual-pivot quicksort algorithm for optimal performance.
  39. What is an ArrayIndexOutOfBoundsException?
    An error when accessing an index outside the array's bounds. Runtime exception thrown when index is negative or >= length.

Study these flashcards