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
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.
What is the ability of different classes to respond to the same method call known as?
Polymorphism
Abstraction
Inheritance
Encapsulation
Which statement is true when a superclass reference refers to a subclass object in Java?
It can access private variables of the subclass directly.
It can call any method that is in the superclass.
It must cast the superclass type to a subclass type to call any method.
It can use only new methods defined in the subclass.
If an abstract class Shape has a non-abstract method draw(), and Circle is a subclass that does not override draw(), what happens when (Shape)new Circle().draw()
is executed?
A compile-time error for not overriding an abstract method in Circle.
The Shape class's draw() method executes without issue.
Runtime exception due to trying to instantiate an abstract class.
Nothing; there is no output or action performed as Circle overrides draw().
In a game where Player is an abstract class and Archer and Knight are subclasses, which approach best utilizes polymorphism when implementing attack behaviors specific to archers and knights?
Placing concrete implementation of attack() in Player assuming generic attacking behavior for all players.
Declaring static attack methods inside Archer and Knight classes bypassing inheritance hierarchy altogether.
Defining an abstract attack() method in Player and overriding it in Archer and Knight with particular behaviors.
Creating multiple overloaded versions of attack() within Player based on weapon types used by archers or knights.

How are we doing?
Give us your feedback and let us know how we can improve
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)
In Java, why would you choose composition over inheritance when designing long-term evolutionary software systems?
Inheritance guarantees constant time access to base class methods, which is an important performance measure.
Inheritance prevents creating too many class hierarchies, thus reducing complexity.
Composition provides better modularity, leaving the system more open for future changes.
Static enforces strict IS-A relationships between classes with rigid boundary definitions.
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();