Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 761–770 of 958
Topics in Java Programming
Q.761 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.762 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.763 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.764 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.765 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.766 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.767 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.768 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
Q.769 Medium OOP in Java
In Java, if a parent class has a method void display() and a child class overrides it as void display(int x), what is this called?
A Method Overriding
B Method Overloading
C Method Shadowing
D Invalid - compilation error
Correct Answer:  B. Method Overloading
EXPLANATION

This is method overloading, not overriding, because the method signature is different (different parameters). Overriding requires the same signature.

Take Test
Q.770 Easy OOP in Java
Which keyword is used to prevent a class from being subclassed in Java?
A abstract
B final
C static
D sealed
Correct Answer:  B. final
EXPLANATION

The 'final' keyword prevents a class from being extended. 'sealed' (Java 17+) allows selective subclassing, but 'final' is the standard way to prevent all subclassing.

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