10 Free Practice Questions for 1Z0-808 Certification
All 10 questions shown below with complete explanations
Option B uses the correct array declaration and instantiation syntax. int[] values = new int[7]; creates an int array of size 7. Option A misses brackets, C uses invalid new int(), and D creates an array of length 1 with the value 7.
Java arrays must be declared with brackets and instantiated with a size. Option A is invalid because int values declares a single int, not an int array. Option B is correct: int[] values = new int[7]; creates an int array with 7 elements initialized to 0. Option C is invalid because new int() is not allowed; array creation requires a size inside brackets like new int[size]. Option D uses an array initializer {7} which creates an array of length 1, not 7. Therefore, only option B correctly declares and creates an integer array of size 7.
Elements of a long[] array default to 0 because long is a primitive numeric type.
In Java, primitive numeric arrays (byte, short, int, long, float, double) are always initialized to zero when created.
When an array is created in Java, the JVM automatically initializes every element with a default value, even before the array is used.
> Primitive types vs Reference types
Primitive numeric types (byte, short, int, long, float, double) β default to 0
char - default '\u0000'
boolean - default false
Reference types (String, arrays of objects, wrapper classes, custom objects) - default to null
>Why long[] defaults to 0
long is a primitive numeric type, not an object.
So a long[] nums = new long[5]; will contain:
[0, 0, 0, 0, 0]
Each element is stored as the primitive long value 0L (printed as 0).
> Common misconception
Some learners confuse:
long[] (primitive array) - 0
Long[] (wrapper object array) - null
> Conclusion
Since a long[] holds primitive long values, each element is automatically initialized to 0, making C the correct answer.
Arrays expose a public field length (no parentheses) to indicate size. String uses length() method, collections use size() method, but arrays use the length field.
The correct way to get the number of elements in an array named scores is: C. scores.length
Explanation :
In Java, arrays use a public field called length (no parentheses).It tells you how many elements the array can hold.
It is not a method, so you do not write scores.length().
Why others are wrong:
A. scores.length() β Arrays donβt have a method named length()
B. scores.size() β Only Lists use .size(), not arrays
D. scores.getLength() β No such method exists in Java arrays
So the correct answer is C. scores.length
Printing an array reference uses Object.toString(), which shows type and hash, e.g., [I@5acf9800. To print contents, use Arrays.toString(array).
In Java, arrays are objects, but they don't override toString().
Option A: [7, 8, 9] - INCORRECT. This is the formatted output you would get from Arrays.toString(data), but not from printing the array reference directly.
Option B: 7 8 9 - INCORRECT. This format might appear if you manually loop and print elements, but not from direct array printing.
Option C: [I@hashcode - CORRECT. When you print an array directly with System.out.println(data), Java calls data.toString(). Arrays inherit toString() from Object class. Object.toString() returns: getClass().getName() + '@' + Integer.toHexString(hashCode()). For int[], the class name is '[I' where '[' means array and 'I' means int. The output looks like '[I@5acf9800' where 5acf9800 is the hexadecimal hash code. Different array types show different prefixes: int[] shows [I@, double[] shows [D@, String[] shows [Ljava.lang.String;@, etc.
Option D: Compilation error - INCORRECT. The code compiles fine. System.out.println() accepts any Object (including arrays), and arrays are valid objects.
The shorthand initializer { ... } is allowed only at declaration time; after declaration you must use new int[]{...}. Option C tries to use shorthand syntax in assignment, causing compilation error.
Option A: int[] arr = new int[3]; - VALID. Standard array creation with explicit size. Creates array of 3 elements, all initialized to 0.
Option B: int[] arr = {5, 10, 15}; - VALID. Shorthand array initializer syntax. This can only be used during declaration. The compiler infers the array type and size from the initializer list. Creates an array of length 3 with values [5, 10, 15].
Option C: int[] arr; arr = {5, 10, 15}; - COMPILATION ERROR. This is the correct answer. The shorthand initializer {...} is ONLY allowed when combined with declaration. After a separate declaration (int[] arr;), you cannot use the shorthand syntax. The compiler needs the type information which is missing in the assignment context.
Option D: int[] arr = new int[]{5,10,15}; - VALID. This is the explicit array initializer syntax. Unlike the shorthand, this can be used in assignment statements or as method arguments. The size is automatically determined from the number of elements provided.
The expression 6*3/2 evaluates to int 9 at compile time; arrays accept int sizes. Option B uses floating-point (9.0), C uses String, D omits size - all invalid.
The correct answer is A
In Java, when you create an array, the size must be an integer.
Now evaluate each option:
A: float[] arr = new float[6*3/2]; β VALID
6*3/2 uses integer arithmetic, because all literals are int.
6*3 = 18, 18/2 = 9
Final array size is 9 β valid.
B: new float[6*3/2.0]; β INVALID
2.0 is a double, so expression becomes a double.
Array size cannot be double β compilation error.
C: new float["9"]; β INVALID
"9" is a String, not an integer.
D: new float[]; β INVALID
You must provide a size or initializer list:
new float[5]
new float[]{1,2}
new float[]
Final Answer: A
Arrays.binarySearch searches sorted array for target value. In vals = {5,10,15,20,25}, the value 20 is at index 3. Binary search returns the index where element is found.
The array is:
Index: 0 1 2 3 4
Value: 5 10 15 20 25
Arrays.binarySearch(vals, 20) returns the index of the value 20.
20 is at index 3.
Correct answer: A β 3
Both x and y reference the same array object. Modifying via y affects x because they share the same underlying array. Thus y[0]=99 changes the array, making x[0] return 99.
Line 1: int[] x = {10, 20, 30}; - Creates an array object [10, 20, 30] on the heap. Variable x holds a reference to this array object. Let's say the array is at memory address 0x1000.
Line 2: int[] y = x; - Assigns x's reference to y. Now y also points to the same array at 0x1000. Important: This does NOT create a copy of the array. Both x and y reference the same underlying array object.
Line 3: y[0] = 99; - Modifies the first element of the array through y. Changes index 0 from 10 to 99. Since x and y point to the same array, this modification affects both. The array is now [99, 20, 30].
Line 4: System.out.println(x[0]); - Prints the first element accessed through x. Since x points to the array that was modified via y, x[0] shows 99.
Option A: 10 - INCORRECT. This would be correct if y were a separate copy, but y is just another reference to the same array. The modification through y affects x.
Option B: 99 - CORRECT. Both x and y reference the same array. Modifying through y changes the array that x references. Therefore x[0] returns 99.
Option C: Compilation error - INCORRECT. The code is perfectly valid. Array assignment and element access are legal operations.
Option D: 0 - INCORRECT. This is the default value for int arrays, but the array was initialized with {10, 20, 30}, and then modified to 99.
C is illegal because 'items' is a variable name, not a type (should be 'new String[8]'). E is illegal due to dimension mismatch (2D vs 1D) and missing array size. F is illegal because array creation requires at least the first dimension size.
A. int[][] grades = new int[7][]; β LEGAL
Creates a 2D array with 7 rows; each row is uninitialized (null).This is valid.
B. Object[][][] storage = new Object[4][0][6]; β LEGAL
You can have a dimension of size 0 in Java arrays.So this is valid.
C. String items[] = new items[8]; β ILLEGAL
items is not a type β should be new String[8].This is a compilation error.
D. java.util.Date[] schedule[] = new java.util.Date[3][]; β LEGAL
This is just a messy way of declaring a 2D array.
Equivalent to:
Date[][] schedule = new Date[3][]; //Valid.
E. int[][] categories = new int[]; β ILLEGAL
You must specify at least one size:
new int[3][] or new int[3][4], etc.
This is a compilation error.
F. int[][] data = new int[][]; β ILLEGAL
You cannot create a 2D array without specifying at least the first dimension.
This is a compilation error.
Correct Answer: C) C, E, F . These three are the only illegal declarations.
Arrays.binarySearch() REQUIRES the array to be sorted in ascending order. When called on an unsorted array, the behavior is undefined and the result is unpredictable. Always use Arrays.sort() before binarySearch().
The correct answer is D β The result is unpredictable.
Arrays.binarySearch() requires the array to be sorted in ascending order before you call it.
But here:int[] numbers = {8, -3, 15, 0, -12};
This array is NOT sorted.When you call:
Arrays.binarySearch(numbers, 15);
Java performs a binary search on an unsorted array, and the JDK documentation clearly states:
If the array is not sorted, the result is undefined.That means the method may return:
-a wrong index
-a negative value
-any number
-it will not throw an exception
-it will compile
Therefore the correct answer is: D. The result is unpredictable
Access all 1624 Java OCA 8 questions covering all OCA topics
β 1,624 practice questions
β 29 full certification tests
β Chapter-wise practice exams
β Detailed explanations for every answer
β Progress tracking & certificates