Implementing Array Algorithms - AP Computer Science A

  1. 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.
  2. What is the default value of an integer array element in Java?
    0. Numeric arrays initialize all elements to zero by default.
  3. Identify the error: int[] arr = {1, 2, 3;}
    Correct: int[] arr = {1, 2, 3};. Missing closing brace in array initialization syntax.
  4. 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.
  5. 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.
  6. What is the time complexity of inserting an element in the middle of an array?
    O(n)O(n). Requires shifting all subsequent elements to make space.
  7. 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.
  8. 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.
  9. What is the time complexity of the 'Arrays.sort()' method?
    O(n×log n)O(n \times \text{log} \thinspace n). Dual-pivot quicksort provides efficient average-case performance.
  10. What is the purpose of the 'Arrays.deepToString()' method?
    Returns a string of a multi-dimensional array. Formats nested arrays with proper string representation.
  11. What is the index of the first element in a Java array?
    0. Java arrays use zero-based indexing like most programming languages.
  12. Identify the error: int[] nums = new [5]int;
    Correct: int[] nums = new int[5];. Incorrect syntax - type should come before array brackets.
  13. What is the default value of a boolean array element?
    false. Boolean arrays initialize all elements to false by default.
  14. What is the time complexity of finding an element in an unsorted array?
    O(n)O(n). Linear search requires checking each element sequentially.
  15. How do you copy an array in Java?
    Arrays.copyOf(arr, arr.length). Creates a new array with copied elements from the original.
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. What is the time complexity of accessing an element in an array?
    O(1)O(1). Direct memory address calculation provides constant time access.
  21. 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.
  22. What is the result of accessing an array at an invalid index?
    ArrayIndexOutOfBoundsException. Runtime exception thrown when index is negative or >= array length.
  23. What does the 'Arrays.sort()' method do?
    Sorts the specified array into ascending order. Uses an efficient sorting algorithm to arrange elements in order.
  24. 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.
  25. What happens if you assign a value outside the array's bounds?
    Throws ArrayIndexOutOfBoundsException. Arrays have fixed size; cannot expand beyond declared bounds.
  26. What does 'System.arraycopy()' do?
    Copies a source array to a destination array. Native method for efficient array copying between arrays.
  27. 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.
  28. How do you declare and initialize an empty array?
    int[] arr = new int[0];. Creates an array with zero elements and length of 0.
  29. 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.
  30. 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.
  31. 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.
  32. 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.

Study these flashcards