Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 181–190 of 212 questions
Q.181 Hard OOP in Java
Which of the following best demonstrates the Liskov Substitution Principle in OOP?
A Child class can override parent methods with different return types
B Child class objects should be usable wherever parent class objects are expected
C Parent class should not know about child class
D Child class should inherit all methods from parent
Correct Answer:  B. Child class objects should be usable wherever parent class objects are expected
EXPLANATION

The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. Option B correctly represents this principle.

Take Test
Q.182 Hard OOP in Java
In a legacy system, you find code with very deep inheritance hierarchy (5+ levels). What design problem does this indicate?
A Good object-oriented design
B Violation of the Liskov Substitution Principle
C Possible violation of 'favor composition over inheritance' principle
D The code is efficient and should be kept as is
Correct Answer:  C. Possible violation of 'favor composition over inheritance' principle
EXPLANATION

Deep inheritance hierarchies are considered a code smell. They suggest that composition might have been a better approach, leading to tighter coupling and reduced flexibility.

Take Test
Q.183 Hard OOP in Java
What is the output of the following code?
interface I1 { default void show() { System.out.println("I1"); } }
interface I2 { default void show() { System.out.println("I2"); } }
class C implements I1, I2 { public void show() { I1.super.show(); } public static void main(String[] args) { new C().show(); } }
A I1
B I2
C Compile-time error due to ambiguity
D I1 I2
Correct Answer:  A. I1
EXPLANATION

When a class implements multiple interfaces with the same default method, it must explicitly override the method. Using I1.super.show() calls I1's default implementation.

Take Test
Q.184 Hard OOP in Java
What happens with exception handling in method overriding?
A Child class method can throw any exception
B Child class method can only throw checked exceptions, not unchecked
C Child class method can throw the same or narrower (subclass) exceptions only
D Exception handling rules don't apply to overridden methods
Correct Answer:  C. Child class method can throw the same or narrower (subclass) exceptions only
EXPLANATION

Liskov Substitution Principle: A child class method can throw the same exception or a narrower exception than the parent method, not a broader one.

Take Test
Q.185 Hard OOP in Java
A real-world scenario: You're designing a banking system. Should you use an abstract class or interface for 'Account'?
A Use interface because it's more flexible
B Use abstract class because Account has state (balance, accountNumber) and shared behavior (deposit, withdraw)
C Use both abstract class and interface together
D Neither, use a concrete class
Correct Answer:  B. Use abstract class because Account has state (balance, accountNumber) and shared behavior (deposit, withdraw)
EXPLANATION

Abstract classes are suitable when you have shared state and constructors needed. An Account has properties like balance and account number, making abstract class the better choice.

Take Test
Advertisement
Q.186 Hard OOP in Java
What does the following code output?
class A { static void display() { System.out.println("A"); } }
class B extends A { static void display() { System.out.println("B"); } }
public class Test { public static void main(String[] args) { A ref = new B(); ref.display(); } }
A A
B B
C Compile-time error
D Runtime error
Correct Answer:  A. A
EXPLANATION

Static methods are resolved at compile-time based on the reference type, not the object type. Hence, A.display() is called, not B.display(). This is method hiding, not overriding.

Take Test
Q.187 Hard OOP in Java
What is the main difference between method overriding and method overloading?
A Overriding is for same class, overloading is for different classes
B Overriding changes method signature, overloading keeps it same
C Overriding is runtime polymorphism, overloading is compile-time polymorphism
D Overriding requires final keyword, overloading doesn't
Correct Answer:  C. Overriding is runtime polymorphism, overloading is compile-time polymorphism
EXPLANATION

Method overloading is compile-time (static) polymorphism with same method name but different parameters in the same class. Method overriding is runtime (dynamic) polymorphism where a child class provides a specific implementation of a parent method.

Take Test
Q.188 Hard OOP in Java
In Java, can you create an object of an abstract class?
A Yes, always
B No, never
C Yes, using anonymous inner class
D Only if it has concrete methods
Correct Answer:  C. Yes, using anonymous inner class
EXPLANATION

Abstract classes cannot be directly instantiated, but you can create objects using anonymous inner classes that implement the abstract methods.

Take Test
Q.189 Hard OOP in Java
What is the output of this code?
class A { int x = 10; }
class B extends A { int x = 20; }
public class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.x); } }
A 10
B 20
C Compilation Error
D Runtime Error
Correct Answer:  A. 10
EXPLANATION

Variable shadowing occurs here. The reference type is A, so a.x accesses A's x field which is 10. Method overriding works with methods, not instance variables.

Take Test
Q.190 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.

Take Test
IGET
iget AI
Online · Ask anything about exams
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