50 Object Oriented Programming Interview QnA
50 Object Oriented Programming Interview QnA
Programming
Interview QnA
Made by Want More
2. What is a class?
A class is a blueprint or template that defines the structure and behavior of objects. It
defines the properties (attributes) and methods (functions) that objects of that class
will have.
3. What is an object?
4. What is inheritance?
5. What is encapsulation?
Encapsulation is the principle of bundling data and methods within a class, hiding
internal implementation details and providing controlled access through methods or
properties. It ensures data integrity, promotes code organization, and supports
information hiding.
6. What is polymorphism?
7. What is abstraction?
Abstraction is the process of representing essential features of an object while hiding
unnecessary details. It focuses on creating simplified models that capture the core
characteristics and behaviors relevant to the problem domain.
Method overloading is a feature that allows multiple methods with the same name but
different parameters to coexist within a class. The compiler determines the appropriate
method to call based on the number, types, and order of the arguments passed.
A constructor is a special method within a class that is used to initialize objects of that
class. It is called automatically when an object is created and helps set the initial state
of the object.
11. What is the difference between a class method and an instance method?
A class method is a method that belongs to the class itself, rather than an instance of
the class. It can be called directly on the class and does not have access to instance-
specific data. An instance method, on the other hand, is associated with objects of the
class and can access and modify instance-specific data.
Method overloading is a feature that allows a class to have multiple methods with the
same name but different parameter lists. The compiler determines which method to
invoke based on the number, types, and order of the arguments passed.
Composition and inheritance are two ways to achieve code reuse and establish
relationships between classes. Composition is a "has-a" relationship, where a class
contains objects of other classes as its members. Inheritance is an "is-a" relationship,
where a subclass inherits properties and behaviors from a superclass. Composition
offers more flexibility and loose coupling, while inheritance provides a more rigid and
hierarchical structure.
15. What is an abstract class?
An abstract class is a class that cannot be instantiated and serves as a base or parent
class for other classes. It may contain abstract methods, which are methods without
implementation, and concrete methods that provide default behavior. Subclasses
derived from an abstract class must implement all the abstract methods or be
declared abstract themselves.
An abstract class can contain both abstract and concrete methods, whereas an
interface can only have abstract methods. A class can implement multiple interfaces,
but it can only inherit from a single abstract class. Abstract classes can have
constructor definitions, while interfaces cannot. Additionally, abstract classes can have
instance variables, while interfaces cannot.
A static method is a method that belongs to the class itself rather than an instance of
the class. It can be called directly on the class without creating an object. Static
methods are typically used for utility functions or operations that do not depend on
the state of individual objects.
19. What is the difference between a static method and an instance method?
A static method belongs to the class itself and can be called directly on the class, while
an instance method belongs to an object of the class and can only be called on
instances of the class. Static methods do not have access to instance-specific data and
can only access static variables, while instance methods can access both instance
variables and static variables.
Method hiding occurs when a subclass defines a static method with the same name as
a static method in its superclass. The subclass method "hides" the superclass method,
and the choice of which method to call is determined at compile-time based on the
reference type, not the actual object type.
A final class is a class that cannot be inherited or subclassed. It is typically used when a
class is complete and should not be extended further.
Method chaining, also known as fluent interface, is a coding style in which multiple
method invocations are chained together in a single statement. Each method call
returns an object, allowing subsequent method calls to be made on the returned
object. This pattern improves code readability and conciseness.
A singleton class is a class that allows only a single instance to be created and provides
global access to that instance. It is often used when there is a need for centralized
control or coordination.
A static variable is a variable that belongs to the class itself, rather than individual
instances of the class. It is shared among all instances of the class and can be accessed
directly through the class name.
Method synchronization is a technique used to ensure that only one thread can
execute a synchronized method of an object at a time. It prevents multiple threads
from concurrently accessing or modifying shared data, thereby avoiding data
inconsistencies or race conditions.
27. What is the difference between shallow copy and deep copy?
Shallow copy creates a new object that shares the same memory as the original object,
including references to the referenced objects. Deep copy creates a completely
independent copy of the object and all its referenced objects, recursively.
The "this" keyword in Java is a reference to the current object within an instance
method or constructor. It is used to differentiate between instance variables and
parameters or local variables with the same name. It can also be used to invoke other
constructors of the same class.
Method overloading in Java is the ability to have multiple methods with the same
name but different parameter lists within a class. The compiler determines which
method to call based on the number, types, and order of the arguments passed.
Checked exceptions are exceptions that need to be declared in the method signature
or handled using try-catch blocks. They are typically used for recoverable conditions
that the calling code should be aware of and handle. Unchecked exceptions, also
known as runtime exceptions, do not need to be explicitly declared or caught. They are
used for unexpected or unrecoverable conditions such as programming errors or
system failures.
The Java Collections Framework is a set of interfaces, classes, and algorithms that
provide reusable data structures and methods for manipulating collections of objects.
It includes classes like ArrayList, LinkedList, HashMap, and interfaces like List, Set, and
Map.
ArrayList and LinkedList are both implementations of the List interface in the Java
Collections Framework. The main difference between them is in their internal data
structure. ArrayList uses a dynamically resizable array, providing fast random access
but slower insertions and deletions. LinkedList uses a doubly linked list, providing
efficient insertions and deletions but slower random access.
HashSet and TreeSet are both implementations of the Set interface in the Java
Collections Framework. HashSet stores elements in an unordered manner, using
hashing to provide constant-time performance for add, remove, and contains
operations. TreeSet stores elements in a sorted manner, providing log-time
performance for these operations.
36. What is the difference between a deep copy and a shallow copy?
A deep copy creates a completely independent copy of an object and all its referenced
objects, recursively, while a shallow copy creates a new object that shares the same
memory as the original object, including references to the referenced objects.
38. What is the difference between abstract classes and interfaces in Java?
Abstract classes can have both abstract and concrete methods, while interfaces can
only have abstract methods. A class can implement multiple interfaces, but it can only
inherit from a single abstract class. Abstract classes can have constructor definitions,
while interfaces cannot. Additionally, abstract classes can have instance variables,
while interfaces cannot.
39. What is the difference between method overloading and method overriding?
Method overloading occurs when a class has multiple methods with the same name
but different parameter lists. The methods may have different return types, but the
method name must be the same. Method overriding occurs when a subclass provides
its own implementation of a method that is already defined in its superclass. The
method in the subclass must have the same name, return type, and parameters as the
method in the superclass.
The "super" keyword in Java is used to refer to the superclass of a class. It can be used
to access superclass methods, invoke superclass constructors, and differentiate
between superclass and subclass members with the same name.
Access modifiers define the level of access to class members (variables and methods)
from other parts of the program. The three main access modifiers are:
Method hiding occurs when a subclass defines a static method with the same name
and signature as a static method in its superclass. The subclass method hides the
superclass method, and the method chosen for execution depends on the type of the
reference used to invoke it, not the actual type of the object.
The 'this' keyword is a reference to the current object within a class. It is used to
differentiate between class members (variables and methods) and local variables or
parameters that have the same name. It is also used to invoke one constructor from
another constructor in the same class.
The 'super' keyword is used to refer to the superclass (parent class) of a subclass. It can
be used to call the superclass constructor, access superclass methods and variables,
and invoke the superclass implementation of overridden methods.
45. What is the difference between method overloading and method overriding?
Method overloading: Method overloading occurs within the same class and involves
creating multiple methods with the same name but different parameters. The
methods have different signatures and are differentiated based on the number, type,
or order of parameters. Method overloading is determined at compile-time (static
binding).
Method overriding: Method overriding occurs between a superclass and its subclass. It
involves creating a method in the subclass with the same name, return type, and
parameters as the method in the superclass. The subclass method provides a specific
implementation that overrides the superclass method. Method overriding is
determined at runtime (dynamic binding).
Method overloading is a feature in Java that allows a class to have multiple methods
with the same name but different parameters. The methods can have different
numbers of parameters or different types of parameters. During compilation, the
appropriate method is selected based on the arguments passed to it. Method
overloading allows for code reuse and provides a way to create methods with similar
functionality but different input.
A singleton class is a class that allows only a single instance of itself to be created. It
provides a global point of access to that instance. Singleton classes are often used for
resource sharing, logging, caching, or when there should be only one instance of a
class throughout the application.
The 'this' keyword is a reference to the current object within a class. It is used to
differentiate between class members (variables and methods) and local variables or
parameters that have the same name. It is also used to invoke one constructor from
another constructor in the same class.