Inheritance in Object-Oriented Programming
Given the following code:
class A {
public void show() {
System.out.println("A");
}
}
class B extends A {
public void show() {
System.out.println("B");
}
}
What will be the output when the following code is executed?
A obj1 = new A();
B obj2 = new B();
A obj3 = new B();
...
A B A
A B B
A A B
It will throw a compilation error
Assume the following code is valid. What is the static type and dynamic type of the variable "ref"?
G ref = new H();
Static type: G, Dynamic type: H
Static type: H, Dynamic type: G
Static type: G, Dynamic type: G
Static type: H, Dynamic type: H
What is the ability of different classes to respond to the same method call known as?
Polymorphism
Abstraction
Inheritance
Encapsulation
In Java, can a subclass object be assigned to a superclass reference variable?
No, it is not allowed, and attempting to do so will result in a run-time error.
No, it is not allowed, and attempting to do so will result in a compilation error.
Yes, it is allowed and does not require any casting.
Yes, but it requires explicit casting to avoid compilation errors.
Given an array list 'animals' containing instances of various classes such as Dog, Cat, and Bird that all extend Animal and override speak(), what will be the Big O time complexity when sequentially calling speak() on each element?
O(n^2)
O(n)
O(log n)
O(1)
Suppose there is a Car class that extends the Vehicle class and whose constructor takes in the “brand” and “year” parameters. What is the static type and dynamic type of the variable "lambo" in the following code?
Car lambo = new Car(“Lamborghini”, 2005);
Static type: Car, Dynamic type: Car
Static type: Vehicle, Dynamic type: Car
Static type: Car, Dynamic type: Vehicle
Static type: Lamborghini, Dynamic type: Car
In a class hierarchy where Class B extends Class A, and Class C extends Class B, which method would be invoked if a method in Class A is overridden by both Classes B and C and an object of type C calls the method?
The method from Class B.
The original method from the Object class.
The method from Class C.
The method from Class A.

How are we doing?
Give us your feedback and let us know how we can improve
In Java, what indicates that polymorphism has been used effectively in code design?
Excessive use of instanceof operator before executing any operation involving inherited or overridden methods.
Objects are manipulated more generally rather than using specific behaviors tied strictly to their declared types.
Each subclass contains duplicated versions of inherited methods with slightly modified behavior instead of overriding them.
A chain of if-else statements determines which specific subclass methods should be invoked based on instance types.
Given a superclass Animal and subclasses Dog and Cat that override a makeSound() method, which line of code demonstrates polymorphism?
Animal pet = new Dog(); pet.makeSound();
Animal animal = new Animal(); animal.makeSound();
Dog dog = new Dog(); dog.makeSound();
Cat cat = new Cat(); cat.makeSound();
What is the purpose of polymorphism in object-oriented programming?
To store objects of different types as the same data type.
To create multiple classes that share the exact same code.
To allow for methods to be used on objects of different classes.
To ensure that all classes have the same methods and attributes.