Java Programming — OOP in Java
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 100 questions in OOP in Java
Q.1 Medium OOP in Java
In Java, when you override a method from a parent class, which of the following is NOT a valid reason to use the @Override annotation?
A To enable compile-time checking for correct method signature
B To improve code readability and maintainability
C To automatically invoke the parent class method implementation
D To catch errors if the parent method is removed or signature changes
Correct Answer:  C. To automatically invoke the parent class method implementation
EXPLANATION

The @Override annotation helps verify that you are correctly overriding a parent method and improves code clarity, but it does NOT automatically invoke the parent class method. You must use super.methodName() for that. @Override is purely a compile-time marker for verification purposes.

Take Test
Q.2 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.3 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.4 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.5 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
Advertisement
Q.6 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
Q.7 Medium OOP in Java
In Java, a class can implement multiple interfaces but can extend only one class. This design choice primarily supports which principle?
A Single Responsibility Principle
B Liskov Substitution Principle
C Avoiding diamond problem in single inheritance
D Interface Segregation Principle
Correct Answer:  C. Avoiding diamond problem in single inheritance
EXPLANATION

Multiple inheritance of implementation can cause the diamond problem. Java avoids this by allowing single class inheritance but multiple interface implementation.

Take Test
Q.8 Medium OOP in Java
Which of the following statements about 'this' keyword in Java is INCORRECT?
A 'this' can be used to refer to the current object
B 'this' can be used to invoke another constructor of the same class
C 'this' can be used in static methods
D 'this' can be used to return the current object
Correct Answer:  C. 'this' can be used in static methods
EXPLANATION

'this' cannot be used in static methods because static methods are class-level and don't have an instance context. 'this' always refers to an instance.

Take Test
Q.9 Medium OOP in Java
What will be printed when this code executes?
class A { int x = 5; }
class B extends A { int x = 10; }
public class Test {
public static void main(String[] args) {
A a = new B();
System.out.println(a.x);
}
}
A 5
B 10
C Compilation error
D Runtime exception
Correct Answer:  A. 5
EXPLANATION

Variable shadowing occurs here. 'a' is of type A, so a.x refers to A's variable x which is 5. Variables are not overridden, only methods are.

Take Test
Q.10 Medium OOP in Java
Consider a scenario where you have an interface Shape with a method calculateArea(). You implement this in classes Circle and Rectangle. Which OOP concept is being demonstrated?
A Encapsulation
B Abstraction
C Polymorphism
D Both Abstraction and Polymorphism
Correct Answer:  D. Both Abstraction and Polymorphism
EXPLANATION

The interface provides abstraction by hiding implementation details. Multiple classes implementing the same interface method demonstrates polymorphism (many forms).

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