Java Arrays Quiz: 2D Arrays, Copying and Patterns Practice
Test your Java array skills with 12 questions and two programs on indexing, 2D and jagged arrays, the Arrays class, copying, rotation and prefix sums.
- Course: Java study plan
- Module: Arrays
- Kind: Checkpoint — cleared at 70%
- Reading time: 22 min
- Runtime: OpenJDK 18
Checkpoint — Arrays is the checkpoint that closes the Arrays module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers array creation and indexing, two-dimensional and jagged arrays, the Arrays class and System.arraycopy, aliasing and copying, and the standard in-place patterns.
How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module.
Before you start, make sure you can answer:
- What
new String[3]contains, and whata.lengthis (field or method). - How many objects
new int[3][4]creates, and whatclone()copies on it. - What
Arrays.asList(new int[]{1, 2}).size()returns. - Why a getter should return
values.clone(). - How to rotate an array in place, and why prefix arrays have
n + 1entries.
The programs are a matrix rotation and a prefix-sum range query — both classics that reward knowing the pattern.
Common questions
What does new String[3] contain in Java?
Three null references. Every slot of a new array starts at its type's default value, and for any reference type, String included, that default is null — so names[0].length() before any assignment throws a NullPointerException.
What does Arrays.asList(new int[]{1, 2}).size() return?
Arrays.asListdoes not box primitives, so the wholeint[]becomes the list's single element and the result is aList<int[]>. UseArrays.stream(a).boxed().toList()to get aList<Integer>.
Why does a prefix sum array have n + 1 entries?
The extra leading zero, prefix[0] = 0, lets every range sum be prefix[to] - prefix[from], including ranges that start at index 0, without a special case. prefix[i] is then the sum of the first i elements.
Exercises
Rotate a square matrix
Read an integer n, then an n × n grid of integers. Rotate it 90° clockwise in place — transpose (swap across the main diagonal, each pair once), then reverse each row — and print the result, one row per line, values space-separated.
Example: 3 then 1 2 3 / 4 5 6 / 7 8 9 →
7 4 1
8 5 2
9 6 3Range sums with a prefix array
Read an integer n, then n integers (which may be negative and large), then an integer q and q queries l r (0-based, inclusive on both ends). Build a prefix-sum array of n + 1 longs once and answer every query in O(1). Print one sum per line.
Example: 5 then 1 2 3 4 5, 3 then 0 4, 1 3, 2 2 →
15
9
3Pascal's triangle
Read an integer n (1 ≤ n ≤ 30). Build the first n rows of Pascal's triangle as a jagged long[][] — row i has i + 1 entries, each the sum of the two above it — and print each row space-separated. Then print rowSum=<sum of the last row> (which is 2^(n-1)) and middle=<the largest value in the last row>.
Example: 4 →
1
1 1
1 2 1
1 3 3 1
rowSum=8
middle=3In this module: Arrays
← Array patterns — reverse, rotate, count, prefix sums · Classes, objects and references →