Java Arrays & Multi-Dimensional Arrays Quiz

Java
0 Passed
0% acceptance

35 deep and practical questions on Java arrays — from declaration and initialization to advanced 2D/3D usage, Arrays utility methods, common pitfalls, and performance gotchas that trip even senior developers.

35 Questions
~70 minutes
1

Question 1

What is the output?

java
int[] arr = new int[5];
System.out.println(arr[2]);
A
0
B
null
C
Compilation error
D
Garbage value
2

Question 2

What is the value of arr.length in this declaration?

java
String[][] matrix = { {"a", "b"}, {"c", "d", "e"}, {"f"} };
A
3
B
6
C
Compilation error
D
2
3

Question 3

What is printed?

java
int[] nums = {10, 20, 30};
int[] copy = Arrays.copyOf(nums, 5);
System.out.println(copy[3] + " " + copy[4]);
A
0 0
B
30 30
C
null null
D
Exception
4

Question 4

Which of these is NOT a valid way to declare a 2D array?

A
int[][] grid = new int[3][4];
B
int grid[][] = new int[3][4];
C
int[] grid[] = new int[3][4];
D
int grid[3][4];
5

Question 5

What is the output?

java
Object[] objects = new String[10];
objects[0] = Integer.valueOf(42);
System.out.println("No exception");
A
ArrayStoreException at runtime
B
No exception
C
Compilation error
D
NullPointerException
6

Question 6

What is printed?

java
int[][] table = new int[3][];
table[0] = new int[] {1, 2};
table[1] = new int[] {3, 4, 5};
table[2] = new int[1];
System.out.println(table[1].length);
A
3
B
5
C
NullPointerException
D
Compilation error
7

Question 7

Which method correctly compares two 2D arrays for deep equality?

A
Arrays.deepEquals(arr1, arr2)
B
Arrays.equals(arr1, arr2)
C
arr1.equals(arr2)
D
arr1 == arr2
8

Question 8

What is the output?

java
int[] data = {5, 2, 8, 1};
Arrays.sort(data);
System.out.println(Arrays.binarySearch(data, 2));
A
1
B
0
C
-2
D
Negative value (not found)
9

Question 9

What is printed?

java
boolean[] flags = new boolean[3];
System.out.println(flags[0] + " " + flags[1]);
A
false false
B
null null
C
0 0
D
Garbage values
10

Question 10

What happens here?

java
int[] source = {1, 2, 3};
int[] target = source;
target[0] = 99;
System.out.println(source[0]);
A
99
B
1
C
Compilation error
D
NullPointerException
11

Question 11

What is the output?

java
int[] nums = new int[] {10, 20};
int[] clone = nums.clone();
clone[0] = 999;
System.out.println(nums[0] + " " + clone[0]);
A
10 999
B
999 999
C
10 10
D
Exception
12

Question 12

Which loop is generally preferred for iterating over an array?

A
Enhanced for loop (for-each)
B
Traditional for loop with index
C
While loop
D
No difference
13

Question 13

What is printed?

java
int[][][] cube = new int[2][3][4];
System.out.println(cube[1][2].length);
A
4
B
3
C
2
D
NullPointerException
14

Question 14

What is the result?

java
int[] arr = {1, 2, 3};
Arrays.fill(arr, 42);
System.out.println(Arrays.toString(arr));
A
[42, 42, 42]
B
[1, 2, 3]
C
[42, 2, 3]
D
Exception
15

Question 15

What is printed?

java
String[] words = {"java", "python", "js"};
Arrays.sort(words);
System.out.println(words[0]);
A
java
B
js
C
python
D
Exception
16

Question 16

How many objects are created by this line?

javascript
int[][] grid = new int[3][4];
A
4 objects (1 outer + 3 inner arrays)
B
1 object
C
13 objects
D
12 objects
17

Question 17

Which of these creates an array with values 0, 0, 0, 0, 0?

A
int[] a = new int[5];
B
int[] a = {0,0,0,0,0};
C
Both A and B
D
Neither
18

Question 18

What is the default value of elements in an Object array?

A
null
B
0
C
false
D
Garbage
19

Question 19

What is the output?

java
int[] a = {1, 2};
int[] b = {1, 2};
System.out.println(Arrays.equals(a, b));
A
true
B
false
C
Compilation error
D
Exception
20

Question 20

Why is this considered a serious bug?

java
private int[] data = {1, 2, 3};
public int[] getData() { return data; }
A
Caller can modify the internal array
B
Returns null sometimes
C
Too slow
D
Compilation error
21

Question 21

What is the result of Arrays.asList on a primitive array?

A
List containing one element: the entire primitive array
B
List<int[]> with each int as element
C
Compilation error
D
Auto-boxing to List<Integer>
22

Question 22

Which statement about array length is correct?

A
length is a final field, not a method
B
length() is a method like in collections
C
length can be changed after creation
D
length is only available at runtime
23

Question 23

What is printed?

java
char[] chars = {'J', 'a', 'v', 'a'};
String s = new String(chars, 1, 3);
System.out.println(s);
A
ava
B
Java
C
av
D
Exception
24

Question 24

What is the fastest way to copy an array?

A
System.arraycopy
B
Arrays.copyOf
C
clone()
D
Manual for loop
25

Question 25

What is the output?

java
int[] arr = new int[0];
System.out.println(arr.length);
A
0
B
Exception
C
null
D
Compilation error
26

Question 26

Can you change the length of an array after creation?

A
No — arrays have fixed size
B
Yes, using resize()
C
Yes, by reassigning length field
D
Only for multi-dimensional arrays
27

Question 27

What is printed?

java
int[][] irregular = {{1}, {2, 3}, {4, 5, 6}};
System.out.println(irregular[2][1]);
A
5
B
6
C
IndexOutOfBoundsException
D
null
28

Question 28

Which method returns a formatted string like [1, 2, 3]?

A
Arrays.toString(array)
B
array.toString()
C
String.valueOf(array)
D
Both A and C
29

Question 29

What is the result of Arrays.binarySearch on an unsorted array?

A
Undefined — may return wrong index or negative value
B
Always returns -1
C
Throws exception
D
Works anyway
30

Question 30

What is the most important rule when working with arrays?

A
Never expose mutable internal arrays — always return a defensive copy
B
Always use == to compare arrays
C
Arrays are resizable
D
clone() performs deep copy
31

Question 31

Can an array’s component type be a primitive?

A
Yes — int[], boolean[], etc. are valid
B
No — only reference types
C
Only for single-dimensional arrays
D
Only in Java 8+
32

Question 32

What is printed?

java
byte[] bytes = new byte[2];
bytes[0] = 100;
bytes[1] = 100;
System.out.println(bytes[0] + bytes[1]);
A
200
B
44
C
-56
D
Compilation error
33

Question 33

What is the result?

java
int[] arr = null;
System.out.println(arr.length);
A
NullPointerException
B
0
C
Compilation error
D
-1
34

Question 34

Which of these is true about Arrays.parallelSort?

A
Uses multiple threads for large arrays
B
Always slower than Arrays.sort
C
Only works on primitive arrays
D
Deprecated
35

Question 35

After mastering arrays, you now know that:

A
Arrays are powerful but require careful handling — they are not collections
B
Arrays are always safe and immutable
C
clone() copies nested arrays
D
All 2D arrays are rectangular

QUIZZES IN Java