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
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?
Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?
What is the key difference between composition and inheritance in object design?
What is the default access modifier for a class member in Java if no modifier is specified?
Which keyword is used to prevent a class from being subclassed 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?
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?
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); } }
Which of the following statements about 'this' keyword in Java is INCORRECT?
In Java, a class can implement multiple interfaces but can extend only one class. This design choice primarily supports which principle?
You need to create a class that provides common functionality for all database operations (insert, update, delete). What is the best approach?
In Java 16+, which sealed class feature allows you to restrict which classes can extend a class?
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();
Which of the following correctly implements the Factory Design Pattern principle in OOP?
In a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List<Account> accounts = new ArrayList<>(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
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?