iGET

Java Programming - MCQ Practice Questions

Practice free Java Programming multiple-choice questions with detailed answers and explanations. Perfect for competitive exam preparation.

958 questions | 100% Free

Q.21Hard

A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?

Q.22Hard

When a static method is called on an instance of a class, what happens?

Q.23Hard

A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?

Q.24Hard

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); } }

Q.25Hard

In Java, can you create an object of an abstract class?

Q.26Hard

What is the main difference between method overriding and method overloading?

Q.27Hard

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(); } }

Q.28Hard

A real-world scenario: You're designing a banking system. Should you use an abstract class or interface for 'Account'?

Q.29Hard

What happens with exception handling in method overriding?

Q.30Hard

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(); } }

Q.31Hard

In a legacy system, you find code with very deep inheritance hierarchy (5+ levels). What design problem does this indicate?

Q.32Hard

Which of the following best demonstrates the Liskov Substitution Principle in OOP?

Q.33Hard

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?

Q.34Hard

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() { } }

Q.35Hard

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?

Q.36Hard

What is the correct order of execution when you create an object in a multi-level inheritance hierarchy?

Q.37Hard

Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?

Q.38Hard

What is the key difference between composition and inheritance in object design?

Q.39Hard

You need to create a class that provides common functionality for all database operations (insert, update, delete). What is the best approach?

Q.40Hard

In Java 16+, which sealed class feature allows you to restrict which classes can extend a class?