Home Subjects Java Programming OOP in Java

Java Programming
OOP in Java

Java OOP, collections, multithreading

27 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–27 of 27
Topics in Java Programming
Q.21 Hard OOP in Java
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
A Create an abstract parent class with abstract methods
B Create an interface with abstract method declarations
C Use composition instead of inheritance
D Create static utility methods in a common class
Correct Answer:  B. Create an interface with abstract method declarations
EXPLANATION

When unrelated classes need to implement a common contract, an interface is the best choice. Interfaces are specifically designed for this purpose, allowing multiple implementation without forcing an inheritance hierarchy.

Test
Q.22 Hard OOP in Java
When a static method is called on an instance of a class, what happens?
A The method is called on the instance object
B The method is called on the class, not the instance (though syntactically allowed)
C A compilation error occurs
D The instance data is used in the static method
Correct Answer:  B. The method is called on the class, not the instance (though syntactically allowed)
EXPLANATION

Static methods belong to the class, not instances. When called on an instance, Java internally calls the method on the class. Static methods cannot access instance variables or use 'this' keyword.

Test
Q.23 Hard OOP in Java
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
A abstract and final
B final and ensure all fields are final
C private and synchronized
D static and volatile
Correct Answer:  B. final and ensure all fields are final
EXPLANATION

To prevent inheritance, use 'final' on the class. To make instances immutable, declare all fields as 'final' and ensure they are not modified after initialization. A classic example is the String class.

Test
Q.24 Hard OOP in Java
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
A A compilation error occurs
B The parent class constructor is automatically called (default no-arg constructor)
C The parent class constructor is never called
D The child class constructor must be declared as abstract
Correct Answer:  B. The parent class constructor is automatically called (default no-arg constructor)
EXPLANATION

Java automatically inserts a call to the parent class's no-arg constructor if 'super()' is not explicitly called. This ensures parent class initialization happens before child class initialization.

Test
Q.25 Hard OOP in Java
What is the output?

interface I1 {
void method1();
}
interface I2 extends I1 {
void method2();
}
class C implements I2 {
public void method1() { System.out.println("M1"); }
public void method2() { System.out.println("M2"); }
}
public class Test {
public static void main(String[] args) {
I1 obj = new C();
obj.method2();
}
}
A M2
B M1
C Compilation Error
D Runtime Error
Correct Answer:  C. Compilation Error
EXPLANATION

obj is of type I1 which doesn't have method2(). Though actual object C has method2(), reference type determines what methods are accessible.

Test
Q.26 Hard OOP in Java
What will be the output?

class Test {
int x = 5;
{
x = 10;
}
Test() {
x = 15;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
A 5
B 10
C 15
D Compilation Error
Correct Answer:  C. 15
EXPLANATION

Instance initializer block executes after variable initialization but before constructor. Constructor executes last, setting x = 15.

Test
Q.27 Hard OOP in Java
What will be the output?

interface A {
void show();
}
class B implements A {
public void show() {
System.out.println("B");
}
}
class C extends B {
public void show() {
System.out.println("C");
}
}
A obj = new C();
obj.show();
A A
B B
C C
D Compilation Error
Correct Answer:  C. C
EXPLANATION

Polymorphism in action. obj is of type A (interface), but actual object is C. C's show() method is called, printing 'C'.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips