Java Topic Practice
Array Basics
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
Array Basics MCQ Practice
Array Basics Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Array Basics: track the final printed value.
int[] data = {4, 9, 2, 7, 5, 1};
int pivot = data[1];
int result = 0;
for (int i = 0; i < data.length; i++) {
if (i % 2 == 0) {
result += data[i] - pivot;
} else if (data[i] >= pivot) {
result += data[i] % 2;
} else {
result -= i;
}
}
System.out.print(result);All 15 array basics questions
Work through the interactive quiz above first. This is the full array basics question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Array Basics: track the final printed value.
int[] data = {4, 9, 2, 7, 5, 1}; int pivot = data[1]; int result = 0; for (int i = 0; i < data.length; i++) { if (i % 2 == 0) { result += data[i] - pivot; } else if (data[i] >= pivot) { result += data[i] % 2; } else { result -= i; } } System.out.print(result);- -14
- -29
- 9
- -23
Show worked solution
Correct answer: -23
The trace combines index parity, a pivot value, modulus, and an else branch. Each element contributes based on both its index and value.
2.Code Tracing 12 - Array Basics: follow the variable updates.
int[] data = {6, 7, 4, 9, 7, 3}; int total = 0; int skips = 0; for (int i = 0; i < data.length; i++) { if (data[i] > 8) { total += data[i] - i; } else { skips++; } } System.out.print(total + ":" + skips);- 6:5
- 11:5
- 6:1
- 14:5
Show worked solution
Correct answer: 6:5
The loop uses every index. Values above the threshold add value minus index; all other values increase skips.
3.Code Tracing 28 - Array Basics: trace the branch and loop path.
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] data = {9, 7, 2, 8, 10, 6}; for (int i = 1; i < data.length; i++) { data[i] += data[i - 1] % 3; } System.out.print(Arrays.toString(data)); } }- [9, 7, 3, 8, 12, 6]
- [9, 7, 2, 8, 10, 6]
- [6, 12, 8, 3, 7, 9]
- [13, 4, 8, 7, 6, 9]
Show worked solution
Correct answer: [9, 7, 3, 8, 12, 6]
Each update uses the already-mutated previous element, so the array state changes as the loop moves right.
4.Code Tracing 39 - Array Basics: watch the index changes.
int[] data = {11, 8, 4, 10, 6, 8}; int index = -1; for (int i = 0; i < data.length; i++) { if (data[i] >= 8 && index == -1) { index = i; } } System.out.print(index);- The code does not compile.
- 0
- 1
- -1
Show worked solution
Correct answer: 0
The guard index == -1 keeps the first matching index instead of replacing it later.
5.Code Tracing 50 - Array Basics: evaluate the state change step by step.
int[] data = {13, 9, 6, 8, 8, 10}; int total = 0; int previous = data[0]; for (int i = 1; i < data.length; i++) { if (data[i] > previous) { total += data[i] - previous; } else { total -= previous - data[i]; } previous = data[i]; } System.out.print(total);- 7
- 6
- -3
- 3
Show worked solution
Correct answer: -3
The trace compares adjacent values. Increases add to total, decreases subtract, and previous changes after each comparison.
6.What is printed by the following code segment?
int[] a = new int[4]; a[a.length - 1] = a.length; System.out.print(a[3] + a[0] + a.length);- 4
- 8
- 7
- 12
Show worked solution
Correct answer: 8
Only the last element is assigned, so the sum is 4 plus 0 plus the length.
7.What is printed by the following code segment?
int[] a = {5, 10, 15}; int[] b = a; b[0] = b[2]; System.out.print(a[0] + " " + b[2]);- 5 15
- 15 5
- 15 15
- 5 5
Show worked solution
Correct answer: 15 15
The two names refer to one array, so the write through b is visible through a.
8.What is printed by the following code segment?
int[] a = {1, 2, 3}; int[] b = new int[3]; for (int i = 0; i < a.length; i++) { b[a.length - 1 - i] = a[i]; } System.out.print(b[0] + "" + b[1] + b[2]);- 123
- 111
- 333
- 321
Show worked solution
Correct answer: 321
Reading forward while writing backward copies the array in reverse.
9.What is the result of executing the following code segment?
int[] a = new int[3]; int i = a.length; a[i - 1] = 1; a[i] = 2;- An ArrayIndexOutOfBoundsException is thrown after the first assignment succeeds.
- Both assignments succeed.
- The code does not compile.
- An exception is thrown before either assignment runs.
Show worked solution
Correct answer: An ArrayIndexOutOfBoundsException is thrown after the first assignment succeeds.
The first index is the last valid one, and the second is one past the end.
10.What is printed by the following code segment?
String[] s = new String[3]; s[1] = "b"; int c = 0; for (String v : s) { if (v != null) { c++; } } System.out.print(c);- 3
- 1
- 0
- 2
Show worked solution
Correct answer: 1
An object array starts full of null and only one slot was assigned.
11.What is printed by the following code segment?
int[] a = {3, 1, 4, 1, 5}; System.out.print(a[a[1]] + a[a[3]]);- 8
- 6
- 2
- 4
Show worked solution
Correct answer: 2
Both inner values are 1, so the expression reads index 1 twice and adds 1 to 1.
12.What is printed by the following code segment?
double[] d = new double[2]; boolean[] b = new boolean[2]; System.out.print(d[0] + " " + b[0] + " " + d.length);- 0 false 2
- 0.0 true 2
- 0.0 false 0
- 0.0 false 2
Show worked solution
Correct answer: 0.0 false 2
Numeric elements default to 0.0 and boolean elements to false, and the length is the declared capacity.
13.What is printed by the following code segment?
int n = 3; int[] a = new int[n]; n = 10; int[] b = new int[n]; System.out.print(a.length + b.length);- 13
- 20
- 6
- 10
Show worked solution
Correct answer: 13
The first length was fixed when that array was created, so changing the variable only affects the second.
14.What is printed by the following code segment?
int[] a = {2, 4}; int[] b = {2, 4}; System.out.print((a == b) + " " + (a[0] == b[0])); System.out.print(" " + (a.length == b.length));- true true true
- false true true
- false false true
- false true false
Show worked solution
Correct answer: false true true
The references differ because each initializer builds its own array, but the element values and lengths match.
15.What is printed by the following code segment?
int[] a = new int[3]; for (int i = 0; i < a.length; i++) { a[i] = i * i - i; } System.out.print(a[0] + "" + a[1] + a[2]);- 012
- 014
- 002
- 024
Show worked solution
Correct answer: 002
The formula gives 0, 0, and 2 for the three indexes.
