🎯 OCA Java Practice Test - 5

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

πŸ“š OCA Core OOP: Polymorphism, Inheritance, Casting & Overriding

πŸ“š Topics Covered in Practice Test 5

Question 1 EASY πŸ“Œ Polymorphic References and Type Compatibility
Which of the following statements can be inserted in the blank line so that the code will compile successfully? (Choose all that apply)

public interface CanSwim {}
public class Fish implements CanSwim {
public static void main(String[] args) {
_____ aquatic = new GoldfishSpecies();
}
}
public class GoldfishSpecies extends Fish {}
A. Fish
B. GoldfishSpecies
C. TropicalAngelfish
D. CanSwim
E. Object
F. Integer

βœ… Correct Answer: "A, B, D, E"

Valid assignments are Fish (superclass), GoldfishSpecies (same type), CanSwim (implemented interface), and Object (universal superclass).

πŸ“– Detailed Explanation:

Fish (Option A): GoldfishSpecies extends Fish, so GoldfishSpecies is-a Fish. A superclass reference can point to a subclass object, enabling polymorphism.

GoldfishSpecies (Option B): A reference of the same type as the object being instantiated is always valid in Java.

CanSwim (Option D): GoldfishSpecies indirectly implements CanSwim because GoldfishSpecies extends Fish, and Fish implements CanSwim. A reference of an interface type can point to an instance of any class that implements the interface.

Object (Option E): Since Object is the universal parent class of all Java classes, any instance of GoldfishSpecies can be assigned to an Object reference.

TropicalAngelfish (Option C) is invalid because this class is not defined anywhere in the provided code.

Integer (Option F) is invalid because Integer is a numeric wrapper class and has no relationship to GoldfishSpecies, causing a compilation error.

Question 2 EASY πŸ“Œ Method Overloading vs Overriding
What is the result of the following code?

public class Runner {
public static void main(String args[]) {
Base ref = new Sub();
System.out.println(ref.process(3.5, 4.2));
}
}

class Base {
public int process(int x, int y) {
return x + y;
}
}

class Sub extends Base {
public double process(double x, double y) {
return x + y;
}
}
A. Prints 7.7
B. Prints 7
C. Compilation fails due to method not found in Base
D. Runtime exception is thrown
E. Prints 0

βœ… Correct Answer: C

Method resolution for overloading happens at compile-time based on reference type, and Base doesn't have process(double, double).

πŸ“– Detailed Explanation:

Correct Answer: C β€” Compilation fails due to method not found in Base

Detailed Explanation
ref is declared as type Base :- Base ref = new Sub();

Even though the object is Sub, the reference type controls which methods are visible at compile time.

The method call is:
ref.process(3.5, 4.2);

Which method does the compiler look for? Since ref is of type Base, the compiler searches only in Base for a method:
process(double, double)

But in Base, we have only:- public int process(int x, int y)

No method that accepts (double, double) exists in Base. Does Java consider the method in Sub? No.

Method resolution for overloading is done at compile time, based on the reference type, not the object type.
So even though Sub defines:- public double process(double x, double y), the compiler does not consider it, because ref is typed as Base.

Result :- Compilation error: Cannot find method process(double, double) in Base


Final Answer: C β€” Compilation fails due to method not found in Base

Question 3 EASY πŸ“Œ Method Overriding Rules
Consider the following code:

class Parent {
protected void execute() {
System.out.print("Parent");
}
}
class Child extends Parent {
// override execute() here
}

Which of the following is a valid way to override execute() in class Child?
A. public final void execute(){}
B. private void execute(){}
C. void execute(){}
D. public void execute(int num){}
E. protected int execute(){}

βœ… Correct Answer: A

Valid overrides must maintain or increase visibility (protected→public/protected) and keep the same signature with void return type.

πŸ“– Detailed Explanation:

In Java, method overriding requires the method name, parameter list, and return type to match exactly between the superclass and subclass (except when covariant returns are allowed for reference types). The overriding method must also have the same or more permissive access modifier, and it cannot reduce visibility.

Option A (public final void execute()) - Valid override.
The subclass method has the same signature and a more permissive access modifier (public instead of protected). Adding final is legal β€” it only prevents further overriding in subclasses. This is a proper override.

Option B (private void execute()) - Invalid.
private is more restrictive than protected. A subclass cannot reduce visibility when overriding a method. This causes a compilation error.

Option C (void execute()) - Invalid.
A method without an explicit modifier is package-private (default access), which is also more restrictive than protected. Reducing visibility is not allowed, so this is not a valid override.

Option D (public void execute(int num)) - Invalid.
The parameter list differs (int num instead of none), so this is method overloading, not overriding.

Option E (protected int execute()) - Invalid.
The return type differs (int instead of void). Primitive types and void cannot use covariant returns. This causes a compilation error.

Final Answer: Only Option A is correct.

Question 4 EASY πŸ“Œ Private Method Access and Inheritance
What is the result of the following code?

class Base {
private final void display() {
System.out.print("class Base");
}
}
class Derived extends Base {
private void display() {
System.out.print("class Derived");
}
}
class TestRunner {
public static void main(String[] args) {
Base obj = new Derived();
obj.display();
}
}
A. class Base
B. class Derived
C. Compilation fails due to private access
D. Runtime exception occurs
E. No output

βœ… Correct Answer: C

The private method display() in Base is not accessible from outside the class, causing a compilation error.

πŸ“– Detailed Explanation:

Private methods are not inherited by subclasses, so Derived is not overriding the display() method from Base - it's defining its own separate private method with the same name.

The final keyword in Base's method is redundant since private methods cannot be overridden anyway (they're not visible to subclasses).

When obj.display() is called on line 16, the compiler looks for a display() method in class Base based on the reference type.

In Base, display() is private, which means it's not accessible from outside the class - not even from a subclass .

Therefore, the code produces a compile-time error: 'display() has private access in Base'. The method cannot be accessed from the main() method because of its private access modifier.

Question 5 MEDIUM πŸ“Œ OCA Java 8 Fundamentals
What is the output?

public class Q309Char {
public static void main(String[] args) {
char c = 'C';
c++;
System.out.print(c);
}
}
A. C
B. D
C. 67
D. Compilation error
E. Runtime exception

βœ… Correct Answer: B

char increments by unicode -> 'D'

πŸ“– Detailed Explanation:

In Java, the char type is a 16-bit unsigned integer representing a Unicode code point. When incremented, the operation applies to the underlying numeric value, not the displayed character. For example, 'C' (Unicode 67) becomes 'D' (Unicode 68). This behavior highlights that char arithmetic operates like integer arithmetic. This concept is especially useful when iterating through character sequences, as it manipulates the code point values directly. Understanding this numeric nature of chars is critical when mixing them with other numeric data types.

Question 6 MEDIUM πŸ“Œ OCA Java 8 Fundamentals
What is printed?

public class Q328SB {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Cluster");
sb.append("Micro");
System.out.print(sb.length() + sb.capacity());
}
}
A. 18
B. 24
C. 35
D. Compilation error
E. 9

βœ… Correct Answer: C

Correct Answer: 35

πŸ“– Detailed Explanation:

StringBuilder sb = new StringBuilder("Cluster");

"Cluster" length = 7

StringBuilder initial capacity formula:

capacity = original_length + 16 = 7 + 16 = 23

So initially: length = 7, capacity = 23

2. Append "Micro"

sb.append("Micro");

"Micro" length = 5; New length = 7 + 5 = 12

Capacity check: 12 < 23 β†’ no expansion

So: final length = 12, final capacity = 23

3. System.out.print(sb.length() + sb.capacity());

Numeric addition: 12 + 23 = 35

Correct Answer: 35

Question 7 MEDIUM πŸ“Œ Polymorphism
What is the output of the following code involving polymorphism?

class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound();
}
}
A. Generic animal sound
B. Woof!
C. Compilation error
D. Runtime exception
E. No output

βœ… Correct Answer: B

The myDog variable is of type Animal but references a Dog object, and due to runtime polymorphism, the overridden makeSound method in Dog is called. This dynamic method dispatch results in "Woof!" being printed. No errors occur since the method is correctly overridden.

πŸ“– Detailed Explanation:


The reference type is:
Animal

But the actual object created is:
new Dog()

At runtime, Java uses the actual object type, not the reference type, to decide which method to call.So:

myDog.makeSound(); executes Dog's overridden method, not Animal's.

Final Output
Woof!

Question 8 MEDIUM πŸ“Œ OCA Java 8 Fundamentals
What is the output

import java.time.*;
public class Q377Date {
public static void main(String[] args) {
LocalDate d = LocalDate.ofYearDay(2015, 363);
d.plusWeeks(2);
System.out.print(d + ":" + d.isLeapYear());
}
}
A. 2015-12-29:false
B. 2016-01-12:true
C. 2015-12-29:true
D. Compilation error
E. Runtime exception

βœ… Correct Answer: A

LocalDate is immutable; plusWeeks returns new object; 2015-12-29 not leap year.

πŸ“– Detailed Explanation:

LocalDate.ofYearDay(2015, 363) creates the date for the 363rd day of 2015. Since 2015 is not a leap year (365 days), day 363 corresponds to December 29, 2015.

The date d now holds: 2015-12-29

Next, the code calls d.plusWeeks(2); β€” but LocalDate is immutable, meaning this does not modify d.

Because the result of plusWeeks() is not stored in any variable, the original d remains unchanged.

Calling d.isLeapYear() checks if the year 2015 is a leap year, and 2015 is not a leap year.

Therefore, the output combines the unchanged date and the leap-year check: 2015-12-29:false

Question 9 HARD πŸ“Œ Upcasting
Which of the following is true about upcasting in Java?
A. It requires explicit casting
B. It assigns a subclass object to a superclass reference
C. It can cause a ClassCastException
D. It prevents method overriding
E. It makes the object immutable

βœ… Correct Answer: B

Upcasting involves assigning a subclass object (e.g., Dog) to a superclass reference (e.g., Animal), as shown in the content’s example Animal animal = new Dog(). It is implicit, requiring no explicit casting, and does not cause a ClassCastException since it is always safe. Upcasting enables polymorphism but does not prevent overriding or affect immutability.

πŸ“– Detailed Explanation:

Upcasting involves assigning a subclass object (e.g., Dog) to a superclass reference (e.g., Animal), as shown in the content’s example Animal animal = new Dog(). It is implicit, requiring no explicit casting, and does not cause a ClassCastException since it is always safe. Upcasting enables polymorphism but does not prevent overriding or affect immutability.

Question 10 HARD πŸ“Œ Downcasting
What happens if you attempt to downcast a Vehicle reference to a Car when the object is actually a Bike?

class Vehicle {}
class Car extends Vehicle {}
class Bike extends Vehicle {}

public class Main {
public static void main(String[] args) {
Vehicle v = new Bike();
Car c = (Car) v; // Downcast attempt
}
}
A. The code compiles and runs successfully
B. A compilation error occurs
C. A ClassCastException is thrown
D. The object is incorrectly downcast at runtime
E. No output is produced

βœ… Correct Answer: C

The compiler allows the cast because Car and Bike share a parent (Vehicle).

But at runtime, the actual object is a Bike, so casting it to a Car causes a ClassCastException.

πŸ“– Detailed Explanation:

The reference Vehicle v = new Bike(); stores a Bike object in a Vehicle variable.

Downcasting to Car compiles because the type hierarchy allows it (Car and Bike are related through Vehicle).

But Java performs runtime type checking to ensure the object is truly a Car.

Since the actual object is a Bike, the cast is invalid.

The JVM detects the mismatch and throws a ClassCastException.

Thus the program compiles successfully but fails at runtime.

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 β†’