OOPS Interview Questions and Answers
50 OOPS Interview Questions
Are you getting ready for an Object-Oriented Programming (OOP) interview? PrepInsta offers a comprehensive collection of typical OOPS Interview Questions and answers. Whether you are a fresher or an experienced programmer, you can access up-to-date information on OOPS topics. Prepare for your interview effectively with PrepInsta’s valuable resources.
OOPS Introduction
Object-Oriented Programming (OOP) is a programming paradigm that is widely used in software development. It is based on the concept of “objects,” which are self-contained units that represent real-world entities or concepts and encapsulate both data and the operations that can be performed on that data. OOP is designed to make software development more organized, efficient, and easier to maintain.
Uses of OOPS
- Software Development: OOP is fundamental in Java, C++, Python, and C# for building robust apps.
- Web Development: OOP structures web code and interacts with databases.
- Game Development: Games use OOP for objects and behaviors.
- Mobile Apps: Android (Java/Kotlin) and iOS (Swift/Objective-C) employ OOP.
- GUI Development: OOP simplifies GUI creation.
- Embedded Systems: OOP manages hardware interactions.
- Data Modeling: OOP designs structured databases.
OOPS Interview Questions and Answers For Freshers
Ques 1: What is a class?
Ans.
A class is nothing more than a description of an object. It is a model, design, or prototype that describes an object’s features.
Ques 2: What is the need for OOPs?
Ans.
Class is a blueprint or template from which objects are created and describes the contents of the object. A class should be defined before creating an object.
Ques 3: What is an object?
Ans. An object is a self-contained unit that represents a real-world entity or concept in programming, containing both data (attributes) and functions (methods) that operate on that data.
Ques 4: Define a constructor.
Ans.
A constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation.
Rules for constructor are:
- Constructor Name should be the same as the class name.
- A constructor must have no return type.
Ques 5: Define Destructor.
Ans.
In Object-Oriented Programming (OOP), a destructor is a special method or function within a class that is used to clean up or release resources when an object of that class is no longer in use. It helps in proper resource management, such as releasing memory or closing files, when the object is destroyed or goes out of scope. Destructors are particularly important in languages like C++ and are automatically invoked when an object is deleted or goes out of scope.
Ques 6: What is the difference between multiple and multilevel inheritance?
Multiple Inheritance | Multilevel Inheritance |
---|---|
when a class inherits more than one base class | when a class inherits from another class which itself is a subclass of some other base class |
Example:- A class defining a child inherits from two base classes Mother and Father | Example:- A class describing a sport car will inherit from one base class car which in turn inherits another class Vehicle |
Ques 7: What is a superclass?
Ans.
A superclass or base class is a class that acts as a parent to some other class or classes.
For example, the Vehicle class is a superclass of Class Car. or, BMW is a type of car, so the superclass for BMW is Class Car.
Ques 8: Differentiate between overloading and overriding.
Ans.
overloading | overriding |
---|---|
two or more methods have same name but different parameters or signatures | child class redefining methods present in the base class or parent class with the same parameters/signatures |
resolved during compile time | resolved during run time |
Ques 9: What is a friend function?
Ans.
A friend function is capable of accessing private/protected data of a class in which it is declared as a friend in spite of not being a member of that class.
Ques 10: What is a virtual function?
Ans.
Virtual function is a special member function that is declared within a base class and redefined by derived class to go with its own task. It is declared using a token, and is implemented by using the virtual keyword.
Ques 11: What is exception handling?
Ans.
Exception handling in Object-Oriented Programming is a very important concept that is used to manage errors. An exception handler allows errors to be thrown and caught and implements a centralized mechanism to resolve them.
Ques 12: What is Encapsulation?
Ans.
Encapsulation is a feature of an entity that holds all secret data. The members of that class can only see the hidden details. Public, Protected, Private are the different levels.
Ques 13: What is Inheritance? Explain the use of Inheritance?
Ans.
Inheritance is a concept where one class shares the structure and behavior defined in another class. Inheritance applied to one class is called Single Inheritance, and if it depends on multiple classes, then it is called multiple Inheritance.
Uses:-
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
Ques 14: What are Static Binding and Dynamic Binding?
- Static Binding is a binding in which the name can be combined with the class during collection time, and it is also called early binding.
- Dynamic Binding is a binding in which name can be identified with the class during execution time, and it is also known as Late Binding.
Ques 15: Can you call the base class method without creating an instance?
- It is a static method.
- The base class is inherited by some other subclass.
Ques 16: How Can We Call The Base Method Without Creating An Instance?
Ans.
Yes, it is possible to call the base method without creating an instance. And that method should be the static method. Doing inheritance from that class use the Base Keyword from a derived class.
Ques 17: What are the differences between method and constructor?
Ans.
Constructors | Methods |
---|---|
constructor name should match with the name of the class | Methods should not have the same name as the class name |
used to initialize and allocate memory of to the object | used to execute certain statements written inside them |
constructors are invoked by the system whenever objects are created | invoked when it is called |
they are invoked using new keyword while creating an instance of the class | invoked during program execution |
does not have a return type | has a return type |
cannot be inherited by subclass | can be inherited by a subclass |
Ques 18: What is a need for Object-oriented programming?
Ans
Need for OOPS:-
- more protection and control over data access
- OOPS offers data hiding functionalities
- code reusability
- data redundancy
- code maintenance
Ques 19: What are the various types of constructors?
Ans.
There are three types of constructors:
- Default Constructor – With no parameters.
- Parametric Constructor – These constructors are those having parameters. Create a new instance of a class and also pass arguments simultaneously.
- Copy Constructor – This creates a new object as a copy of an existing object.
Ques 20: What is a try/ catch block?
Ans.
Finalize is a method used to free up unmanaged resources and cleanup before Garbage Collection(GC). It performs memory management tasks.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
JAVA Interview Questions and Answers For Experienced.
Ques 21: What are the various types of inheritance?
Ans.
Various types of inheritance are :
- Single Inheritance: Single child class inherits characteristics of the single-parent class.
- Multiple Inheritance: One class inherits features of more than one base class and is not supported in Java, but the class can implement more than one interface.
- Multilevel Inheritance: A class can inherit from a derived class making it a base class for a new class, for example, a Child inherits behavior from his father, and the father has inherited characteristics from his father.
- Hierarchical Inheritance: One class is inherited by multiple subclasses.
- Hybrid Inheritance: This is a combination of single and multiple inheritances.
Ques 22: What are the disadvantages of OOPS?
- Cannot be applied in all scenarios
- Programs are comparatively more complex than those of Procedural programming
- OOPS Projects need more planning and designing as using oops is tricky
- Long programs need more time for testing
Ques 23: What is hybrid inheritance?
Ans.
Hybrid inheritance is when a class in object-oriented programming inherits properties and behaviors from two or more parent classes. It’s like mixing different types of inheritance to reuse code and create class hierarchies.
Ques 24: What is an interface?
Ans.
An interface in OOP is a set of method rules that a class must follow if it claims to implement that interface. It defines what methods a class should have but doesn’t provide their actual code. Interfaces promote a common structure among classes, enabling flexibility and consistency in code design through polymorphism.
Ques 25: How is data abstraction accomplished?
- Class definition using data and methods.
- Regulating who has access to private or protected data.
- Making use of informative method signatures.
- Encapsulating data and techniques.
- Passing on and distributing code.
- Making consistent interactions with polymorphism.
- Making use of interfaces and abstract classes.
- Making use of design patterns.
- Providing detailed records.
Ques 26: Can we run a Java application without implementing the OOPs concept?
Ans.
Yes, it is possible to run a Java program without fully utilizing OOP ideas, but because OOP is what gives Java its strength, it is usually recommended to do so for better code structure and reusability.
Ques 27: What is Abstraction?
Ans.
Abstraction is the process of simplifying complex things by focusing on what is important and disregarding irrelevant aspects. It involves building models in programming that capture the fundamental properties of an object or system, making it easier to work with and comprehend.
Ques 28: What other paradigms of programming exist besides OOPs?
Ans.
Types programming paradigms:
Impretive Programming: Imperative programming is about giving explicit step-by-step instructions, often involving changing program state with variables and control structures like loops and conditionals.
- Structured Programming: Structured programming is about breaking code into organized pieces (procedures/functions), using clear control flow (sequences, loops, conditionals), and avoiding the “goto” statement. It makes code readable, maintainable, and reduces errors.
- Object Oriented Programming: Object-Oriented Programming (OOP) is about organizing code into objects with data and behaviors. It promotes code reuse, modularity, and simplifies complex systems.
- Procedural Programming: Procedural programming organizes code into functions or procedures for sequential execution, focusing on modularity and limited code reuse.
- Functional Programming: Functional programming is about using functions as the primary building blocks of code, focusing on immutability, avoiding side effects, and using high-order functions. It leads to concise and predictable code.
- Logic Programming: Logic programming uses logical rules and facts to define programs. It’s declarative, relies on an inference engine, and is well-suited for domains like artificial intelligence and rule-based systems. Prolog is a notable language for logic programming.
These paradigms provide several approaches to structuring and solving programming challenges
Ques 29: What is the difference between Structured Programming and Object Oriented Programming?
Ans.
Procedural Programming:
- Focuses on functions or procedures.
- Makes use of simple data structures.
- Reusability of code is limited.
- There is little emphasis on modeling real-world things.
- No built-in inheritance or polymorphism support.
Object-Oriented Programming (OOP):
- Objects with data and action are the focus of OOP.
- Data and operations are encapsulated within objects.
- Encourages code reuse through inheritance.
- Places a premium on simulating real-world entities and connections.
- Allows for inheritance and polymorphism.
Ques 30: What are the limitations of OOPs?
Ans.
Limitations of OOPS
- Large project complexity.
- The possibility of performance overhead.
- A difficult learning curve.
- Excessive abstraction might lead to code that is difficult to understand.
- Not suitable for all domains (for example, real-time systems).
- Lack of adaptability in class structures.
- Concurrency management can be difficult.
- There is a lack of standardization.
- Increased resource utilization.
- Potential constraints in performance.
- Limited functional programming support.
Ques 31: How can you prevent inheritance in a class?
final
):In Java and C++, you can prevent inheritance by declaring a class as final
. A final
class cannot be extended (subclassed).public final class MyFinalClass { // Class code here }
class MyFinalClass final{ // Class code here }
Ques 32: What are the SOLID principles of object-oriented design?
Ans.
Certainly, the SOLID principles of object-oriented design may be summarized as follows:
- Single Responsibility Principle (SRP): A class should only change for one reason.
- Open/Closed Principle (OCP): Software things should be extensible but not modifiable.
- Liskov Substitution Principle (LSP): Subtypes must be interchangeable with their base types without causing program behavior to change.
- Interface Segregation Principle (ISP): states that clients should not be compelled to rely on interfaces that they do not use.
- Dependency Inversion Principle (DIP): states that high-level modules should not be dependent on low-level modules. Both should be predicated on abstractions.
Ques 33: What is a getter method and a setter method, and why are they used?
Ans.
Getter Method:
- To obtain the value of an object’s attribute.
- Frequently referred to as getPropertyName() or simply propertyName().
- Controlled read access is provided.
Setter Method:
- To modify the value of an object’s attribute.
- setPropertyName(newValue) is a common name for this method.
- Controlled write access with validation if necessary.
Ques 34: How run time polymorphism is achieved?
Ans.
Run time polymorphism helps resolve the problem of early binding at compile time.
#include using namespace std; // This is Base class class Base { public: virtual void show() { cout << "Showing Base Class" << endl; } }; // This is Derived class function class Derived : public Base { public: // This function is declared in the base class as well void show() { cout << "Showing Derived Class" << endl; } }; int main() {
// pointer reference to base class Base *base_object;
// creating derived class object Derived derived_object;
// mapping base object to address of derived class object base_object = &derived_object; // since we use pointers so we use -> rather than .
base_object->show(); return 0; }
Ques 35: Can you explain what Access Modifiers are?
- Private
- Public
- Protected
- Friend
- Protected Friend
Ques 36: What are the limitations of inheritance?
- Increases the time and effort required to execute a program as it requires jumping back and forth between different classes
- The parent class and the child class get tightly coupled
- Any modifications to the program would require changes both in the parent as well as the child class
- Needs careful implementation else would lead to incorrect results
Ques 37: What is the difference between Abstraction and Encapsulation?
Abstraction | Encapsulation |
---|---|
Gives a class a general structure and leaves the implementation to the implementers. | Creates and defines an object’s permissions and constraints as well as the variables and methods that make up the object. |
Abstraction is accomplished by the use of an interface and an abstract class. | Encapsulation is accomplished by the use of four different access level modifiers: public, protected, no modifier and private. |
Ques 38: What is the difference between a class and a structure?
Class | Structure |
---|---|
Class is a referencing type. | Structure is a value type. |
CLR allocates memory for its instance in heap. | Memory is allocated on the stack. |
Support Inheritance. | Does not support Inheritance. |
Variables of a class can be assigned as null. | Structure members cannot have null values. |
Class can contain constructor/destructor | Structure does not require constructor/destructor and members can be initialized automtaically. |
Ques 39: What Are Different Types Of Arguments?
- Call by Value – Value passed will get modified only inside the function, and it returns the same value whatever it is passed into the function.
- Call by Reference – Value passed will get modified both inside and outside the functions and it returns the same or different value.
Ques 40: Why Java does not support multiple inheritance?
Ans.
Java was designed to be a simple language and multiple inheritance introduces complexities like the diamond problem. Inheriting states or behaviors from two different type of classes is a case which in reality very rare and it can be achieved easily through an object association.
Ques 41: What are similarities between a class and a structure?
- Access specifiers, such as public, private, and protected, are identically used in structures and classes to restrict the access of their data and methods outside their body.
- The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
- Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
- Both structures and classes can implement interfaces to use multiple-inheritance in code.
- Both structures and classes can have constructors with parameters. Both structures and classes can have delegates and events.
Ques 42: Explain Is Java a pure Object Oriented language.
- Java supports and uses primitive data types such as int, float, double, char, etc.
- Primitive data types are stored as variables or on the stack instead of the heap.
- In Java, static methods can access static variables without using an object, contrary to object-oriented concepts.
Ques 43: How many types of inheritance are present?
Ans.
Encapsulation is a feature of an entity that holds all secret data. The members of that class can only see the hidden details. Public, Protected, Private are the different levels.
Ques 44: What are the various types of constructors in C++?
class A { int x; A() { x = 0; } }Parameterized constructor: The constructors that take some arguments are known as parameterized constructors.
class A { int x; A(int y) { x = y; } }Copy constructor: A copy constructor is a member function that initializes an object using another object of the same class.
class A { int x; A(int y) { x = y; } // Copy constructor A(A a) { x = a.x; } }
Ques 45: What is casting?
Ans.
In C++ we create a pointer reference between parent and child classes and form an “is a” relationship between them, This is known as casting and they are of two types:-
- Upcasting
- Downcasting
Ques 46: What are access specifiers?
Ans.
Access specifiers are used to hide or show data to the end user. It is used in data hiding or data abstraction.
Ques 47: What is an abstract class?
Ans.
Abstract classes are special classes which are declared but are initialized, which means that it is not possible to create an object for the abstract class.
Ques 48: What are the characteristics of an abstract class?
Ans.
- There can only be one class
- Derived class used abstract class to create interfaces
- They are used for upcasting
Ques 49: What is the difference between extends and implements?
Extends | Implements |
---|---|
A class can extend another class interface | A class can implement an interface |
A subclass extending superclass may not override all of the superclass methods | A class implementing interface has to implement all the methods of the interface |
A class can only extend a single superclass | A class can implement any number of interfaces |
An interface can extend more than one interfaces | An interface cannot implement any other interface |
Syntax: Class child Extend Class Parent | Syntax: Class Hybrid implements Rose |
Ques 50: What is Abstract Class and Abstract Method?
- An object cannot be inherited from the abstract class.
- Subclass created or inherit abstract class to access members of the abstract class.
- An abstract class can contain abstract methods or non-abstract methods.
- An abstract method has a signature but does not have a body
- It is compulsory to override abstract methods of the superclass in their sub-class
- Class containing abstract method should be made abstract class
Getting Ready for Interviews
Also Check:-
- C++ Technical Interview Questions
- OS Technical Interview Questions
- C Technical Interview Questions
- JAVA Technical Interview Questions
- Python Technical Interview Questions
- DSA Technical Interview Questions
- DBMS Technical Interview Questions
- Software Engineering Question
- DSA Technical Interview Questions For Freshers
- Github Technical Interview Questions
- SQL Technical Interview Questions
- Most Asked Coding Question
Join Our Interview Course Now to Get Yourself Prepared -
Join Our Interview Course Now to Get Yourself Prepared
Prepare for the interview process in both Service and Product Based companies along with Group Discussion, Puzzles, Resume Building, HR, MR and Technical Interviews.
Login/Signup to comment