OOPs Interview Questions
Q.1 what is Object-Oriented Programming (OOP)?
Answer: Object-Oriented Programming (OOP) is a programming paradigm
based on the concept of objects, which can contain data in the form
of fields and code in the form of procedures (methods). It emphasizes
the organization of code into reusable, self-contained units
(objects) that interact with each other.
Q.2 what are the four main principles of OOP?
Answer: The four main principles of OOP are:
Encapsulation
Inheritance
Polymorphism
Abstraction
Q.3 what is encapsulation?
Answer: Encapsulation is the process of bundling data (fields) and
methods (functions) that operate on the data into a single unit
called a class. It hides the internal state of an object from the
outside world and only exposes the necessary functionality through
public methods.
Q.4 what is inheritance?
Answer: Inheritance is a mechanism in Java by which one class
(subclass/derived class) can inherit properties and behavior from
another class (superclass/base class). It promotes code reusability
and allows subclasses to extend or modify the functionality of the
superclass.
Q.5 Explain the types of inheritance.
Answer: In Java, there are three types of inheritance supported:
Single inheritance
Multilevel inheritance
Hierarchical inheritance
Q.6 what is polymorphism?
Answer: Polymorphism is the ability of objects to take on multiple
forms. In Java, polymorphism can be achieved through method
overloading (compile-time polymorphism) and method overriding (run-
time polymorphism).
Q.7 what is method overloading?
Answer: Method overloading is a feature of Java that allows a class
to have multiple methods with the same name but different parameter
lists. It is also known as compile-time polymorphism or static
polymorphism.
Q.8 what is method overriding?
Answer: Method overriding is a feature of Java that allows a subclass
to provide a specific implementation of a method that is already
defined in its superclass. It is also known as run-time polymorphism
or dynamic polymorphism.
Q.9 what is abstraction?
Answer: Abstraction is the process of hiding the implementation
details and showing only the essential features of an object. It
allows you to focus on what an object does rather than how it does
it, thus simplifying complex systems.
Q.10 what is a class?
Answer: In Java, a class is a blueprint or template for creating
objects. It defines the properties (fields) and behaviors (methods)
that all objects of that class will have.
Q.11 what is an object?
Answer: An object is an instance of a class. It is a real-world
entity that has state (fields) and behavior (methods).
Q.12 what is a constructor?
Answer: A constructor in Java is a special type of method that is
used to initialize objects of a class. It has the same name as the
class and does not have a return type.
Q.13 what is the difference between a constructor and a method?
Answer: Constructors are used to initialize objects of a class and
are invoked automatically when an object is created. Methods, on the
other hand, are used to perform operations on objects and are invoked
explicitly by calling them.
Q.14 what is the “this” keyword?
Answer: The this keyword refers to the current object instance. It is
used to differentiate between instance variables and parameters with
the same name, to invoke constructors within other constructors, and
to pass the current object as a parameter.
Q.15 what is method chaining?
Answer: Method chaining is a technique in Java where multiple method
calls are chained together in a single statement. It is achieved by
having each method return the object itself (this) and allows for
concise and fluent code.
Q.16 what is composition (Tight Coupling)?
Answer: Without existing container object if there is no chance of
existing contained object then the container and contained object are
strongly associated and this strong association is nothing but a
composition.
Q.17 what is aggregation (Loose Coupling)?
Answer: Without existing container object if there is chance of
existing contained object then the container and contained object are
weekly associated and this week association is nothing but a
composition.
Q.18 what is the super keyword in Java?
Answer: The super keyword in Java is used to refer to the superclass
(parent class) of the current object instance. It can be used to
access superclass methods, constructors, and variables from the
subclass.
Q.19 what is method hiding in Java?
Answer: Method hiding is a concept in Java where a subclass defines a
static method with the same signature as a static method in its
superclass. It does not override the superclass method but hides it,
causing the subclass method to be invoked at compile time.
Q20 what is the instanceof operator in Java?
Answer: The instanceof operator in Java is used to test whether an
object is an instance of a particular class or interface. It returns
true if the object is an instance of the specified type, false
otherwise.
Q.21 what is a package?
Answer: A package in Java is a namespace that organizes a set of
related classes and interfaces. It helps to avoid naming conflicts,
provides access protection, and promotes code organization and
reusability.
Q.22 what is an abstract class?
Answer: An abstract class in Java is a class that cannot be
instantiated directly and may contain abstract methods (methods
without a body). It is intended to be subclasses, and subclasses must
provide implementations for all abstract methods.
Q.23 what is an interface?
Answer: An interface in Java is a reference type that can contain
only constants, method signatures, default methods, static methods,
and nested types. It defines a contract for classes that implement
it, specifying the methods that must be implemented.
Q.24 what is a static method?
Answer: A static method in Java is a method that belongs to the class
rather than to any individual object instance. It can be called
directly on the class without the need to create an object and is
commonly used for utility or helper methods.
Q.25 what is a static variable?
Answer: A static variable in Java is a class-level variable that is
shared among all instances of the class. It is declared with the
static keyword and is initialized only once, regardless of how many
instances of the class are created.
Q.26 what is method overriding?
Answer: Method overriding is a feature of Java that allows a subclass
to provide a specific implementation of a method that is already
defined in its superclass. It is used to achieve runtime polymorphism
and enables subtype polymorphism.
Q.27 what is method overloading?
Answer: Method overloading is a feature of Java that allows a class
to have multiple methods with the same name but different parameter
lists. It is used to provide multiple ways to invoke a method based
on the number and types of arguments passed to it.
Q.28 what is the difference between method overloading and
method overriding?
Answer: Method overloading involves defining multiple methods with
the same name but different parameter lists within the same class,
while method overriding involves providing a specific implementation
of a method in a subclass that is already defined in its superclass.
Q.29 what is the final keyword?
Answer: The final keyword in Java is used to declare constants,
prevent method overriding, and prevent class inheritance. When
applied to a variable, method, or class, it indicates that its value,
implementation, or inheritance structure cannot be changed,
respectively.
Q.30 what is the abstract keyword?
Answer: The abstract keyword in Java is used to declare abstract
classes and abstract methods. An abstract class cannot be
instantiated directly and may contain abstract methods that must be
implemented by its subclasses.