OOPS Interview Question & Answers
- What is OOPs?
Object-Oriented Programming (OOPS) is a programming paradigm that is based on the concept of “objects” which can contain data and code that manipulate the data. OOP is focused on encapsulation, inheritance, and polymorphism.
- What is Encapsulation?
Encapsulation is the process of hiding the internal details of an object from the outside world. It is achieved by using access modifiers such as public, private, and protected to control the visibility of the object’s properties and methods.
- What is Inheritance?
Inheritance is the process of creating new classes from existing classes. The new class, known as the derived class, inherits the properties and methods of the base class. Inheritance promotes code reusability and reduces duplication of code.
- What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. In OOP, polymorphism is achieved through method overloading and method overriding. Method overloading allows multiple methods with the same name to be defined in a class, while method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
- What is Abstraction?
Abstraction is the process of hiding the implementation details of an object and exposing only the necessary details to the user. It is achieved by using abstract classes and interfaces.
- What is the difference between an abstract class and an interface?
An abstract class is a class that cannot be instantiated and is used as a base class for other classes. It can contain abstract and non-abstract methods, and can also have fields and properties. An interface, on the other hand, is a collection of abstract methods and constants that define a contract for implementing classes. An interface cannot contain fields or properties, and it cannot be instantiated.
- What is the difference between a class and an object?
A class is a blueprint or template for creating objects. It defines the properties and methods that an object of that class will have. An object, on the other hand, is an instance of a class. It is created from the class blueprint and has its own unique set of values for the class properties.
- What is a constructor?
A constructor is a special method in a class that is called when an object of that class is created. It is used to initialize the properties of the object and set them to their default values.
- What is a destructor?
A destructor is a special method in a class that is called when an object of that class is destroyed or goes out of scope. It is used to perform any necessary cleanup operations before the object is destroyed.
- What is method overloading?
Method overloading is the process of defining multiple methods with the same name in a class. The methods must have different parameters, and the compiler uses the type and number of the parameters to determine which method to call.
- What is method overriding?
Method overriding is the process of providing a new implementation for a method that is already defined in a superclass. The new implementation is provided in a subclass, and it must have the same name and parameters as the original method.
- What is the final keyword in Java?
The final keyword in Java is used to indicate that a variable, method, or class cannot be changed or overridden. A final variable is a constant and cannot be reassigned once it has been initialized. A final method cannot be overridden by a subclass, and a final class cannot be extended by a subclass.
- What is a static method?
A static method is a method that belongs to the class and not to any instance of the class. It can be called without creating an object of the class, and it can access only static variables and other static methods.
- What is a Static Variable?
A static variable is a variable that belongs to the class and not to any instance of the class. It is shared by all objects of the class, and it can be accessed using the class name instead of an object reference.
- What is a singleton class?
A singleton class is a class that allows only one instance of itself to be created. It is often used to provide a single point of access to a resource or service that needs to be shared by multiple objects in an application.
- What is a thread?
A thread is a lightweight process that can execute concurrently with other threads in a program. Each thread has its own call stack and can access shared data in the program.
- What is synchronization?
Synchronization is the process of coordinating the execution of multiple threads to ensure that they access shared resources in a safe and consistent manner. It is typically achieved using locks or semaphores to control access to shared data.
- What is a deadlock?
A deadlock is a situation in which two or more threads are blocked waiting for each other to release resources that they need to continue executing. Deadlocks can occur when two or more threads acquire locks on resources in a different order, or when a thread holds a lock while waiting for another resource that is already locked by another thread.
- What is the difference between an abstract class and a concrete class?
An abstract class is a class that cannot be instantiated and is used as a base class for other classes. It can contain abstract and non-abstract methods, and it can also have fields and properties. A concrete class, on the other hand, is a class that can be instantiated and used directly. It must implement all of the abstract methods defined in its base classes.
- What is a package?
A package is a grouping mechanism in Java that allows related classes, interfaces, and sub-packages to be organized together. Italso provides a way to control access to the classes and interfaces within the package by using access modifiers such as public, private, and protected.
- What is a garbage collector?
A garbage collector is a program that automatically frees memory that is no longer being used by a program. It identifies objects that are no longer reachable and releases the memory that they occupy, making it available for reuse by the program.
- What is a stack and a heap?
A stack is a data structure that is used to store temporary data, such as method calls and local variables. It is organized as a LIFO (Last In, First Out) structure, where the most recently added item is the first one to be removed.
A heap is a data structure that is used to store objects and other data that need to persist beyond the lifetime of a single method call. It is organized as a dynamic structure, where objects are allocated and deallocated as needed.
- What is an interface in Java?
An interface in Java is a collection of abstract methods and constants that define a contract for implementing classes. It cannot contain fields or properties, and it cannot be instantiated. Classes that implement an interface must provide an implementation for all of its methods.
- What is a polymorphic reference?
A polymorphic reference is a variable that can refer to objects of different classes at different times. This is possible because of polymorphism, which allows objects to take on many different forms and behave differently depending on their type.
- What is a factory method?
A factory method is a method that is used to create objects without exposing the creation logic to the client. It is often used in conjunction with the Factory Method design pattern to provide a way to create objects of a specific type without the client having to know the details of how they are created.
- What is the difference between composition and inheritance?
Composition and inheritance are two different ways of achieving code reuse in object-oriented programming.
Inheritance is a mechanism where a subclass inherits the properties and behavior of its parent class. It allows the subclass to reuse the code from the parent class, and also enables polymorphism, where objects of different classes can be treated as if they belong to a common superclass.
Composition, on the other hand, is a mechanism where an object is composed of other objects. It allows for greater flexibility in defining the behavior of objects, as well as better encapsulation and separation of concerns.
The main difference between the two is that inheritance is a “is-a” relationship, where the subclass is a type of the parent class, while composition is a “has-a” relationship, where an object has a reference to other objects that it uses to implement its behavior.
- What is the difference between a constructor and a method?
A constructor is a special method that is used to initialize an object when it is created. It has the same name as the class and does not have a return type. It is called automatically when an object is created, and can be used to set the initial values of the object’s fields.
A method, on the other hand, is a regular function that belongs to a class and can be called on an object of that class. It has a name, a return type, and may take parameters. It is used to implement the behavior of the object and can be called multiple times on the same object.
- What is the difference between overriding and overloading?
Overriding is a mechanism where a subclass provides its own implementation of a method that is already defined in its parent class. The method must have the same signature (i.e., name, return type, and parameters) as the parent class method.
Overloading, on the other hand, is a mechanism where multiple methods can have the same name but different signatures. This allows methods with different functionality to be grouped under the same name for convenience.
- What is a virtual method?
A virtual method is a method that can be overridden by subclasses to provide their own implementation. It is marked with the virtual keyword in C# or the virtual and override keywords in Java. When a virtual method is called on an object, the runtime determines which implementation to use based on the actual type of the object, rather than the declared type of the variable that references it.
- What is the difference between private and protected access modifiers?
Private access modifiers restrict access to a class member to only the class that defines it. Protected access modifiers allow access to a class member by the class that defines it and its subclasses.
- What is an abstract class?
An abstract class is a class that cannot be instantiated and is designed to be a base class for other classes. It contains one or more abstract methods, which are methods without an implementation. Subclasses of an abstract class must provide implementations for all the abstract methods.
- What is a final class?
A final class is a class that cannot be sub classed. It is often used to prevent other classes from modifying or extending its behavior. In addition, all methods and fields of a final class are implicitly final as well.
- What is an anonymous class?
An anonymous class is a class that is defined on the fly without a name. It is often used to define a subclass or implement an interface without having to create a separate class for it. Anonymous classes are defined within the scope of a method and are often used for event handlers and callbacks.
- What is a static method?
A static method is a method that belongs to a class rather than to an instance of a class. It can be called using the class name rather than an object reference. Static methods cannot access non-static (instance) variables or methods.
- What is the difference between an instance variable and a class variable?
An instance variable is a variable that is declared within a class but outside of any method. Each object of the class has its own copy of the instance variable, and changes made to it affect only that object.
A class variable, on the other hand, is a variable that is shared among all objects of the class. It is declared with the static keyword, and changes made to it affect all objects of the class.
- What is the difference between an abstract class and an interface?
An abstract class is a class that cannot be instantiated and is designed to be a base class for other classes. It can contain both abstract and non-abstract methods, and can have instance variables. A subclass of an abstract class must provide implementations for all abstract methods.
An interface, on the other hand, is a collection of abstract methods and constants that define a contract for implementing classes. It cannot contain fields or properties, and it cannot be instantiated. Classes that implement an interface must provide an implementation for all its methods.
- What is the purpose of the finalize() method?
The finalize() method is a method that is called by the garbage collector when an object is about to be reclaimed. It is used to perform any necessary cleanup before the object is destroyed, such as releasing resources or closing open files. However, the use of finalize() is generally discouraged, as its behavior is unpredictable and it can cause performance issues.