OOP Principles
Encapsulation & Access Modifiers
Inheritance
Polymorphism
Abstract Classes & Interfaces
100

What are the four main principles of OOP?

Encapsulation, Inheritance, Polymorphism, Abstraction

100

Which access modifier makes a variable only accessible within the same class?

private

100

What keyword is used in Java to inherit from another class?

extends

100

What are the two types of polymorphism in Java?

Compile-time (method overloading) and runtime (method overriding).

100

Can an abstract class have a constructor?

Yes, but it cannot be instantiated.

200

Which OOP principle ensures that a class only exposes necessary information?

Encapsulation

200

What is the purpose of getter and setter methods in encapsulation?

They control access to private variables while maintaining data integrity.

200

Can a Java class inherit from multiple classes? Why or why not?

No, Java does not support multiple inheritance due to ambiguity issues.

200

Can a subclass override a static method from its superclass? Why or why not?

No, because static methods belong to the class, not instances.

200

What is the difference between an abstract class and an interface?

Abstract classes can have both concrete and abstract methods, while interfaces can only have abstract methods (before Java 8).

300

What is the difference between method overloading and method overriding?

Overloading = same method name, different parameters; Overriding = subclass provides a new implementation of a method in the superclass.

300

Given this code snippet, what will be the output?
class Car { private String model = "Tesla"; public String getModel() { return model; } } public class Main { public static void main(String[] args) { Car c = new Car(); System.out.println(c.model); }}

Compilation error (model is private and cannot be accessed directly).

300

class Parent { void show() { System.out.println("Parent"); }}<br>class Child extends Parent { void show() { System.out.println("Child"); }}<br>public class Main { public static void main(String[] args) { Parent p = new Child(); p.show(); }}

Child

300

How does Java determine which overridden method to call at runtime?

It depends on the object's actual type, not the reference type (dynamic binding).

300

Can a class implement multiple interfaces?

Yes, unlike inheritance, a class can implement multiple interfaces.

400

Given the following class, what will be the output when calling obj.display()?
class A { 

void display() { System.out.println("A"); }

}

class B extends A { 

void display() { System.out.println("B"); }}

public class Main { 

public static void main(String[] args) { 

A obj = new B(); obj.display(); }}

B

400

What is the difference between public, protected, private, and default access modifiers?

public (anywhere), protected (same package + subclasses), private (same class only), default (same package only).

400

What is constructor chaining, and how does it work in Java?

It is the process of calling one constructor from another, either in the same class using this() or in a superclass using super().

400

Can overloaded methods have different return types in Java?

No, overloading is based on method signature (name + parameters), and return type alone does not differentiate methods.

400

What happens if a class implements an interface but does not define all of its methods?

The class must be declared abstract or implement all required methods.

500

Identify the error in this code:
class A { private void show() { System.out.println("Hello"); }}<br>class B extends A { void show() { System.out.println("Hi"); }}

public class Main { public static void main(String[] args) { B obj = new B(); obj.show(); }}

Compilation error because show() in class A is private and not inherited by B, so B is not overriding anything.

500

Can a class be declared private in Java? Why or why not?

No, a top-level class cannot be private. However, nested classes can be private to restrict access inside an outer class.

500

 What will be the output?
java <br> class Parent { Parent() { System.out.println("Parent Constructor"); } } <br> class Child extends Parent { Child() { System.out.println("Child Constructor"); } } <br> public class Main { public static void main(String[] args) { Child c = new Child(); } }

Parent Constructor
 Child Constructor
 Explanation: The superclass constructor (Parent()) is always called first before the subclass constructor (Child()).

500

Given this code snippet, what is the output?
java<br>class Animal { void speak() { System.out.println("Animal speaks"); }}<br>class Dog extends Animal { void speak() { System.out.println("Dog barks"); }}<br>public class Main { public static void main(String[] args) { Animal a = new Dog(); a.speak(); }}

Output: "Dog barks"

500

When should you use an abstract class instead of an interface?

You should choose this over an interface when you need shared behavior, constructors, default implementations, enforced inheritance, or plan future expansion

M
e
n
u