Array Creation and Access - AP Computer Science A

  1. What will 'arr.length' return for an array 'arr' of 5 elements?
    5. Property returns total number of elements in array.
  2. State the syntax to copy an array 'source' to 'destination'.
    System.arraycopy(source, 0, destination, 0, source.length);. System method for efficient array copying.
  3. How do you create a new array of 5 float elements?
    float[] arr = new float[5];. Standard syntax for float array creation.
  4. Find the correct syntax to fill the array 'arr' with zeros.
    Arrays.fill(arr, 0);. Utility method fills entire array with specified value.
  5. Find the mistake: int[] arr = new int[3]{1, 2, 3};
    Size should not be specified in array initializer. Cannot specify size with initializer list.
  6. Identify the error: double[] arr = {1, 2, '3'};
    Incompatible types: char cannot be converted to double. Type mismatch: char literal incompatible with double array.
  7. What is 'arr[1]' if int[] arr = {10, 20, 30}?
    20. Index 1 accesses the second element.
  8. What does 'arr[0].length' return for char[][] arr = new char[5][6]?
    6. Returns number of columns in first row.
  9. Which expression evaluates to the length of the first row in a 2D array 'arr'?
    arr[0].length. Accesses length property of first row.
  10. What is the default value of an uninitialized boolean array element?
    false. Boolean arrays initialize to false by default.
  11. What will 'arr.length' return for an empty array 'arr'?
    0. Empty arrays have zero length.
  12. How do you assign a value 42 to the fifth element of an array 'arr'?
    arr[4] = 42;. Fifth element has index 4 in zero-based indexing.
  13. How do you declare an array of 10 boolean values?
    boolean[] arr = new boolean[10];. Standard syntax for boolean array declaration.
  14. How do you initialize an array of strings named 'colors' with 'red', 'green', 'blue'?
    String[] colors = {"red", "green", "blue"};. Array literal syntax using curly braces.
  15. What exception is thrown for accessing an invalid array index?
    ArrayIndexOutOfBoundsException. Runtime exception for invalid array indices.
  16. Which keyword is used to create a new array in Java?
    new. Instantiates array objects in memory.
  17. What does 'arr[1][1]' access in int[][] arr = {{1, 2}, {3, 4}}?
    4. Accesses row 1, column 1 (second row, second column).
  18. What is the result of 'Arrays.toString(arr)' for int[] arr = {1, 2, 3}?
    [1, 2, 3]. Utility method converts array to string representation.
  19. What is the syntax to declare a char array named 'letters' with size 5?
    char[] letters = new char[5];. Standard syntax for character array declaration.
  20. How do you access the third element of an array named 'arr'?
    arr[2]. Arrays use 0-based indexing, so index 2 is the third element.
  21. What is the default value of an uninitialized int array element?
    0. Numeric primitive arrays initialize to zero.
  22. How is a two-dimensional array initialized with values in Java?
    int[][] arr = {{1, 2}, {3, 4}};. Nested braces initialize rows and columns.
  23. How do you declare a 3x3 2D array of integers?
    int[][] arr = new int[3][3];. Creates rectangular array with specified dimensions.
  24. How do you access the last element of an array 'arr'?
    arr[arr.length - 1]. Uses array length minus one for last index.
  25. What is the syntax to declare an integer array named 'arr' with 10 elements?
    int[] arr = new int[10];. Standard array declaration: type[] name = new type[size];
  26. How do you initialize a 1D array with 5 identical values of 9?
    Arrays.fill(arr, 9);. Fills all array elements with specified value.
  27. What will the expression 'arr[2].length' return for int[][] arr = new int[3][4]?
    4. Returns column count of row at index 2.
  28. How do you declare a two-dimensional array of integers?
    int[][] arr;. Double brackets indicate two dimensions.
  29. What is the syntax to declare a long array with 100 elements?
    long[] arr = new long[100];. Standard syntax for long array with specified size.
  30. What does 'arr[0][1]' refer to in a 2D array 'arr'?
    Element in first row, second column. First index is row, second is column.
  31. How do you declare an array of Objects in Java?
    Object[] arr;. Object is superclass of all Java classes.
  32. What is the default value of an uninitialized object array element?
    null. Object arrays initialize to null references.
  33. What is the syntax to declare an empty array of integers?
    int[] arr = {};. Empty braces create zero-length array.
  34. What is the index of the first element in a Java array?
    0. Java arrays use zero-based indexing.
  35. Which data type can an array in Java be made of?
    Any primitive or object type. Arrays support all Java data types.
  36. What is the syntax to declare an array of doubles named 'prices'?
    double[] prices;. Declaration without instantiation using semicolon.
  37. Identify the error: int[] arr = new int[-5];
    Array size cannot be negative. Array size must be non-negative integer.

Study these flashcards