Unit 3 - Practice Quiz
1 Which keyword is used in Java to specify that a class is inheriting from another class?
2 The relationship between a subclass and its superclass in Java is known as an...
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?
4 Which annotation is commonly used to tell the compiler that you intend to override a method from a superclass?
5 How can a subclass constructor call the constructor of its direct superclass?
this()
parent()
super()
6
What is the purpose of super.myMethod() inside a subclass method?
myMethod from the superclass
myMethod
7 Which class is the ultimate superclass of every class in Java?
8
What is the primary reason for overriding the toString() method?
9
What is the effect of declaring a class with the final keyword?
final.
10
If a method is declared as final, what does it mean?
11
What type of value does the instanceof operator return?
Object
boolean
String
int
12
What does the expression myObject instanceof MyClass check?
myObject has a method named MyClass
myObject is an instance of MyClass or one of its subclasses
MyClass is an instance of myObject
myObject and MyClass are equal
13 Which keyword is used to declare a method that has no implementation body?
14 What is a key characteristic of an abstract class?
new keyword.
new keyword.
15 Which keyword is used for a class to inherit the methods from an interface?
16 Can a single class implement multiple interfaces in Java?
17 Starting from Java 8, a method in an interface can have an implementation if it is declared with which keyword?
18
How is a static method inside an interface invoked?
this keyword
19
What is the main purpose of overriding the equals() method from the Object class?
20 A class that inherits from another class is called a...
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?
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() 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();
}
}
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));
}
}
25 Which of the following code snippets will result in a compilation error?
class A { void method() {} }
final class B extends A { }
class A { final void method() {} }
class B extends A { void method() {} }
class A { final void method() {} }
class B extends A { }
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);
}
}
27 What is the primary problem that interfaces solve in Java regarding inheritance?
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 {}
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);
}
}
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();
}
}
class Derived extends Base {
public Object process() { return new Object(); }
}
class Derived extends Base {
public Data process(int x) { return new Data(); }
}
class Derived extends Base {
public SubData process() { return new SubData(); }
}
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?
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.
33
What is the main purpose of declaring a class as final?
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.");
}
}
}
35 Which statement best describes a key difference between an abstract class and an interface in Java (post-Java 8)?
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?
}
show() will randomly choose one implementation.
show() method from the first interface listed (A) is used.
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();
}
}
38 Which of the following is an invalid declaration within a Java interface?
public static final int MAX_VALUE = 100;
void calculate();
default boolean isValid() { return true; }
protected void process();
39 Which statement is true regarding abstract methods?
{}.
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();
}
}
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();
}
}
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();
}
}
private methods cannot be accessed in a constructor.
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();
}
}
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();
}
}
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());
}
}
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();
}
}
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();
}
}
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);
}
}
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);
}
}
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);
}
}
Data updated with MAX_RETRIES = 5
Accessing via interface ref: 3
u.MAX_RETRIES is ambiguous.
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();
}
}
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++; } }
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
}
}
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());
}
}
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();
}
}
model) cannot be accessed in an overridden method.
turnOn) cannot be called from within a constructor.
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"));
}
}
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());
}
}
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();
}
}
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);
}
}
this() and super() cannot be in the same constructor.
Shape() is undefined.
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());
}
}
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();
}
}
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;
}
}
Integer is not a valid covariant return type for Object.
@Override.
getNumber in Sub cannot have a more restrictive access modifier (private) than the overridden method in Super (protected).
@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();
}
}
NullPointerException at runtime.