Unit 3: Inheritance and Polymorphism - Practice Quiz

CSE310 — Programming In Java 63 Questions
0 Correct 0 Wrong 63 Left
0/63

1 Which keyword is used in Java to specify that a class is inheriting from another class?

Inheritance Easy
A. super
B. implements
C. extends
D. inherits

2 The relationship between a subclass and its superclass in Java is known as an...

Inheritance Easy
A. "is-a" relationship
B. "part-of" relationship
C. "has-a" relationship
D. "uses-a" relationship

3 What is it called when a subclass defines a method with the same name, return type, and parameters as a method in its superclass?

Method overriding Easy
A. Method Overloading
B. Method Duplication
C. Method Overriding
D. Method Hiding

4 Which annotation is commonly used to tell the compiler that you intend to override a method from a superclass?

Method overriding Easy
A. @Inherit
B. @Override
C. @FunctionalInterface
D. @Super

5 How can a subclass constructor call the constructor of its direct superclass?

super keyword Easy
A. Using parent()
B. Using this()
C. Using super()
D. It is called automatically and cannot be called explicitly

6 What is the purpose of super.myMethod() inside a subclass method?

super keyword Easy
A. To declare a new method in the superclass
B. To check if the superclass has myMethod
C. To call a static method from the superclass
D. To call the overridden version of myMethod from the superclass

7 Which class is the ultimate superclass of every class in Java?

Object class and overriding toString() and equals() method Easy
A. java.lang.Main
B. java.lang.Object
C. java.lang.System
D. java.lang.Class

8 What is the primary reason for overriding the toString() method?

Object class and overriding toString() and equals() method Easy
A. To convert an object to an integer
B. To compare two objects for equality
C. To provide a meaningful string representation of an object
D. To finalize an object before garbage collection

9 What is the effect of declaring a class with the final keyword?

Using super and final keywords Easy
A. All methods in the class are automatically final.
B. The class can only have one instance.
C. The class cannot be extended (subclassed).
D. The class cannot be instantiated.

10 If a method is declared as final, what does it mean?

Using super and final keywords Easy
A. It can only be called once.
B. It cannot be overridden by any subclass.
C. It becomes a static method.
D. It must return a value.

11 What type of value does the instanceof operator return?

instanceof operator Easy
A. An Object
B. A boolean
C. A String
D. An int

12 What does the expression myObject instanceof MyClass check?

instanceof operator Easy
A. If myObject is an instance of MyClass or one of its subclasses
B. If myObject has a method named MyClass
C. If MyClass is an instance of myObject
D. If myObject and MyClass are equal

13 Which keyword is used to declare a method that has no implementation body?

Abstract Class and Interface : Abstract method and abstract class Easy
A. empty
B. abstract
C. virtual
D. concrete

14 What is a key characteristic of an abstract class?

Abstract Class and Interface : Abstract method and abstract class Easy
A. It can only contain abstract methods.
B. It can be instantiated using the new keyword.
C. It cannot have a constructor.
D. It cannot be instantiated using the new keyword.

15 Which keyword is used for a class to inherit the methods from an interface?

Interfaces Easy
A. uses
B. extends
C. implements
D. inherits

16 Can a single class implement multiple interfaces in Java?

Interfaces Easy
A. No
B. Only if the interfaces have no methods
C. Yes
D. Only if the interfaces are in the same package

17 Starting from Java 8, a method in an interface can have an implementation if it is declared with which keyword?

static and default methods Easy
A. override
B. final
C. concrete
D. default

18 How is a static method inside an interface invoked?

static and default methods Easy
A. Directly using the Interface's name
B. Using the this keyword
C. It cannot be invoked
D. Through an instance of a class that implements the interface

19 What is the main purpose of overriding the equals() method from the Object class?

Object class and overriding toString() and equals() method Easy
A. To define a condition for logical equality between two objects
B. To check if two object references point to the same memory location
C. To create a copy of an object
D. To provide a custom string representation

20 A class that inherits from another class is called a...

Inheritance Easy
A. Parent Class
B. Base Class
C. Subclass
D. Superclass

21 Consider the following Java code snippet:

JAVA
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 Test {
    public static void main(String[] args) {
        Animal myPet = new Dog();
        myPet.makeSound();
    }
}



What is the output of this program?

Method overriding Medium
A. A runtime error occurs.
B. A compilation error occurs.
C. Generic Animal Sound
D. Woof

22 What is the output of the following Java code?

JAVA
class Parent {
    String name;
    Parent(String n) {
        this.name = n;
        System.out.print("Parent");
    }
}

class Child extends Parent {
    Child(String n) {
        super(n);
        System.out.print("Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child("Test");
    }
}

super keyword Medium
A. ParentChild
B. ChildParent
C. Parent
D. A compilation error because super() is called with an argument.

23 What happens when you try to compile and run the following code?

JAVA
abstract class Shape {
    Shape() {
        System.out.println("Shape constructor");
    }
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

public class Test {
    public static void main(String[] args) {
        Shape s = new Circle();
        s.draw();
    }
}

Abstract Class and Interface : Abstract method and abstract class Medium
A. Output:
Shape constructor
Drawing Circle
B. Compilation error: Abstract classes cannot have constructors.
C. Runtime error: Cannot instantiate an abstract class.
D. Output: Drawing Circle

24 Consider the Book class below. What will be the output of the main method?

JAVA
class Book {
    private String title;
    public Book(String title) { this.title = title; }

    // equals() and hashCode() are NOT overridden
}

public class Library {
    public static void main(String[] args) {
        Book b1 = new Book("Java Essentials");
        Book b2 = new Book("Java Essentials");
        Book b3 = b1;

        System.out.print(b1.equals(b2));
        System.out.print(b1.equals(b3));
    }
}

Object class and overriding toString() and equals() method Medium
A. truefalse
B. falsetrue
C. truetrue
D. falsefalse

25 Which of the following code snippets will result in a compilation error?

Using super and final keywords Medium
A.
JAVA
class A { final void method() {} }
class B extends A { void method() {} }

B.
JAVA
final class A { }
class B { A a = new A(); }

C.
JAVA
class A { final void method() {} }
class B extends A { }

D.
JAVA
class A { void method() {} }
final class B extends A { }

26 Analyze the following code. What is the output?

JAVA
interface Flyable {}
class Animal {}
class Bird extends Animal implements Flyable {}
class Fish extends Animal {}

public class Test {
    public static void main(String[] args) {
        Animal a = new Bird();
        boolean check1 = a instanceof Flyable;
        boolean check2 = a instanceof Bird;
        boolean check3 = a instanceof Animal;
        // boolean check4 = a instanceof Fish; // This would be false, but let's focus on the first three
        System.out.println(check1 + " " + check2 + " " + check3);
    }
}

instanceof operator Medium
A. true true false
B. A compilation error occurs.
C. true true true
D. false true true

27 What is the primary problem that interfaces solve in Java regarding inheritance?

Interfaces Medium
A. They provide a mechanism to achieve a form of 'multiple inheritance' of type, allowing a class to adhere to multiple contracts.
B. They enforce that all subclasses must have a specific set of constructors.
C. They allow a class to inherit state (instance variables) from multiple sources.
D. They allow a class to inherit implemented methods from multiple superclasses.

28 Given the following interface and class, which statement will compile successfully?

JAVA
interface Powertool {
    static String getCategory() {
        return "Tools";
    }
    default void start() {
        System.out.println("Starting tool...");
    }
}

class Drill implements Powertool {}

static and default methods Medium
A. Powertool.getCategory();
B. new Powertool().start();
C. Drill d = new Drill(); d.getCategory();
D. Drill.getCategory();

29 What is the result of compiling and running the following code?

JAVA
class A {
    int i = 10;
}

class B extends A {
    int i = 20;
}

public class Test {
    public static void main(String[] args) {
        A a = new B();
        System.out.println(a.i);
    }
}

Inheritance Medium
A. 20
B. A compilation error occurs.
C. A runtime exception is thrown.
D. 10

30 Which of the following method signatures is a valid override of the process() method in class Base?

JAVA
class Data { }
class SubData extends Data { }

class Base {
    public Data process() {
        return new Data();
    }
}

Method overriding Medium
A.
JAVA
class Derived extends Base {
    public Data process(int x) { return new Data(); }
}

B.
JAVA
class Derived extends Base {
    public Object process() { return new Object(); }
}

C.
JAVA
class Derived extends Base {
    public SubData process() { return new SubData(); }
}

D.
JAVA
class Derived extends Base {
    private Data process() { return new Data(); }
}

31 If a class does not override the toString() method, what will be the result of printing an object of that class?

Object class and overriding toString() and equals() method Medium
A. A compilation error, as toString() must always be overridden.
B. The memory address of the object.
C. The string "null".
D. A string representation consisting of the class name, an '@' sign, and the unsigned hexadecimal representation of the object's hash code.

32 What is the output of the following code snippet?

JAVA
class SuperClass {
    void printMethod() {
        System.out.print("Printed in SuperClass");
    }
}

class SubClass extends SuperClass {
    void printMethod() {
        super.printMethod();
        System.out.print(", Printed in SubClass");
    }
}

public class Main {
    public static void main(String[] args) {
        SubClass s = new SubClass();
        s.printMethod();
    }
}

super keyword Medium
A. Printed in SuperClass
B. Printed in SubClass
C. A compilation error occurs due to the use of super.
D. Printed in SuperClass, Printed in SubClass

33 What is the main purpose of declaring a class as final?

Using super and final keywords Medium
A. To ensure that all its methods are implicitly final.
B. To indicate that the class can only be used in a single-threaded environment.
C. To prevent the class from being extended (subclassed).
D. To prevent the class from being instantiated.

34 Consider the following code. What will be printed to the console?

JAVA
public class Test {
    public static void main(String[] args) {
        String s = "hello";
        Object o = s;
        if (o instanceof String) {
            System.out.print("Is String. ");
        }
        if (o instanceof Object) {
            System.out.print("Is Object. ");
        }
        if (s instanceof CharSequence) {
            System.out.print("Is CharSequence.");
        }
    }
}

instanceof operator Medium
A. A compilation error.
B. Is String.
C. Is String. Is Object.
D. Is String. Is Object. Is CharSequence.

35 Which statement best describes a key difference between an abstract class and an interface in Java (post-Java 8)?

Abstract Class and Interface : Abstract method and abstract class Medium
A. An interface cannot have any implemented methods, while an abstract class can.
B. An abstract class can have instance variables (state), while an interface cannot.
C. An interface can be instantiated, but an abstract class cannot.
D. A class can implement multiple abstract classes but only extend one interface.

36 What happens if a class implements two interfaces that both have a default method with the same signature?

JAVA
interface A { 
    default void show() { System.out.println("A"); } 
}
interface B { 
    default void show() { System.out.println("B"); } 
}
class C implements A, B {
    // What is required here?
}

static and default methods Medium
A. The code compiles, and the show() method from the first interface listed (A) is used.
B. The code fails to compile unless class C explicitly overrides the show() method.
C. The code compiles, and a call to show() will randomly choose one implementation.
D. The code fails to compile because a class cannot implement two interfaces with conflicting methods.

37 Examine the constructor chain in this code. What is the output?

JAVA
class Base {
    Base() {
        System.out.print("Base ");
    }
}

class Derived extends Base {
    Derived() {
        // Implicit super() call here
        System.out.print("Derived ");
    }
}

class MoreDerived extends Derived {
    MoreDerived() {
        System.out.print("MoreDerived ");
    }
}

public class Test {
    public static void main(String[] args) {
        new MoreDerived();
    }
}

Inheritance Medium
A. MoreDerived Derived Base
B. A compilation error occurs.
C. Base Derived MoreDerived
D. MoreDerived

38 Which of the following is an invalid declaration within a Java interface?

Interfaces Medium
A. default boolean isValid() { return true; }
B. void calculate();
C. protected void process();
D. public static final int MAX_VALUE = 100;

39 Which statement is true regarding abstract methods?

Abstract Class and Interface : Abstract method and abstract class Medium
A. An abstract method must be declared as final to prevent further changes.
B. An abstract method can provide a default implementation using curly braces {}.
C. An abstract method must be declared inside an interface; it cannot be in an abstract class.
D. A class containing one or more abstract methods must also be declared as abstract.

40 What is the output of the following Java program?

JAVA
class A {
    static void print() {
        System.out.println("A");
    }
}

class B extends A {
    static void print() {
        System.out.println("B");
    }
}

public class Test {
    public static void main(String[] args) {
        A a = new B();
        a.print();
    }
}

Method overriding Medium
A. A compilation error.
B. A
C. A runtime error.
D. B

41 Consider the following Java code snippet. What is the output when the main method is executed?

JAVA
class Vehicle {
    static void printType() {
        System.out.print("Vehicle ");
    }
    void printName() {
        System.out.print("GenericVehicle ");
    }
}

class Car extends Vehicle {
    static void printType() {
        System.out.print("Car ");
    }
    void printName() {
        System.out.print("MyCar ");
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle myVehicle = new Car();
        myVehicle.printType();
        myVehicle.printName();
    }
}

Method overriding Hard
A. Vehicle GenericVehicle
B. Vehicle MyCar
C. Car MyCar
D. Compilation Error

42 Analyze the code below. Which statement accurately describes the compilation and execution result?

JAVA
class Base {
    private final void process() {
        System.out.println("Base.process");
    }
    
    Base() {
        process();
    }
}

class Derived extends Base {
    void process() { // Note: This is NOT an override
        System.out.println("Derived.process");
    }
}

public class Main {
    public static void main(String[] args) {
        Base b = new Derived();
    }
}

Using super and final keywords Hard
A. The code fails to compile because private methods cannot be accessed in a constructor.
B. The code compiles and prints "Base.process".
C. The code compiles and prints "Derived.process".
D. The code fails to compile because process() in Derived cannot override the final method in Base.

43 What is the result of compiling and running the following code?

JAVA
interface I1 {
    default void display() { System.out.print("I1"); }
}

interface I2 {
    default void display() { System.out.print("I2"); }
}

class MyClass implements I1, I2 {
    @Override
    public void display() {
        I1.super.display();
    }
}

public class Test {
    public static void main(String[] args) {
        new MyClass().display();
    }
}

static and default methods Hard
A. I1
B. Compilation Error: Ambiguous method call.
C. I2
D. Runtime Exception

44 Examine this hierarchy. What will be printed to the console?

JAVA
abstract class Writer {
    public static void write() {
        System.out.print("Writing...");
    }
}

class Author extends Writer {
    public static void write() {
        System.out.print("Writing book...");
    }
}

public class Programmer extends Author {
    public static void write() {
        System.out.print("Writing code...");
    }

    public static void main(String[] args) {
        Author a = new Programmer();
        a.write();
    }
}

Abstract Class and Interface : Abstract method and abstract class Hard
A. Writing book...
B. Writing code...
C. Writing...
D. Compilation Error

45 Given the Point class, what is the output of the main method?

JAVA
import java.util.HashSet;
import java.util.Set;

class Point {
    private final int x, y;

    public Point(int x, int y) { this.x = x; this.y = y; }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Point)) return false;
        Point p = (Point) o;
        return p.x == x && p.y == y;
    }
    
    // hashCode() is NOT overridden
}

public class TestEquals {
    public static void main(String[] args) {
        Set<Point> set = new HashSet<>();
        Point p1 = new Point(1, 2);
        Point p2 = new Point(1, 2);

        set.add(p1);
        set.add(p2);

        System.out.println(set.size());
    }
}

Object class and overriding toString() and equals() method Hard
A. The code throws a runtime exception.
B. 0
C. 2
D. 1

46 What is the output of the following Java program?

JAVA
class SuperClass {
    int value;
    SuperClass() {
        this.value = 10;
        printValue();
    }
    void printValue() {
        System.out.print(value);
    }
}

class SubClass extends SuperClass {
    int value;
    SubClass() {
        super();
        this.value = 20;
    }
    @Override
    void printValue() {
        System.out.print(super.value + this.value);
    }
}

public class Main {
    public static void main(String[] args) {
        new SubClass();
    }
}

super keyword Hard
A. 100
B. 20
C. 10
D. 30

47 What is the output of the following Java program? Pay close attention to initialization order and polymorphic method dispatch from constructors.

JAVA
class SuperClass {
    String value = "10";
    SuperClass() {
        printValue();
    }
    void printValue() {
        System.out.print(value);
    }
}

class SubClass extends SuperClass {
    String value = "0";
    SubClass() {
        super();
        // Constructor Body
    }
    @Override
    void printValue() {
        System.out.print(super.value + this.value);
    }
}

public class Main {
    public static void main(String[] args) {
        new SubClass();
    }
}

super keyword Hard
A. 10null
B. 10
C. 100
D. null0

48 Given the following class and interface structure, what is the output of the main method?

JAVA
interface Flyable {}
class Animal {}
class Mammal extends Animal {}
class Bat extends Mammal implements Flyable {}
class Bird extends Animal implements Flyable {}

public class TestInstanceOf {
    public static void main(String[] args) {
        Animal a1 = new Bat();
        Animal a2 = null;
        Flyable f1 = new Bird();

        boolean r1 = a1 instanceof Mammal;
        boolean r2 = a2 instanceof Animal;
        boolean r3 = f1 instanceof Bat;
        boolean r4 = a1 instanceof Flyable;

        System.out.print("" + r1 + r2 + r3 + r4);
    }
}

instanceof operator Hard
A. truefalsetalsefalsetrue
B. truefalsetruetrue
C. truefalsetalsefalsetrue
D. truefalsetruefalse

49 Given the following class and interface structure, what is the output of the main method?

JAVA
interface Flyable {}
class Animal {}
class Mammal extends Animal {}
class Bat extends Mammal implements Flyable {}
class Bird extends Animal implements Flyable {}

public class TestInstanceOf {
    public static void main(String[] args) {
        Animal a1 = new Bat();
        Animal a2 = null;
        Flyable f1 = new Bird();

        boolean r1 = a1 instanceof Mammal;
        boolean r2 = a2 instanceof Animal;
        boolean r3 = f1 instanceof Bat;
        boolean r4 = a1 instanceof Flyable;

        System.out.print("" + r1 + r2 + r3 + r4);
    }
}

instanceof operator Hard
A. Compilation Error
B. truefalsetruetrue
C. truefalsetruefalse
D. truefalsefalsetrue

50 What is the result of attempting to compile and run the following code?

JAVA
interface Updatable {
    void update();
    int MAX_RETRIES = 3;
}

class Data implements Updatable {
    public int MAX_RETRIES = 5; // Hiding the field
    public void update() {
        System.out.println("Data updated with MAX_RETRIES = " + MAX_RETRIES);
    }
}

public class Test {
    public static void main(String[] args) {
        Updatable u = new Data();
        u.update();
        System.out.println("Accessing via interface ref: " + u.MAX_RETRIES);
    }
}

Interfaces Hard
A. Compilation Error because u.MAX_RETRIES is ambiguous.
B. Output:
Data updated with MAX_RETRIES = 5
Accessing via interface ref: 3
C. Compilation Error because a class cannot redefine an interface field.
D. Output:
Data updated with MAX_RETRIES = 5
Accessing via interface ref: 5

51 Consider the following code involving constructor chaining and instance initializers. What is the output?

JAVA
class Parent {
    String s = "Parent";
    Parent() {
        System.out.print(s);
    }
}

class Child extends Parent {
    { s = "Initializer"; }
    
    Child() {
        System.out.print(s);
    }
}

public class Main {
    public static void main(String[] args) {
        new Child();
    }
}

Inheritance Hard
A. InitializerParent
B. ParentParent
C. InitializerInitializer
D. ParentInitializer

52 Which of the following code snippets will fail to compile and why?

JAVA
// Snippet 1
class A { final void m() {} }
class B extends A { void m() {} }

// Snippet 2
class C { private void m() {} }
class D extends C { void m() {} }

// Snippet 3
final class E {}
class F extends E {}

// Snippet 4
class G { void m(final int x) { x++; } }

Using super and final keywords Hard
A. All snippets will compile successfully.
B. Snippet 1, 3 and 4.
C. Snippet 2, because private methods cannot be overridden.
D. Snippet 1 and 3.

53 Given the interfaces I1 and I2 and class C1, what happens when you try to compile class C2?

JAVA
interface I1 { 
    static void utility() { System.out.println("I1 Utility"); }
}
interface I2 {}

class C1 implements I1 {}

class C2 implements I2 {
    public static void main(String[] args) {
        C1.utility(); // Line X
        I1.utility(); // Line Y
    }
}

static and default methods Hard
A. Compilation fails at Line Y because static methods can only be called on implementing class names.
B. Compilation fails at both Line X and Line Y.
C. The code compiles and runs, printing "I1 Utility" twice.
D. Compilation fails at Line X because static interface methods are not inherited by implementing classes.

54 What is the output of the following code?

JAVA
class Money {
    int amount;
    String currency;

    public Money(int a, String c) { this.amount = a; this.currency = c; }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (!(obj instanceof Money)) return false;
        Money other = (Money) obj;
        // Bug: Only checks currency
        return this.currency.equals(other.currency);
    }

    @Override
    public int hashCode() {
        return this.currency.hashCode();
    }
}

public class Test {
    public static void main(String[] args) {
        Money income = new Money(100, "USD");
        Money expense = new Money(50, "USD");

        System.out.print(income.equals(expense));
        System.out.print(income.hashCode() == expense.hashCode());
    }
}

Object class and overriding toString() and equals() method Hard
A. truetrue
B. falsefalse
C. falsetrue
D. truefalse

55 Why does the following code fail to compile?

JAVA
abstract class Device {
    abstract void turnOn();
    Device() { 
        System.out.println("Device created"); 
        turnOn(); 
    }
}

class Phone extends Device {
    private String model = "iPhone";
    @Override
    void turnOn() {
        System.out.println(model.toUpperCase());
    }
}

public class Main {
    public static void main(String[] args) {
        Device d = new Phone();
    }
}

Abstract Class and Interface : Abstract method and abstract class Hard
A. Because an abstract class cannot have a constructor.
B. Because an abstract method (turnOn) cannot be called from within a constructor.
C. Because a private field (model) cannot be accessed in an overridden method.
D. It does not fail to compile; it throws a NullPointerException at runtime.

56 What is the result of compiling and running this code snippet?

JAVA
class Alpha {
    public CharSequence process(String s) {
        return new StringBuilder("Alpha");
    }
}

class Beta extends Alpha {
    @Override
    public String process(String s) { // Covariant Return Type
        return "Beta";
    }
}

public class Test {
    public static void main(String[] args) {
        Alpha a = new Beta();
        System.out.println(a.process("input"));
    }
}

Method overriding Hard
A. Alpha
B. Compilation Error: The method in Beta must have the exact same return type as in Alpha.
C. Beta
D. Compilation Error: Return type String is not compatible with CharSequence.

57 Analyze the following interface and class structure. What is the output of the main method?

JAVA
interface Perishable {
    default String getExpiry() { return "1 Day"; }
}

interface Sellable {
    default String getExpiry() { return "3 Days"; }
}

class Milk implements Perishable, Sellable {
    @Override
    public String getExpiry() {
        return Sellable.super.getExpiry();
    }
}

public class Test {
    public static void main(String[] args) {
        Perishable p = new Milk();
        System.out.println(p.getExpiry());
    }
}

Interfaces Hard
A. A RuntimeException is thrown.
B. 1 Day
C. Compilation Error due to ambiguous default methods.
D. 3 Days

58 Predict the output of the following program which demonstrates shadowing of instance variables.

JAVA
class A {
    int x = 10;
}

class B extends A {
    int x = 20;

    void printX() {
        int x = 30;
        System.out.print(x);
        System.out.print(this.x);
        System.out.print(super.x);
    }
}

public class Main {
    public static void main(String[] args) {
        new B().printX();
    }
}

Inheritance Hard
A. 102030
B. 303020
C. 303010
D. 302010

59 What is the result of attempting to compile and run this code?

JAVA
class Shape {
    Shape(String name) {
        System.out.println("Shape: " + name);
    }
}

class Circle extends Shape {
    Circle() {
        System.out.println("Circle created");
    }
    Circle(int radius) {
        this();
        // super("Circle"); // Line X
        System.out.println("Circle with radius " + radius);
    }
}

public class Test {
    public static void main(String[] args) {
        new Circle(5);
    }
}

super keyword Hard
A. The code compiles and runs, printing "Shape: null" followed by other text.
B. Compilation Error: The constructor Shape() is undefined.
C. Compilation Error: this() and super() cannot be in the same constructor.
D. The code compiles and runs, printing "Circle created" followed by "Circle with radius 5".

60 What is the output of the following code that mixes method hiding and overriding?

JAVA
class Parent {
    static String info() { return "Parent Static"; }
    String getInfo() { return "Parent Instance"; }
}

class Child extends Parent {
    static String info() { return "Child Static"; }
    String getInfo() { return "Child Instance"; }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.print(p.info() + ", ");
        System.out.print(p.getInfo());
    }
}

Using super and final keywords Hard
A. Compilation Error
B. Parent Static, Child Instance
C. Parent Static, Parent Instance
D. Child Static, Child Instance

61 Given an abstract class A and a concrete class B, what is the output of the program?

JAVA
abstract class A {
    static {
        System.out.print("SA-");
    }
    {
        System.out.print("IA-");
    }
    A() {
        System.out.print("CA-");
    }
    abstract void run();
}

class B extends A {
    static {
        System.out.print("SB-");
    }
    {
        System.out.print("IB-");
    }
    B() {
        System.out.print("CB-");
    }
    void run(){
        System.out.print("RUN");
    }
}

public class Test {
    public static void main(String[] args) {
        A a = new B();
    }
}

Abstract Class and Interface : Abstract method and abstract class Hard
A. SA-IA-CA-SB-IB-CB-
B. Compilation Error
C. SA-SB-IA-CA-IB-CB-
D. SA-SB-IB-CB-IA-CA-

62 Which statement best explains why this code fails to compile?

JAVA
class Super {
    protected Object getNumber() {
        return 10;
    }
}

class Sub extends Super {
    @Override
    private Integer getNumber() {
        return 20;
    }
}

Method overriding Hard
A. The @Override annotation is mandatory for overriding methods.
B. The overriding method getNumber in Sub cannot have a more restrictive access modifier (private) than the overridden method in Super (protected).
C. Private methods cannot be annotated with @Override.
D. The return type Integer is not a valid covariant return type for Object.

63 What is the result of attempting to compile and execute the following code?

JAVA
interface Game {
    static void play() { System.out.println("Playing Game"); }
}

interface Chess extends Game {
    // This static method is completely independent of Game.play()
    static void play() { System.out.println("Playing Chess"); }
}

public class Test {
    public static void main(String[] args) {
        Game g = null;
        g.play();
    }
}

static and default methods Hard
A. It fails to compile because a static method cannot be called on a null reference.
B. It throws a NullPointerException at runtime.
C. It fails to compile because a sub-interface cannot redefine a static method.
D. It prints "Playing Game".