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 11–20 of 27
Topics in Java Programming
Q.11 Hard OOP in Java
Design scenario: You need to create a payment system where Credit Card, Debit Card, and UPI are payment methods. What's the best OOP approach?
A Create a concrete PaymentCard class with if-else for each type
B Create an interface PaymentMethod with implementations for each type
C Use a single abstract method for all payment types
D Use static methods to handle all payment types
Correct Answer:  B. Create an interface PaymentMethod with implementations for each type
EXPLANATION

Using an interface with multiple implementations follows the Interface Segregation Principle and Strategy Pattern, making the system extensible and maintainable.

Test
Q.12 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.

Test
Q.13 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.

Test
Q.14 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.

Test
Q.15 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.

Test
Q.16 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.

Test
Q.17 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.

Test
Q.18 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.

Test
Q.19 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.

Test
Q.20 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.

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