December 11, 2024

OOPS Interview Question & Answers

OOPS
Share :

OOPS Interview Question & Answers


  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.


For More Updates Join Our Channels :