🎯 OCA Java Practice Test 4

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

πŸ“š OCA Core Concepts: Varargs, Collections, Inheritance & Access Control

πŸ“š Topics Covered in Practice Test 4

About this Practice Test

This practice test covers a mixed set of OCA Java SE 8 topics including Inheritance, Varargs, Collections (ArrayList), and Access Modifiers. These topics appear frequently across the real 1Z0-808 exam.

πŸ’‘ Note: This page shows 9 carefully selected questions Each question includes detailed explanations and matches the real OCA exam format.

Question 1 EASY πŸ“Œ Varargs Syntax
Which is a valid varargs method declaration?
A. public void display(int values...)
B. public void display(int... values)
C. public void display(...int values)
D. public void display(int values[])

βœ… Correct Answer: B

The correct syntax is 'type... variableName'. The ellipsis (...) must appear between the type and variable name, like int... values.

πŸ“– Detailed Explanation:

Option A: public void display(int values...) - INCORRECT. The ellipsis is placed AFTER the variable name. Correct syntax requires ellipsis between type and name. This causes compilation error: 'invalid method declaration; return type required'. The compiler interprets 'values...' as invalid syntax.

Option B: public void display(int... values) - CORRECT. This is the proper varargs syntax. Type (int) followed by ellipsis (...) followed by variable name (values). The ellipsis is correctly positioned between type and name. Inside the method, values is treated as int[].

Option C: public void display(...int values) - INCORRECT. The ellipsis appears BEFORE the type. This is completely invalid syntax. Compilation error: 'illegal start of type'. Java doesn't recognize ... as valid at this position.

Option D: public void display(int values[]) - INCORRECT. This declares a regular array parameter, NOT varargs. Syntax is valid but it's an array, not varargs.

Question 2 EASY πŸ“Œ Varargs as Arrays
What will be printed by:

public class Sample {
  static void displayValues(int... values) {
    System.out.println(values.length);
  }

  public static void main(String[] args) {
    displayValues(5, 10, 15);
  }
}
A. 0
B. 1
C. 3
D. Compilation error

βœ… Correct Answer: C

The varargs parameter is treated as an array inside the method. Since three arguments (5, 10, 15) are passed, values.length returns 3.

πŸ“– Detailed Explanation:

The output is: 3

You are calling:
displayValues(5, 10, 15);

The method signature is:
static void displayValues(int... values)

A varargs parameter (int...) collects all passed arguments into an array.
So here: values = {5, 10, 15}

Which means: values.length == 3

Thus the print statement:
System.out.println(values.length);

prints: 3

Question 3 EASY πŸ“Œ ArrayList Package
Which package contains the ArrayList class in Java?
A. java.io
B. java.util
C. java.lang
D. java.collection

βœ… Correct Answer: B

ArrayList is part of the Collections Framework.

All core collection classes (ArrayList, HashMap, HashSet, etc.) live in java.util.

Therefore, the correct package is java.util.

πŸ“– Detailed Explanation:

ArrayList is a resizable array implementation of the List interface.

The entire Collections Framework (List, Set, Map, Queue, etc.) resides inside the java.util package.

Option A (java.io) is for input/output classes like File, PrintStream, Reader, etc.

Option C (java.lang) contains core classes like String, Math, Object β€” not collections.

Option D (java.collection) is not even a real package in Java.

Only java.util.ArrayList is correct and exists in the JDK.

Therefore, answer B is correct.

Question 4 EASY πŸ“Œ Array vs ArrayList
What is the main difference between an array and an ArrayList?
A. Arrays can grow dynamically
B. ArrayList is of fixed size
C. ArrayList can grow or shrink dynamically
D. Arrays store only primitive types

βœ… Correct Answer: C

ArrayList can grow and shrink dynamically as elements are added or removed, unlike arrays which have fixed size.

πŸ“– Detailed Explanation:

Unlike arrays which have a fixed length once declared, an ArrayList automatically resizes when new elements are added or removed.

This makes it more flexible for collections where the number of elements can change.

Key differences between Array and ArrayList:

Size: Arrays have fixed size. ArrayList has dynamic size

Type: Arrays can store primitives and objects (int[], String[]).

ArrayList can only store objects (ArrayList, not ArrayList).

Primitives must be wrapped (Integer, not int).

Syntax: Arrays use brackets (arr[0], arr.length). ArrayList uses methods (list.get(0), list.size()).

Performance: Arrays are faster for fixed-size collections.

ArrayList has overhead for resizing but more convenient.

Functionality: Arrays have basic operations only.

ArrayList has rich API (add, remove, contains, indexOf, etc.).

Memory: Arrays are more memory efficient.

ArrayList uses more memory due to internal array and metadata.

Option A is incorrect - Arrays cannot grow dynamically. Once created, array size is fixed.
Option B is incorrect - ArrayList is NOT fixed size, it grows dynamically.
Option D is incorrect - Arrays can store both primitives and objects, not just primitives.

Question 5 MEDIUM πŸ“Œ Inheritance and Access Modifiers
Given the following code:

1. class Demo {
2. public static void main(String args[]) {
3. Parent ref = new Child();
4. //insert here
5. }
6. }
7.
8. class Parent {
9. protected int value = 15;
10. static int count = 30;
11. }
12.
13. class Child extends Parent {}

Which of the following inserted at line 4, will produce a compile time error?
A. System.out.print(ref.value);
B. System.out.print(Child.count);
C. System.out.print(ref.count);
D. System.out.print(Parent.value);
E. None of above

βœ… Correct Answer: D

Option D fails . Value is an instance variable, not a static variable.
Parent.value tries to access an instance variable using class syntax, which is illegal.
Instance variables must be accessed through an object reference, not a class name.

πŸ“– Detailed Explanation:

The value field in Parent is a protected instance variable, meaning it belongs to objects, not the class itself.
Accessing instance fields must always be done using an object reference like ref.value, not using Parent.value.
count is static, so it can be accessed with either Child.count or ref.count (even though ref.count is discouraged, it compiles).
Option A compiles because value is inherited and accessible through the object reference ref.
Option B compiles because count is static and inherited by Child.
Option C compiles because static fields can be accessed via objects, even though it's poor style. Option D fails because it violates static access rules.

Question 6 MEDIUM πŸ“Œ Varargs Rules
Which statement about varargs is TRUE?
A. A method can have multiple varargs parameters
B. Varargs can be declared anywhere in the parameter list
C. Varargs must be the last parameter
D. Varargs must be the first parameter

βœ… Correct Answer: C

A method may declare only one varargs parameter, and it must appear last in the parameter list. This is because the varargs parameter collects all remaining arguments, so placing anything after it would make the parameter list ambiguous and cause a compilation error.

πŸ“– Detailed Explanation:

In Java, a method can have only one varargs parameter, and it must appear last in the parameter list.
This rule exists because varargs absorb all remaining arguments into an array.
If varargs were not last, the compiler would not know how many arguments belong to it and how many belong to subsequent parameters.
For example:

void test(int... nums, String s) {} // invalid


because nums would capture everything, leaving nothing predictable for s.
Correct declarations place varargs at the end, such as:

void test(String prefix, int... values); // valid


Therefore, options A, B, and D violate Java's varargs rules, making C the only correct choice.

Question 7 MEDIUM πŸ“Œ ArrayList Remove Method
What happens when you call remove(1) on an ArrayList containing [10, 20, 30]?
A. Removes value 10
B. Removes value 20
C. Removes index 0
D. Throws an exception

βœ… Correct Answer: B

remove(1) removes the element at index 1, which is 20. For ArrayList, int literals are interpreted as indices, not values.

πŸ“– Detailed Explanation:

ArrayList has two remove() methods: remove(int index) removes element at specified index, and remove(Object o) removes first occurrence of specified object. When an int literal is passed to remove() on ArrayList, Java interprets it as an index, not a value.


What happens here

Since you’re passing an int literal (1), Java chooses:

remove(int index), not remove(Object o).

That means:
It removes the element at index 1, i.e. the second element (20).

Result

After the call:

list = [10, 30]

Question 8 MEDIUM πŸ“Œ ArrayList Thread-Safety
Which of these is true about ArrayList synchronization?
A. ArrayList is thread-safe by default
B. ArrayList is synchronized internally
C. ArrayList is not synchronized by default
D. ArrayList uses ConcurrentLock

βœ… Correct Answer: C

ArrayList is not thread-safe unless you explicitly wrap it.

It does not synchronize internally.

It also does not use locks like ConcurrentLock.

πŸ“– Detailed Explanation:

Java’s ArrayList is designed for fast, single-threaded access. ArrayList is not thread-safe by default. Multiple threads accessing the same ArrayList concurrently can cause data corruption or Concurrent Modification Exception.

To make an ArrayList thread-safe, you must wrap it:

List list = Collections.synchronizedList(new ArrayList<>());

Option A is incorrect - ArrayList is NOT thread-safe by default.
Option B is incorrect - ArrayList is NOT synchronized internally, unlike Vector which is.
Option C is CORRECT - ArrayList is not synchronized, requires external synchronization.
Option D is incorrect - ArrayList doesn't use ConcurrentLock. Some concurrent collections use locks, but ArrayList doesn't.

Question 9 HARD πŸ“Œ Access Modifiers Rules
Which statement is correct about access modifiers in Java?
A. The access modifier "private" can be used with every class
B. The access modifier "protected" can be used with every class
C. The only access modifier which can be used inside a method is public
D. Default access level is less restrictive than the protected access level
E. None of above

βœ… Correct Answer: E

None of the statements are correct based on Java's access modifier rules.

πŸ“– Detailed Explanation:

Option A is false because the private modifier cannot be applied to top-level classes (classes defined in their own .java file). Top-level classes can only be public or have default (package-private) access. private can be used with inner or nested classes, but not with every class.

Option B is false because the protected modifier cannot be applied to top-level classes.

Option C is false because access modifiers cannot be used inside a method body at all. Local variables within a method have no access modifiers and are scoped only to that method. You cannot write private int x = 5; or public int y = 10; inside a method body - this causes compilation errors.

Option D is false because default access (package-private) is MORE restrictive than protected, not less. Default access is accessible only within the same package, while protected access is accessible within the same package AND by subclasses in different packages through inheritance.

Therefore, the correct answer is E - None of the above statements are correct.

Want More Practice Questions?

Access all 1624 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 β†’