Java Programming — OOP in Java
Java OOP, collections, multithreading
27 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 27 questions in OOP in Java
Q.1 Hard OOP in Java
In a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List accounts = new ArrayList(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
A Encapsulation
B Abstraction only
C Polymorphism and Runtime Binding
D Inheritance only
Correct Answer:  C. Polymorphism and Runtime Binding
EXPLANATION

The same withdraw() call behaves differently for SavingsAccount and CurrentAccount based on the actual object type at runtime. This is polymorphism with runtime (dynamic) binding.

Take Test
Q.2 Hard OOP in Java
Which of the following correctly implements the Factory Design Pattern principle in OOP?
A Creating objects directly using 'new' keyword everywhere
B Using a separate class with static methods to create and return objects of different types
C Using inheritance to create a base factory class
D Making all constructors private
Correct Answer:  B. Using a separate class with static methods to create and return objects of different types
EXPLANATION

The Factory Pattern uses a separate class (factory) with methods to create and return objects. This decouples object creation from usage.

Take Test
Q.3 Hard OOP in Java
A system has classes: Vehicle -> Car -> ElectricCar. If Vehicle.start() is overridden in Car and again in ElectricCar, what is the output of: Vehicle v = new ElectricCar(); v.start();
A Vehicle's start() executes
B Car's start() executes
C ElectricCar's start() executes
D Compilation error
Correct Answer:  C. ElectricCar's start() executes
EXPLANATION

This demonstrates dynamic (runtime) polymorphism. The actual object type (ElectricCar) determines which method is called, not the reference type (Vehicle).

Take Test
Q.4 Hard OOP in Java
In Java 16+, which sealed class feature allows you to restrict which classes can extend a class?
A Using 'final' keyword on all methods
B Using 'sealed' keyword with 'permits' clause
C Using 'protected' access modifier on constructor
D Using package-private access on the class
Correct Answer:  B. Using 'sealed' keyword with 'permits' clause
EXPLANATION

Java 17 introduced 'sealed' classes with the 'permits' clause to explicitly specify which classes can extend a sealed class, providing better control over inheritance.

Take Test
Q.5 Hard OOP in Java
You need to create a class that provides common functionality for all database operations (insert, update, delete). What is the best approach?
A Create an interface DatabaseOperations
B Create an abstract class with abstract methods
C Create a concrete class with final methods
D Create an abstract class with some concrete methods for common logic
Correct Answer:  D. Create an abstract class with some concrete methods for common logic
EXPLANATION

An abstract class allows you to define both abstract methods (to be overridden) and concrete methods (shared implementation), which is ideal for this scenario.

Take Test
Advertisement
Q.6 Hard OOP in Java
What is the key difference between composition and inheritance in object design?
A Composition is always better than inheritance
B Inheritance represents 'is-a' relationship; composition represents 'has-a' relationship
C Composition cannot access private members
D Inheritance is only for abstract classes
Correct Answer:  B. Inheritance represents 'is-a' relationship; composition represents 'has-a' relationship
EXPLANATION

Inheritance models an 'is-a' relationship (Dog is-an Animal), while composition models a 'has-a' relationship (Car has-an Engine). Composition is often preferred for flexibility.

Take Test
Q.7 Hard OOP in Java
Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?
A Method Overloading
B Method Overriding with Covariant Return Types
C Runtime Polymorphism
D Method Hiding
Correct Answer:  B. Method Overriding with Covariant Return Types
EXPLANATION

Covariant return types (Java 5+) allow a method to return a subtype of the parent class method's return type. For example, if parent returns Animal, child can return Dog (subclass of Animal).

Take Test
Q.8 Hard OOP in Java
What is the correct order of execution when you create an object in a multi-level inheritance hierarchy?
A Child constructor -> Parent constructor -> Grandparent constructor
B Grandparent constructor -> Parent constructor -> Child constructor
C All constructors execute simultaneously
D Only the child constructor executes
Correct Answer:  B. Grandparent constructor -> Parent constructor -> Child constructor
EXPLANATION

Constructor execution follows the inheritance chain from top to bottom. super() is implicitly called, executing parent constructors first.

Take Test
Q.9 Hard OOP in Java
In a real-world e-commerce system, you want to prevent direct instantiation of a base Product class but allow creation of Laptop, Mobile, and Tablet. Which approach is best?
A Make Product class final
B Make Product class abstract with abstract methods for subclasses to implement
C Make Product class private
D Use an interface for Product
Correct Answer:  B. Make Product class abstract with abstract methods for subclasses to implement
EXPLANATION

An abstract class with abstract methods enforces that subclasses must implement required behaviors while preventing direct instantiation of the base class.

Take Test
Q.10 Hard OOP in Java
What will be the compilation result of this code?
interface A { void method(); }
interface B { void method(); }
class C implements A, B {
public void method() { }
}
A Compilation error: ambiguous method
B Compiles successfully
C Runtime error
D Compilation error: method signature mismatch
Correct Answer:  B. Compiles successfully
EXPLANATION

Java allows implementing multiple interfaces even if they have the same method signature. The class provides a single implementation that satisfies both interfaces. This compiles successfully.

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