What is the index of the first element of a Java array?
Valid indexes run from 0 to length − 1.
Arrays in Java · Exercise 1 of 4
Twenty multiple-choice questions on Java arrays: creation and default values, length, indexes, references and copies, the Arrays class, 2D and jagged arrays, and covariance. Pick an option to check your answer instantly.
What is the index of the first element of a Java array?
Valid indexes run from 0 to length − 1.
How do you get the number of elements in an array a?
length is a field for arrays; Strings use length() and collections size().
What does new int[3] contain?
Array elements are always default-initialised, even in local arrays.
What does new String[2] contain?
Reference elements default to null; calling a method on them throws NullPointerException.
What happens when a program reads a[a.length]?
Java checks every array access at run time.
Can you add a sixth element to an array created with new int[5]?
Arrays.copyOf(a, 6) creates a new, larger copy.
After int[] b = a;, what is true?
Use a.clone() for a separate copy.
What does new int[]{1, 2}.equals(new int[]{1, 2}) return?
Use Arrays.equals(a, b) for contents.
What does System.out.println(new int[]{1, 2, 3}) print?
Arrays do not override toString(); use Arrays.toString(a).
What must be true before calling Arrays.binarySearch(a, key)?
On unsorted data the result is undefined.
Timed online tests with instant scores and explanations.