Unit 3 - Practice Quiz

CSE310 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. implements
B. inherits
C. extends
D. super

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

Inheritance Easy
A. "uses-a" relationship
B. "is-a" relationship
C. "part-of" relationship
D. "has-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 Duplication
B. Method Hiding
C. Method Overloading
D. Method Overriding

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 this()
B. Using parent()
C. It is called automatically and cannot be called explicitly
D. Using super()

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

super keyword Easy
A. To call the overridden version of myMethod from the superclass
B. To call a static method from the superclass
C. To check if the superclass has myMethod
D. To declare a new method in 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.Class
B. java.lang.Object
C. java.lang.System
D. java.lang.Main

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

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

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

Using super and final keywords Easy
A. The class cannot be extended (subclassed).
B. All methods in the class are automatically final.
C. The class can only have one instance.
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 must return a value.
B. It becomes a static method.
C. It cannot be overridden by any subclass.
D. It can only be called once.

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 has a method named MyClass
B. If myObject is an instance of MyClass or one of its subclasses
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. concrete
B. abstract
C. empty
D. virtual

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. extends
B. implements
C. uses
D. inherits

16 Can a single class implement multiple interfaces in Java?

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

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. final
B. override
C. default
D. concrete

18 How is a static method inside an interface invoked?

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

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 provide a custom string representation
B. To define a condition for logical equality between two objects
C. To create a copy of an object
D. To check if two object references point to the same memory location

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

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

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. Generic Animal Sound
C. A compilation error occurs.
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. Parent
B. ParentChild
C. A compilation error because super() is called with an argument.
D. ChildParent

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: Drawing Circle
B. Compilation error: Abstract classes cannot have constructors.
C. Runtime error: Cannot instantiate an abstract class.
D. Output:
Shape constructor
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. falsefalse
C. truetrue
D. falsetrue

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

Using super and final keywords Medium
A. java
class A { void method() {} }
final class B extends A { }
B. java
class A { final void method() {} }
class B extends A { void method() {} }
C. java
class A { final void method() {} }
class B extends A { }
D. java
final class A { }
class B { A a = new 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. false true true
B. A compilation error occurs.
C. true true true
D. true true false

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

Interfaces Medium
A. They enforce that all subclasses must have a specific set of constructors.
B. They allow a class to inherit state (instance variables) from multiple sources.
C. They provide a mechanism to achieve a form of 'multiple inheritance' of type, allowing a class to adhere to multiple contracts.
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. Drill.getCategory();
B. new Powertool().start();
C. Powertool.getCategory();
D. Drill d = new Drill(); d.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 runtime exception is thrown.
C. 10
D. A compilation error occurs.

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 Object process() { return new Object(); }
}
B. java
class Derived extends Base {
public Data process(int x) { return new Data(); }
}
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 string representation consisting of the class name, an '@' sign, and the unsigned hexadecimal representation of the object's hash code.
B. The memory address of the object.
C. The string "null".
D. A compilation error, as toString() must always be overridden.

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 SubClass
B. Printed in SuperClass
C. Printed in SuperClass, Printed in SubClass
D. A compilation error occurs due to the use of super.

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. Is String. Is Object. Is CharSequence.
B. Is String.
C. A compilation error.
D. Is String. Is Object.

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. A class can implement multiple abstract classes but only extend one interface.
C. An interface can be instantiated, but an abstract class cannot.
D. An abstract class can have instance variables (state), while an interface cannot.

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 a call to show() will randomly choose one implementation.
B. The code compiles, and the show() method from the first interface listed (A) is used.
C. The code fails to compile because a class cannot implement two interfaces with conflicting methods.
D. The code fails to compile unless class C explicitly overrides the show() method.

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. MoreDerived
C. Base Derived MoreDerived
D. A compilation error occurs.

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

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

39 Which statement is true regarding abstract methods?

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

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. B
B. A runtime error.
C. A compilation error.
D. A

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. Car MyCar
C. Compilation Error
D. Vehicle MyCar

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 "Derived.process".
C. The code fails to compile because process() in Derived cannot override the final method in Base.
D. The code compiles and prints "Base.process".

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. I2
B. Compilation Error: Ambiguous method call.
C. I1
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...
B. Writing code...
C. Writing book...
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. 1
B. 2
C. 0
D. The code throws a runtime exception.

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. 30
D. 10

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. 10
B. 10null
C. null0
D. 100

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. truefalsetruetrue
B. truefalsefalsetrue
C. Compilation Error
D. truefalsetruefalse

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. Output:
Data updated with MAX_RETRIES = 5
Accessing via interface ref: 3
B. Compilation Error because u.MAX_RETRIES is ambiguous.
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. ParentInitializer
B. InitializerInitializer
C. ParentParent
D. InitializerParent

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. Snippet 1, 3 and 4.
B. All snippets will compile successfully.
C. Snippet 1 and 3.
D. Snippet 2, because private methods cannot be overridden.

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 both Line X and Line Y.
B. The code compiles and runs, printing "I1 Utility" twice.
C. Compilation fails at Line Y because static methods can only be called on implementing class names.
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. truefalse
B. falsefalse
C. falsetrue
D. truetrue

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 a private field (model) cannot be accessed in an overridden method.
B. Because an abstract class cannot have a constructor.
C. Because an abstract method (turnOn) cannot be called from within a constructor.
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. Compilation Error: The method in Beta must have the exact same return type as in Alpha.
B. Alpha
C. Compilation Error: Return type String is not compatible with CharSequence.
D. Beta

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. Compilation Error due to ambiguous default methods.
C. 3 Days
D. 1 Day

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. 303010
B. 302010
C. 303020
D. 102030

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. Compilation Error: this() and super() cannot be in the same constructor.
B. The code compiles and runs, printing "Shape: null" followed by other text.
C. Compilation Error: The constructor Shape() is undefined.
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. Child Static, Child Instance
B. Parent Static, Parent Instance
C. Compilation Error
D. Parent 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-SB-IB-CB-IA-CA-
B. Compilation Error
C. SA-IA-CA-SB-IB-CB-
D. SA-SB-IA-CA-IB-CB-

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 return type Integer is not a valid covariant return type for Object.
B. Private methods cannot be annotated with @Override.
C. The overriding method getNumber in Sub cannot have a more restrictive access modifier (private) than the overridden method in Super (protected).
D. The @Override annotation is mandatory for overriding methods.

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 throws a NullPointerException at runtime.
B. It prints "Playing Game".
C. It fails to compile because a sub-interface cannot redefine a static method.
D. It fails to compile because a static method cannot be called on a null reference.