🎯 OCA Java Practice Test 8

10 Free Practice Questions for 1Z0-808 Certification
All 10 questions shown below with complete explanations

πŸ“š Arrays, Searching & Common Array Pitfalls (Java 8)

πŸ“š Topics Covered in Practice Test 8

Question 1 EASY πŸ“Œ Array Declaration and Instantiation
Which of the following correctly declares and instantiates an integer array of size 7?
A. int values = new int[7];
B. int[] values = new int[7];
C. int values[] = new int();
D. int[] values = {7};

βœ… Correct Answer: B

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.

πŸ“– Detailed Explanation:

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.

Question 2 EASY πŸ“Œ Default Array Values
What is the default value of an element in a long array?
A. 1
B. null
C. 0
D. undefined

βœ… Correct Answer: C

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.

πŸ“– Detailed Explanation:

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.

Question 3 EASY πŸ“Œ Array Length Field
How do you get the number of elements in an array named scores?
A. scores.length()
B. scores.size()
C. scores.length
D. scores.getLength()

βœ… Correct Answer: C

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.

πŸ“– Detailed Explanation:

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

Question 4 EASY πŸ“Œ Printing Arrays
Given int[] data = {7,8,9}; what does System.out.println(data) print?
A. [7, 8, 9]
B. 7 8 9
C. [I@
D. Compilation error

βœ… Correct Answer: C

Printing an array reference uses Object.toString(), which shows type and hash, e.g., [I@5acf9800. To print contents, use Arrays.toString(array).

πŸ“– Detailed Explanation:

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.

Question 5 MEDIUM πŸ“Œ Array Initializer Syntax
Which statement will cause a compilation error?
A. int[] arr = new int[3];
B. int[] arr = {5, 10, 15};
C. int[] arr; arr = {5, 10, 15};
D. int[] arr = new int[]{5,10,15};

βœ… Correct Answer: C

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.

πŸ“– Detailed Explanation:

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.

Question 6 MEDIUM πŸ“Œ Array Size Expressions
Which declaration is valid?
A. float[] arr = new float[6*3/2];
B. float[] arr = new float[6*3/2.0];
C. float[] arr = new float["9"];
D. float[] arr = new float[];

βœ… Correct Answer: A

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.

πŸ“– Detailed Explanation:

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

Question 7 MEDIUM πŸ“Œ Arrays.binarySearch Method
What does Arrays.binarySearch(vals, 20) return if vals = {5, 10, 15, 20, 25}?
A. 3
B. 4
C. 2
D. -1

βœ… Correct 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.

πŸ“– Detailed Explanation:

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

Question 8 MEDIUM πŸ“Œ Array Reference and Aliasing
What is printed?

int[] x = {10, 20, 30};
int[] y = x;
y[0] = 99;
System.out.println(x[0]);
A. 10
B. 99
C. Compilation error
D. 0

βœ… Correct Answer: B

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.

πŸ“– Detailed Explanation:

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.

Question 9 HARD πŸ“Œ Array Declaration Legality
Which of these array declarations is NOT legal? (Choose all that apply)

A. int[][] grades = new int[7][];

B. Object[][][] storage = new Object[4][0][6];

C. String items[] = new items[8];

D. java.util.Date[] schedule[] = new java.util.Date[3][];

E. int[][] categories = new int[];

F. int[][] data = new int[][];
A. A only
B. B and D
C. C, E, F
D. A, B, D
E. All are legal
F. All are illegal

βœ… Correct Answer: C

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.

πŸ“– Detailed Explanation:



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.

Question 10 HARD πŸ“Œ Arrays.binarySearch Requirements
What is the output of the following code?

int[] numbers = {8, -3, 15, 0, -12};
int target = 15;
int index = Arrays.binarySearch(numbers, target);
System.out.println(index);
A. 2
B. 4
C. -1
D. The result is unpredictable
E. Compilation error

βœ… Correct Answer: D

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().

πŸ“– Detailed Explanation:

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

Want More Practice Questions?

Access all 1624 Java OCA 8 questions covering all OCA topics

What You'll Get:

β†’ 1,624 practice questions

β†’ 29 full certification tests

β†’ Chapter-wise practice exams

β†’ Detailed explanations for every answer

β†’ Progress tracking & certificates

Start Free Trial Now β†’