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
What will be the output? interface A { void show(); } class B implements A { public void show() { System.out.println("B"); } } class C extends B { public void show() { System.out.println("C"); } } A obj = new C(); obj.show();
What will be the output? class Test { int x = 5; { x = 10; } Test() { x = 15; } public static void main(String[] args) { Test t = new Test(); System.out.println(t.x); } }
What is the output? interface I1 { void method1(); } interface I2 extends I1 { void method2(); } class C implements I2 { public void method1() { System.out.println("M1"); } public void method2() { System.out.println("M2"); } } public class Test { public static void main(String[] args) { I1 obj = new C(); obj.method2(); } }
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
When a static method is called on an instance of a class, what happens?
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
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); } }
In Java, can you create an object of an abstract class?
What is the main difference between method overriding and method overloading?
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(); } }
A real-world scenario: You're designing a banking system. Should you use an abstract class or interface for 'Account'?
What happens with exception handling in method overriding?
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(); } }
In a legacy system, you find code with very deep inheritance hierarchy (5+ levels). What design problem does this indicate?
Which of the following best demonstrates the Liskov Substitution Principle in OOP?
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?
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() { } }
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?
What is the correct order of execution when you create an object in a multi-level inheritance hierarchy?