iGET

Java Programming - MCQ Practice Questions

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

951 questions | 100% Free

Q.21Hard

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

Q.22Hard

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.23Hard

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.24Hard

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

Q.25Hard

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

Q.26Hard

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.27Hard

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

Q.28Hard

What happens with exception handling in method overriding?

Q.29Hard

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.30Hard

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

Q.31Hard

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

Q.32Hard

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.33Hard

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.34Hard

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.35Hard

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

Q.36Hard

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

Q.37Hard

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

Q.38Hard

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

Q.39Hard

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

Q.40Hard

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