Array Creation and Access - AP Computer Science A
- What will 'arr.length' return for an array 'arr' of 5 elements?
- 5. Property returns total number of elements in array.
- State the syntax to copy an array 'source' to 'destination'.
- System.arraycopy(source, 0, destination, 0, source.length);. System method for efficient array copying.
- How do you create a new array of 5 float elements?
- float[] arr = new float[5];. Standard syntax for float array creation.
- Find the correct syntax to fill the array 'arr' with zeros.
- Arrays.fill(arr, 0);. Utility method fills entire array with specified value.
- 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.
- Identify the error: double[] arr = {1, 2, '3'};
- Incompatible types: char cannot be converted to double. Type mismatch: char literal incompatible with double array.
- What is 'arr[1]' if int[] arr = {10, 20, 30}?
- 20. Index 1 accesses the second element.
- What does 'arr[0].length' return for char[][] arr = new char[5][6]?
- 6. Returns number of columns in first row.
- Which expression evaluates to the length of the first row in a 2D array 'arr'?
- arr[0].length. Accesses length property of first row.
- What is the default value of an uninitialized boolean array element?
- false. Boolean arrays initialize to false by default.
- What will 'arr.length' return for an empty array 'arr'?
- 0. Empty arrays have zero length.
- 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.
- How do you declare an array of 10 boolean values?
- boolean[] arr = new boolean[10];. Standard syntax for boolean array declaration.
- 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.
- What exception is thrown for accessing an invalid array index?
- ArrayIndexOutOfBoundsException. Runtime exception for invalid array indices.
- Which keyword is used to create a new array in Java?
- new. Instantiates array objects in memory.
- What does 'arr[1][1]' access in int[][] arr = {{1, 2}, {3, 4}}?
- 4. Accesses row 1, column 1 (second row, second column).
- What is the result of 'Arrays.toString(arr)' for int[] arr = {1, 2, 3}?
- [1, 2, 3]. Utility method converts array to string representation.
- 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.
- 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.
- What is the default value of an uninitialized int array element?
- 0. Numeric primitive arrays initialize to zero.
- How is a two-dimensional array initialized with values in Java?
- int[][] arr = {{1, 2}, {3, 4}};. Nested braces initialize rows and columns.
- How do you declare a 3x3 2D array of integers?
- int[][] arr = new int[3][3];. Creates rectangular array with specified dimensions.
- How do you access the last element of an array 'arr'?
- arr[arr.length - 1]. Uses array length minus one for last index.
- 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];
- How do you initialize a 1D array with 5 identical values of 9?
- Arrays.fill(arr, 9);. Fills all array elements with specified value.
- What will the expression 'arr[2].length' return for int[][] arr = new int[3][4]?
- 4. Returns column count of row at index 2.
- How do you declare a two-dimensional array of integers?
- int[][] arr;. Double brackets indicate two dimensions.
- 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.
- 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.
- How do you declare an array of Objects in Java?
- Object[] arr;. Object is superclass of all Java classes.
- What is the default value of an uninitialized object array element?
- null. Object arrays initialize to null references.
- What is the syntax to declare an empty array of integers?
- int[] arr = {};. Empty braces create zero-length array.
- What is the index of the first element in a Java array?
- 0. Java arrays use zero-based indexing.
- Which data type can an array in Java be made of?
- Any primitive or object type. Arrays support all Java data types.
- What is the syntax to declare an array of doubles named 'prices'?
- double[] prices;. Declaration without instantiation using semicolon.
- Identify the error: int[] arr = new int[-5];
- Array size cannot be negative. Array size must be non-negative integer.