Java: Inheritance, Exceptions and APIs certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Java: Inheritance, Exceptions and APIs exam and earn Java: Inheritance, Exceptions and APIs certificate.
Table of Contents
- Question 1
- Answer
- Explanation
- Question 2
- Answer
- Explanation
- Question 3
- Answer
- Explanation
- Question 4
- Answer
- Explanation
- Question 5
- Answer
- Explanation
- Question 6
- Answer
- Explanation
- Question 7
- Answer
- Explanation
- Question 8
- Answer
- Explanation
- Question 9
- Answer
- Explanation
- Question 10
- Answer
- Explanation
- Question 11
- Answer
- Explanation
- Question 12
- Answer
- Explanation
Question 1
Which of the following is the most significant benefit provided by Java’s inheritance mechanism?
A. It guarantees that all methods declared in the superclass are automatically made private in the subclass.
B. It eliminates the need for the Garbage Collector by assigning fixed memory to all objects.
C. It allows a subclass to reuse fields and methods from a superclass, promoting code reusability and minimizing redundancy.
D. It enforces strict compile-time checking that prevents any method from being overridden.
Answer
C. It allows a subclass to reuse fields and methods from a superclass, promoting code reusability and minimizing redundancy.
Explanation
The main benefit of inheritance in OOP is code reuse. A subclass automatically gains all non-private members of its superclass, reducing the need to write the same code multiple times. The primary advantage of inheritance is code reusability, which allows new classes to adopt the attributes and behaviors of existing classes without rewriting code. By extending a superclass, the subclass automatically inherits all non-private fields and methods, enabling developers to build upon existing logic and create a cleaner, hierarchical structure that reduces redundancy.
Question 2
Given the inheritance structure where Cat extends Animal, and both classes have an overridden makeSound() method. What core principle of polymorphism is demonstrated by the line Animal a = new Cat(); followed by a.makeSound();?
A. Dynamic method dispatch, where the actual object type determines the method executed at run time.
B. Compile-time polymorphism (Method Overloading).
C. The reference variable determines which version of the method is executed.
D. Static linking, where the Animal class version of the method is always executed.
Answer
A. Dynamic method dispatch, where the actual object type determines the method executed at run time.
Explanation
This is the exact definition of run-time polymorphism: the JVM uses the actual object type on the heap (Cat) to dynamically determine which version of the overridden method to execute, leading to different behaviors from a single method call. This scenario demonstrates runtime polymorphism, specifically via dynamic method dispatch. When you invoke an overridden method like makeSound() on a reference variable, Java determines which version of the method to execute based on the actual object type (in this case, Cat) residing in memory, not the reference type (Animal). This allows the program to exhibit the specific behavior of the subclass at runtime even when accessed through a generic parent reference.
Question 3
Examine the following line of Java code. What is the reference type and what is the object type in this statement?
Object ref = new String("Hello");
A. Reference Type: String; Object Type: Object.
B. Reference Type: Object; Object Type: String.
C. Both the Reference Type and the Object Type are String.
D. Both the Reference Type and the Object Type are Object.
Answer
B. Reference Type: Object; Object Type: String.
Explanation
The Reference Type (or declared type) is determined by the variable type on the left side of the assignment (Object). The Object Type (or actual type) is determined by the class instantiated with the new keyword (String). In the statement Object ref = new String(“Hello”);, the variable ref is explicitly declared as type Object, making it the reference type used by the compiler to enforce type safety. However, the new keyword instantiates the actual object in heap memory as a String, which is the runtime object type. This separation allows for polymorphism, where a parent reference can hold a child object.
Question 4
Which statement correctly describes the difference between Upcasting and Downcasting for object references in Java?
A. Upcasting requires an explicit cast and is checked at runtime, while Downcasting is automatic and safe.
B. Upcasting is automatic and moves a reference from a subclass to a superclass type.
C. Downcasting is automatic and moves a reference from a superclass to a subclass type.
D. Both Upcasting and Downcasting are always safe and require an explicit cast operator.
Answer
B. Upcasting is automatic and moves a reference from a subclass to a superclass type.
Explanation
Upcasting (e.g., Object obj = new String(“Hi”);) is always safe because a subclass object is always a valid instance of its superclass. The compiler allows this implicitly (automatically) without a cast. Upcasting occurs when a subclass reference is assigned to a superclass variable (e.g., Animal a = new Cat();), which Java handles automatically because a subclass is guaranteed to define everything the superclass has. In contrast, downcasting (moving from superclass to subclass) is not automatic; it requires an explicit cast operator and carries the risk of a ClassCastException if the object is not actually an instance of the target subclass.
Question 5
Examine the following code. Which line correctly calls the display() method defined in the Parent class from within the Child object’s overridden display() method?
class Parent {
public void display() { System.out.println("Parent"); }
}
class Child extends Parent {
@Override
public void display() {
// Correct call to Parent's display() method
// Line 1: ???
System.out.println("Child");
}
}
A. this.display();
B. Parent.display();
C. super.display();
D. new Parent().display();
Answer
C. super.display();
Explanation
The super keyword provides a special reference that allows an object to explicitly access a member (method or field) defined in its immediate superclass (the Parent class), even if that member has been overridden in the current class. This is the correct and idiomatic way to invoke the parent’s implementation. To call the overridden version of a method residing in the parent class, Java uses the super keyword. Writing super.display() inside the Child class specifically targets the display() implementation in Parent, bypassing the Child’s own overriding method. Options like this.display() would cause a recursive stack overflow, and Parent.display() is syntactically incorrect for instance methods.
Question 6
Examine the code snippet. What will be the output of the expression obj.print()?
class A {
int value = 10;
public void print() { System.out.print("A:" + value); }
}
class B extends A {
int value = 20;
public void print() { System.out.print("B:" + value); }
}
public class Test {
public static void main(String[] args) {
A obj = new B();
obj.print();
}
}
A. A:10
B. B:20
C. A:20
D. B:10
Answer
B. B:20
Explanation
Method Resolution (Dynamic/Polymorphism): The call obj.print() is resolved based on the actual object type, which is B. Therefore, the B.print() method executes. Field Resolution (Static/Hiding): Inside the B.print() method, the unqualified access to value refers to the field within the current class’s scope, which is B’s field (value $= 20$). The output is thus “B:20”. When obj.print() is called, runtime polymorphism ensures the overridden print() method in class B is executed. Inside class B’s print() method, the variable value resolves to the instance variable declared within class B (which is 20), because variable access in Java is not polymorphic but determined by the scope in which the code is written. Therefore, the method prints “B:” followed by B’s local value of 20.
Question 7
Examine the following code. Which line represents a valid method overriding of the process() method?
class Vehicle {
protected Object process() throws Exception { return null; }
}
class Car extends Vehicle {
// A: public String process() throws IOException { return ""; }
// B: public void process() { }
// C: protected Object process(String s) { return null; }
// D: public String process() throws RuntimeException { return ""; }
}
A. Method A: public String process() throws IOException
B. Method B: public void process()
C. Method C: protected Object process(String s)
D. Method D: public String process() throws RuntimeException
Answer
D. Method D: public String process() throws RuntimeException
Explanation
All Rules Followed. 1. Access: public is wider than protected (legal). 2. Return Type: String is a subclass (subtype) of Object (legal covariant return). 3. Exceptions: RuntimeException is an unchecked exception, which can be added, removed, or changed freely in an overriding method (legal).
Question 8
Examine the following code snippet. Which line will compile without error, but potentially result in a ClassCastException at runtime?
class Animal { /* ... */ }
class Dog extends Animal { /* ... */ }
class Cat extends Animal { /* ... */ }
Animal a = new Dog();
// Line 1:
Cat c1 = (Cat) a;
// Line 2:
Animal a2 = new Cat();
// Line 3:
Dog d1 = new Animal();
// Line 4:
Cat c2 = a2;
A. Line 2: Animal a2 = new Cat();
B. Line 3: Dog d1 = new Animal();
C. Line 1: Cat c1 = (Cat) a;
D. Line 4: Cat c2 = a2;
Answer
C. Line 1: Cat c1 = (Cat) a;
Explanation
This is an explicit Downcast. The cast to (Cat) satisfies the compiler because Cat and Dog share a common ancestor (Animal). However, at runtime, the object stored in a is actually a Dog, not a Cat, so the cast fails, throwing a ClassCastException. Line 1 compiles successfully because a is of type Animal, and casting an Animal to a Cat is syntactically valid inheritance logic. However, at runtime, the actual object stored in a is a Dog (from Animal a = new Dog();). Since a Dog cannot be cast to a Cat, the Java Virtual Machine will throw a ClassCastException at this line.
Question 9
Examine the class below. What is the correct use and placement of the this() keyword within a constructor of the same class?
public class Engine {
private int rpm;
private String type;
// Constructor 1
public Engine(int rpm) { /*...*/ }
// Constructor 2
public Engine(int rpm, String type) { /*...*/ }
}
A. It can be used anywhere in the body of Constructor 2 to call Constructor 1, as long as it is before any other code.
B. It is used in Constructor 1 as the first statement to call Constructor 2 and must pass the required arguments.
C. It is used to call the superclass constructor, which is synonymous with super(), and can be the first or second statement.
D. It can be used multiple times within a single constructor to call several different constructors.
Answer
B. It is used in Constructor 1 as the first statement to call Constructor 2 and must pass the required arguments.
Explanation
The this() keyword is used for constructor chaining within the same class. It must correctly match the parameters of the target constructor and is required to be the first executable statement in the calling constructor. The this() statement is used for constructor chaining, allowing one constructor to call another within the same class to reduce code duplication. The strict rule for using this() is that it must be the very first statement in the constructor body. If used, it delegates initialization to the other constructor before executing the rest of its own code.
Question 10
Examine the following code snippet. Which line correctly calls the parent class’s constructor from within the subclass’s constructor?
class Parent {
public Parent(String name) { /* ... */ }
}
class Child extends Parent {
public Child(String name) {
// Which statement is correct?
}
}
A. super(name);
B. this(name);
C. super.Parent(name);
D. Parent.super(name);
Answer
A. super(name);
Explanation
The super(arguments) syntax is the only correct way to explicitly invoke a constructor of the immediate superclass (Parent). This call must be the very first statement inside the subclass constructor. In a subclass constructor, super() is used to explicitly invoke a constructor from the parent class. To pass the name argument up to the Parent constructor, super(name); must be placed as the first statement in the Child constructor. This ensures the parent part of the object is fully initialized before the child class attempts to initialize its own fields.
Question 11
Which statement correctly identifies a key difference in the members allowed inside a Java interface compared to an abstract class?
A. An abstract class can define constructors, but an interface cannot.
B. An abstract class can only have public members, whereas an interface can have protected members.
C. An interface can contain instance fields (non-static), but an abstract class cannot.
D. Both abstract class and interface can be instantiated using the new keyword.
Answer
A. An abstract class can define constructors, but an interface cannot.
Explanation
An abstract class can have constructors, which are necessary to initialize the common state of its concrete subclasses. An interface, however, is a contract and cannot be instantiated, so it is forbidden from defining constructors. A key structural difference is that abstract classes are classes and can therefore have constructors (used by subclasses via super()), whereas interfaces are purely contract definitions and cannot have constructors. While modern Java interfaces can have private and default methods, they still cannot hold state-initializing constructors or instance fields (other than static final constants), unlike abstract classes.
Question 12
Examine the constructor method below. What is the main purpose of using the this keyword in the line this.value = value;?
public class Box {
private int value;
public Box(int value) {
this.value = value;
}
}
A. To create a new Box object instance before assigning the value.
B. To allow the value variable to be accessed from outside the Box class.
C. To convert the primitive int parameter into an Integer wrapper object.
D. To distinguish between the instance variable (this.value) and the local parameter (value) that have the same name.
Answer
D. To distinguish between the instance variable (this.value) and the local parameter (value) that have the same name.
Explanation
When a local variable (the parameter value) or method parameter has the same name as a field (the instance variable value), the local variable shadows the field. The this keyword is required to explicitly access the instance variable of the current object and prevent the program from simply assigning the parameter value to itself. In the provided constructor, the parameter name value shadows the class’s instance variable value. The keyword this is used to explicitly refer to the current object’s instance field (this.value), allowing the compiler to differentiate it from the local variable value passed into the constructor. Without this, the assignment value = value; would essentially do nothing by assigning the parameter to itself.