Chapter 10: Arrays
Multiple Choice · Basics
Question 1: Length and Last Index
int[] a = new int[6]; creates an array. Which statement is true?
C: The length is 6, the indices run 0 … length − 1 = 5. a[6] is out of bounds. For arrays length is a field, written without parentheses; length() is a String method.
Output Question · Basics
Question 2: Default Values
What is the output of the following code?
int[] a = new int[3]; double[] d = new double[2]; boolean[] b = new boolean[2]; String[] s = new String[2]; System.out.println(a[1] + " " + d[0] + " " + b[1] + " " + s[0]); System.out.println(a.length + d.length + s.length);
Show answer
Output:
0 0.0 false null 7
- The cells of an array created with
newget the default value of the type. - In the second line three
ints are added: 3 + 2 + 2 = 7 (there is no String first, so it is addition).
Multiple Choice · Access
Question 3: i <= arr.length
Which option gives the output of the following code fragment correctly?
int arr[] = {1, 2, 3, 4, 5}; for (int i = 0; i <= arr.length; ++i) System.out.print(arr[i] + " ");
D: Because the condition is <=, i becomes 5 in the last round; arr[5] does not exist → ArrayIndexOutOfBoundsException. 1 2 3 4 5 has already been printed before the error, but the program ends with the error. The code compiles: bounds checking is done at run time, not at compile time. The correct form is i < arr.length.
Step-by-Step Trace · Access
Question 4: arr[i] = arr[arr[i]]
What is the output of the code? (Options: A) [0, 2, 3, 1] B) [0, 3, 1, 3] C) [2, 3, 2, 3] D) [0, 1, 2, 3])
An array of four elements. Note: the values are also valid indices (0–3).
i = 0; 0 < 4 → true.
arr[0] = 0 → arr[0] = arr[0] = 0; no change.
i = 1; 1 < 4 → true.
arr[1] = 2 → arr[1] = arr[2] = 3.
i = 2; 2 < 4 → true.
arr[2] = 3 → arr[2] = arr[3] = 1.
i = 3; 3 < 4 → true.
arr[3] = 1 → arr[3] = arr[1]. arr[1] is no longer 2 but the 3 written in step i = 1!
i = 4; 4 < 4 → false, the loop ends.
The correct option is B. Because changes are made in the same array, later steps read the updated values.
int[] arr = {0, 2, 3, 1}; for (int i = 0; i < arr.length; i++) { arr[i] = arr[arr[i]]; } System.out.println(Arrays.toString(arr));
Output Question · Access
Question 5: Reference Copy and clone
What is the output of the following code?
int[] a = {1, 2, 3}; int[] b = a; b[0] = 9; int[] c = a.clone(); c[1] = 50; a[2] = 7; System.out.println(a[0] + " " + b[2] + " " + c[1] + " " + c[2]);
Show answer
Output: 9 7 50 3
b = adoes not create a new array: a and b refer to the same object, sob[0] = 9changes a as well.a.clone()creates a separate array with the current contents ({9, 2, 3}); later changes in c and in a do not affect each other.
Multiple Choice · Loops
Question 6: Assignment in an Enhanced for
What is the output of the following code?
int[] a = {3, 5, 7}; for (int x : a) { x = x * 2; } System.out.println(Arrays.toString(a));
B: In an enhanced for, x is a copy of the cell; assigning to x does not change the array. To modify the array an indexed for is needed: a[i] = a[i] * 2;
Output Question · Loops
Question 7: Backwards and Two at a Time
What is the output of the following code?
int[] a = {3, 6, 9, 12, 15, 18}; for (int i = a.length - 1; i >= 0; i -= 2) { System.out.print(a[i] + " "); }
Show answer
Output: 18 12 6
- i starts at length − 1 = 5 and decreases by 2 each round: 5, 3, 1.
- The next value is −1 →
-1 >= 0is false and the loop ends (no out-of-bounds access).
Output Question · Loops
Question 8: The Large Even Ones
What is the output when the following code is run?
int[] numbers = {12, 7, 3, 21, 1, 5, 8, 9, 20, 13, 12, 14, 16}; for (int i = 0; i < numbers.length; i++) { if (numbers[i] > 10 && numbers[i] % 2 == 0) { System.out.print(numbers[i] + " "); } }
Show answer
Output: 12 20 12 14 16
- Both conditions (
&&) must hold: greater than 10 and even. - 21 and 13 are large but odd; 8 is even but smaller than 10 → not printed. 12 appears twice, so it is printed twice.
Multiple Choice · Operations
Question 9: Maximum: Wrong Starting Value
If the starting value in the findMax method is written as 0 instead of arr[0], what does findMax(new int[]{-8, -3, -5}) return?
static int findMax(int[] arr) { int largest = 0; // instead of arr[0] for (int i = 0; i < arr.length; i++) if (arr[i] > largest) largest = arr[i]; return largest; }
D: No element is greater than 0, so the condition is never met and 0, which is not in the array, is returned. The candidate must always be an element of the array (arr[0]); then the result would be −3.
Output Question · Operations
Question 10: Average and Empty Array
The correct average method is called with three different arrays. What is the output?
static double average(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) sum += arr[i]; return (double) sum / arr.length; } // inside main: System.out.println(average(new int[]{4, 7, 2, 6})); System.out.println(average(new int[]{5, 6})); System.out.println(average(new int[0]));
Show answer
Output:
4.75 5.5 NaN
- 19.0 / 4 = 4.75 and 11.0 / 2 = 5.5.
- In an empty array the loop never runs and sum stays 0:
0.0 / 0is floating-point division, gives no error and produces NaN (Not a Number). (With two ints,0 / 0would throw an ArithmeticException.) The checkarr.length > 0should be made first.
Output Question · Operations
Question 11: Median Element
What is the output of the median method?
static double median(int[] arr) { Arrays.sort(arr); if (arr.length % 2 == 0) { int mid1 = arr[arr.length / 2 - 1]; int mid2 = arr[arr.length / 2]; return (mid1 + mid2) / 2.0; } return arr[arr.length / 2]; } // inside main: System.out.println(median(new int[]{9, 2, 7, 4})); System.out.println(median(new int[]{8, 1, 6, 3, 5}));
Show answer
Output:
5.5 5.0
- Even length (4): sorted {2, 4, 7, 9}; the two middle elements are at indices 1 and 2 → (4 + 7) / 2.0 = 5.5.
- Odd length (5): sorted {1, 3, 5, 6, 8}; index 5 / 2 = 2 → 5; the return type is double → 5.0.
Multiple Choice · Operations
Question 12: Side Effect of the Median
What is the output of the following code?
static double median(int[] arr) { Arrays.sort(arr); if (arr.length % 2 == 0) { int mid1 = arr[arr.length / 2 - 1]; int mid2 = arr[arr.length / 2]; return (mid1 + mid2) / 2.0; } return arr[arr.length / 2]; } // inside main: int[] a = {9, 2, 7, 4}; double m = median(a); System.out.println(Arrays.toString(a) + " " + m);
C: The method receives the reference of the array; Arrays.sort(arr) also sorts the array a in main. To keep the original order, arr.clone() should be sorted inside the method.
Multiple Choice · Operations
Question 13: Swapping Across the Whole Array
What is the output of this code, which tries to reverse the array?
int[] a = {1, 2, 3, 4}; for (int i = 0; i < a.length; i++) { int t = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = t; } System.out.println(Arrays.toString(a));
A: Because the loop traverses the whole array, each pair is swapped twice: i = 0, 1 reverses the array, i = 2, 3 reverses it back. The loop should run only up to the middle (i < a.length / 2).
Step-by-Step Trace · Operations
Question 14: Rotating One Position to the Right
The array is rotated one position to the right without an extra array (the last element goes to the front). What is its final state?
The initial array.
The last element is saved so that it is not lost.
We go from right to left: i = 3; 3 > 0 → true.
a[3] = a[2] = 3.
i = 2: a[2] = a[1] = 2.
i = 1: a[1] = a[0] = 1.
i = 0; 0 > 0 → false, the loop ends.
The saved value is written to the front.
If we had shifted from left to right (i increasing), a[0] would always be copied: after the loop [1, 1, 1, 1], the result [4, 1, 1, 1].
int[] a = {1, 2, 3, 4}; int last = a[a.length - 1]; for (int i = a.length - 1; i > 0; i--) { a[i] = a[i - 1]; } a[0] = last; System.out.println(Arrays.toString(a));
Step-by-Step Trace · Searching and Sorting
Question 15: Binary Search: target = 11
We search for 11 in a sorted array. Which index is printed; how do lo, hi and mid change?
The search range is the whole array: lo = 0, hi = 6.
0 <= 6 → mid = (0 + 6) / 2 = 3, a[3] = 15.
15 == 11 → false; 15 < 11 → false.
else: hi = mid − 1 = 2. The right half is eliminated.
0 <= 2 → mid = (0 + 2) / 2 = 1, a[1] = 8.
8 == 11 → false; 8 < 11 → true: lo = mid + 1 = 2.
2 <= 2 → mid = 2, a[2] = 11.
11 == 11 → result = 2, break.
Found in 3 comparison rounds (a linear search would also look at 3 elements; in a large array the difference is between O(log n) and O(n)).
int[] a = {3, 8, 11, 15, 20, 27, 31}; int target = 11, lo = 0, hi = a.length - 1; int result = -1; while (lo <= hi) { int mid = (lo + hi) / 2; if (a[mid] == target) { result = mid; break; } if (a[mid] < target) lo = mid + 1; else hi = mid - 1; } System.out.println(result);
Multiple Choice · Searching and Sorting
Question 16: binarySearch: Not Found
What does Arrays.binarySearch(new int[]{2, 4, 6, 8}, 7) return?
C: 7 is not present; the place where it belongs in the sorted array (the insertion point) is index 3 (between 6 and 8). The return value is −(insertion point) − 1 = −3 − 1 = −4. A negative result means “not found”.
Multiple Choice · Searching and Sorting
Question 17: Bubble Sort: First Pass
Only the first pass of bubble sort is performed. What is the output?
int[] a = {5, 1, 4, 2, 3}; for (int j = 0; j < a.length - 1; j++) { if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } System.out.println(Arrays.toString(a));
D: In every comparison 5 swaps with the smaller element on its right and “bubbles” to the end: [1,5,4,2,3] → [1,4,5,2,3] → [1,4,2,5,3] → [1,4,2,3,5]. A full sort needs n − 1 passes.
Output Question · Matrix
Question 18: Row, Column and Cell
What is the output of the following code?
int[][] m = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(m.length + " " + m[0].length);
System.out.println(m[1][2] + m[0][1]);
m[1][0] = m[0][2] * 10;
System.out.println(Arrays.toString(m[1]));Show answer
Output:
2 3 8 [30, 5, 6]
m.lengthis the number of rows (2),m[0].lengthis the number of columns of the first row (3).- The first index is the row, the second the column: m[1][2] = 6, m[0][1] = 2 → 6 + 2 = 8.
- m[1][0] = 3 × 10 = 30;
m[1]is a row array.
Step-by-Step Trace · Matrix
Question 19: Row Sums
Nested loops print the sum of each row. What is the output?
2 rows, 3 columns.
Row i = 0; sum is reset at the start of each row.
sum += m[0][0] → 1.
sum += m[0][1] → 3.
sum += m[0][2] → 6. Then j = 3 < 3 → false.
The row sum is printed.
Row i = 1; sum is 0 again.
sum += m[1][0] → 4.
sum += m[1][1] → 9.
sum += m[1][2] → 15.
The sum of the second row is printed.
i = 2; 2 < 2 → false. If the line sum = 0 were not inside the outer loop, the second result would be 21.
int[][] m = {{1, 2, 3}, {4, 5, 6}}; for (int i = 0; i < m.length; i++) { int sum = 0; for (int j = 0; j < m[i].length; j++) { sum += m[i][j]; } System.out.print(sum + " "); }
Multiple Choice · Matrix
Question 20: null Row in a Jagged Array
What happens when the following code is run?
int[][] m = new int[3][]; m[0] = new int[2]; m[2] = new int[4]; System.out.println(m[0].length + m[2].length); System.out.println(m[1].length);
B: new int[3][] creates only the outer array; m[1] is never assigned, so it stays null. After 2 + 4 = 6 is printed, m[1].length tries to read a field of null → NullPointerException.