DEV Community

Cover image for OOPS Interview Questions
Jemmy Dalsaniya
Jemmy Dalsaniya

Posted on

OOPS Interview Questions

You can get the oops code here

Table of Content:

  1. What is OOPS?
  2. What is a Class?
  3. What is an Object?
  4. What is Attribites
  5. What are Methods
  6. What is the size of the class when it is empty?
  7. What are Access Modifiers?
  8. What is Padding?
  9. What is Greedy Alignment?
  10. What does this mean?
  11. Shallow vs Deep Cloning
  12. What is a Destructor?
  13. What is the const Keyword?
  14. What is an Initialization List?
  15. What is the Static Keyword?
  16. What are the Pillars of OOPS?
  17. What is Encapsulation?
  18. What is Inheritance?
  19. What is Polymorphism?
  20. What is Abstraction?
  21. What is an Abstract Class?
  22. What is a Pure Virtual Function?
  23. What is a Virtual Function?
  24. What is an Interface?
  25. What is a Friend Function?
  26. What are other programming paradigms besides OOP?
  27. What is a Constructor? & Its types
  28. Structure vs Class
  29. What is an Exception?
  30. What is meant by Exception Handling?
  31. What is Garbage Collection in OOP?
  32. What are the limitations of OOP?
  33. What is Constructor Chaining?
  34. What is Coupling in OOP?

OOPS

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a entity that has unique attributes and behavior.

Class:

Classes are user-defined data types that act as the template individual objects, attributes and methods.

Object:

Objects are the instance of the classes. An object can be defined as a entity that has unique attributes and behaviour.

Attributes:

Attributes represent the state of an object. In other words, they are the characteristics that distinguish classes.

Method or Behaviour:

Methods are functions that objects can perform. They are defined inside a class that describe the behaviors of an object.

What is the size of the class when it is empty??

-> 1 byte because for uniquely identify the object created or to keep track.

Access Modifiers:

It is a restriction of where we can Access the class members.

-> There are 3 Types of Access Modifiers:

Public : Can access the member from any where.
Private : Member can access within the class only
Protected : Member can acces within the class and its child members

Padding:

Padding is the technique of adding extra bytes to a structure or class to align its data members to specific boundaries. This is done to ensure that the data members are accessed efficiently by the processor, which typically fetches data from memory in chunks of fixed sizes, such as 4 or 8 bytes.

Greedy:

Greedy alignment is a technique used to minimize padding by ordering the data members of a class in decreasing order of size. This ensures that the largest data members are placed first in the class, minimizing the amount of padding required to align the subsequent data members.

this:

Refer to the current object of the class.(pointer to current object)

Shallow vs Deep Cloning:

When using copy constructors, shallow copy is when objects share same memory locations for variables. While deep copy is when objects have their own individual memory locations.

-> In shallow cloning if the member is dynamically created then creating the copy of that object will point to the same memory reference thus making change in any property will change for all the object copied but this does not happen in deep copy since they allocate different memory locations.

Destructor:

Used to deallocate the memory which is being allocate previously. It should not have parameter and return value. And starting with the '~' + 'Classname'

Eg: ~Hero(){}

-> For the statistically allocated property Destructor is automatically called. But for the dynamically allocated member you have to manually call the destructor

Eg : ~Hero(){ delete name; }

const Keyword:

πŸ”— https://www.javatpoint.com/const-keyword-in-cpp

Initialization List:

πŸ”— https://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c/

Static Keyword:

When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of the variable in the previous call gets carried through the next function call.

Static Member Function
In a class static member is the function that is declared as static because of which function attains certain properties as defined below:

->A static member function is independent of any object of the class.

->A static member function can be called even if no objects of the class exist.

-> A static member function can also be accessed using the class name through the scope resolution operator.

->A static member function can only access static data members and static member functions inside or outside of the class. Static member functions have a scope inside the class and cannot access the current object pointer.

->You can also use a static member function to determine how many objects of the class have been created.


Pillars of OOPS

Encapsulation:

Encapsulation: Wrapping of the data members and function into a single entity is called Encapsulation.

Fully Encapsulated Class: The class is said to be Fully Encapsulated when all its members or properties is marked as private.

Why Encapsulation is needed ??

-> It is used for data hiding or information hiding.

-> Advantages :

  • Data hiding which increase the security concern.
  • If we want we can make the class read-only.
  • Code Resuability

Inheritance:

Inheritance: Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.

Types of Inheritance:

  • Single Level : The inheritance in which a single derived class is inherited from a single base class is known as the Single Inheritance.
  • Multilevel: When one class inherits another class it is further inherited by another class. It is known as multi-level inheritance.
  • Multiple : Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.
  • Hierarchical: one base class is inherited by more than one derived class.
  • Hybrid :The process of combining more than one type of Inheritance together while deriving subclasses in a program is called a Hybrid Inheritance.

Polymorphism:

Polymorphism means, the same entity (function or object) behaves differently in different scenarios.

Types of Polymorphism:

  • Compile Time Polymorphism:(also called static polymorphism) When the relationship between the definition of different functions and their function calls, is determined during the compile-time, it is known as compile-time polymorphism.

It has two types : 1) Function Overloading and 2) Operator Overloading

  • Run Time Polymorphism:(also called dynamic polymorphism): In runtime polymorphism, the compiler resolves the object at run time and then it decides which function call should be associated with that object. It is also known as dynamic or late binding polymorphism.

The type of runtime polymorphism is : Method Overriding.

Rules for method Overriding:

  • method must have same name
  • must have same parameter
  • possible through inheritance only

Abstraction:

It refers to showing only relevant information to the outside world. In simple words, we can say it means hiding any background information from the outside world.

It can be done through 3 ways : 1) Using access modifiers 2) Abstract Class 3) Using Data Abstraction

Abstract CLass:

Abstract class must have at least one pure virtual function. It may have variables and normal functions. The derived classes of an abstract class must implement all the pure virtual functions of their base class or else they too become abstract.

Pure Virtual Function:

A pure virtual function (or abstract function) in C++ is a virtual function for which we can have an implementation, But we must override that function in the derived class, otherwise, the derived class will also become an abstract class. A pure virtual function is declared by assigning 0 in the declaration.

Virtual Function:

A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime.


Interface:

πŸ”— https://www.geeksforgeeks.org/cpp-program-to-create-an-interface/

Friend function:

A friend function is a function that isn't a member of a class but has access to the class's private and protected members.

πŸ”— https://www.geeksforgeeks.org/friend-class-function-cpp/


What are some other programming paradigms other than OOPs?

-> Programming paradigms refers to the method of classification of programming languages based on their features. There are mainly two types of Programming Paradigms:

1) Imperative Programming Paradigm
2) Declarative Programming Paradigm

Now, these paradigms can be further classified based:

  1. Imperative Programming Paradigm: HOW to execute program logic and defines control flow as statements.

This can be further classified as:
a) Procedural Programming Paradigm: Procedural programming specifies the steps a program must take to reach the desired state, usually read in order from top to bottom.
b) Object-Oriented Programming or OOP: Object-oriented programming (OOP) organizes programs as objects, that contain some data and have some behavior.
c) Parallel Programming: Parallel programming paradigm breaks a task into subtasks and focuses on executing them simultaneously at the same time.

  1. Declarative Programming Paradigm: WHAT to execute and defines program logic, but not a detailed control flow.

Declarative paradigm can be further classified into:
a) Logical Programming Paradigm: refers to a set of sentences expressing facts and rules about how to solve a problem
b) Functional Programming Paradigm: Functional programming is a programming paradigm where programs are constructed by applying and composing functions.
c) Database Programming Paradigm: Database programming model is used to manage data and information structured as fields, records, and files.


What is a constructor? & Its type

-> Constructors are special methods whose name is the same as the class name. The constructors serve the special purpose of initializing the objects.

Default constructor: The default constructor is the constructor which doesn’t take any argument. It has no parameters.

Parameterized constructor: The constructors that take some arguments are known as parameterized constructors.

Copy constructor: A copy constructor is a member function that initializes an object using another object of the same class.


Structure vs CLass:

-> CLass:

-> Class is saved in heap memory.
-> It is normally used for data abstraction and further inheritance.
->Members of a class are private by default.

Structure:

-> The structure is saved in the stack memory
-> Abstraction is not possible in abstraction.
->Members of a structure are public by default.


What is an exception?

-> An exception can be considered as a special event, which is raised during the execution of a program at runtime, that brings the execution to a halt.


What is meant by exception handling?

-> The exceptions can be handled in the program beforehand and prevent the execution from stopping. This is known as exception handling. Try-catch is the most common method used for handling exceptions in the program.


What is meant by Garbage Collection in OOPs world?

-> Garbage collection refers to this mechanism of handling the memory in the program. Through garbage collection, the unwanted memory is freed up by removing the objects that are no longer needed.


What are the limitations of OOPs?

->Usually not suitable for small problems
Takes more time to solve the problem
Requires proper planning
The programmer should think of solving a problem in terms of objects.


What is constructor chaining?

Constructor chaining is a method to call one constructor from another concerning a current object reference. It can be done in two ways: –

Using the β€œthis” keyword, the reference can be made to the constructor in the current class.
To call the constructor from the base class β€œsuper” keyword will be used.


What is Coupling in OOP, and why is it helpful?

-> The degree of dependency between the components is called coupling.

Types of Coupling:

Tight Coupling – If the dependency between components is high, these components are called tightly coupled.

Loose Coupling – If the dependency between components is low, it is called loose coupling.

Loose coupling is preferred because of the following reasons:-
It increases the maintainability of code
It provides reusability of code

Top comments (0)