10 Free Practice Questions for 1Z0-808 Certification
All 10 questions shown below with complete explanations
Valid assignments are Fish (superclass), GoldfishSpecies (same type), CanSwim (implemented interface), and Object (universal superclass).
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.
Method resolution for overloading happens at compile-time based on reference type, and Base doesn't have process(double, double).
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
Valid overrides must maintain or increase visibility (protectedβpublic/protected) and keep the same signature with void return type.
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.
The private method display() in Base is not accessible from outside the class, causing a compilation error.
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.
char increments by unicode -> 'D'
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.
Correct Answer: 35
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
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.
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!
LocalDate is immutable; plusWeeks returns new object; 2015-12-29 not leap year.
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
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.
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.
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.
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.
Access all 1624 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