What are the four main principles of OOP?
Encapsulation, Inheritance, Polymorphism, Abstraction
Which access modifier makes a variable only accessible within the same class?
private
What keyword is used in Java to inherit from another class?
extends
What are the two types of polymorphism in Java?
Compile-time (method overloading) and runtime (method overriding).
Can an abstract class have a constructor?
Yes, but it cannot be instantiated.
Which OOP principle ensures that a class only exposes necessary information?
Encapsulation
What is the purpose of getter and setter methods in encapsulation?
They control access to private variables while maintaining data integrity.
Can a Java class inherit from multiple classes? Why or why not?
No, Java does not support multiple inheritance due to ambiguity issues.
Can a subclass override a static method from its superclass? Why or why not?
No, because static methods belong to the class, not instances.
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).
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.
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).
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
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).
Can a class implement multiple interfaces?
Yes, unlike inheritance, a class can implement multiple interfaces.
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
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).
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().
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.
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.
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.
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.
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()).
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"
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