[go: up one dir, main page]

0% found this document useful (0 votes)
28 views6 pages

OOPS_Questions

Uploaded by

anishsarawgi9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

OOPS_Questions

Uploaded by

anishsarawgi9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

# Interview Questions

1. Why do we use OOPs?


Ans: It gives clarity in programming and allows simplicity in solving complex problems.
- Data and code are bound together by encapsulation.
- Code can be reused, and it reduces redundancy.
- It also helps to hide unnecessary details with the help of Data Abstraction.
- Problems can be divided into subparts.
- It increases the readability, understandability, and maintainability of the code.

2. What are the differences between the constructor and the method?
Constructor:
- A special type of function that is automatically called when an object is created.
- It is used to initialize the object.
- It has the same name as the class and no return type.
Method:
- A regular function defined inside a class.
- It is called explicitly to perform a certain task or operation.
- It can have any name and must have a return type.

3. What are the main features of OOPs?


- Inheritance
- Encapsulation
- Polymorphism
- Data Abstraction

4. What are the disadvantages of OOPs?


- Requires pre-work and proper planning.
- In certain scenarios, programs can consume a large amount of memory.
- Not suitable for a small problem.
- Proper documentation is required for later use.

5. What is the difference between class and structure?


Class:
- User-defined blueprint from which objects are created.
- It consists of methods or sets of instructions that are to be performed on the objects.
Structure:
- A structure is basically a user-defined collection of variables of different data types.

6. What is the difference between a class and an object?


Class:
- A class is a blueprint or template that defines properties (attributes) and behaviors (methods) for objects.
- It does not occupy any memory until objects are created from it.

Object:
- An object is an instance of a class, representing a specific example of the data and behavior defined by the class.
- It occupies memory and allows you to access class methods and attributes.
7. Does C++ compiler create a default constructor when we write our own?
In C++, the compiler by default creates a default constructor for every class. But if we define our own constructor,
the compiler doesn’t create the default constructor.

8. Explain constructor in C++.


A constructor is a special member function automatically called when an object is created. A constructor initializes
the class data members with a garbage value if we don’t put any value to it explicitly.

9. What do you mean by constructor overloading?


The concept of having more than one constructor with different parameters to perform a different task is known as
constructor overloading.

10. Explain Destructor in C++.


A destructor is a special member function that works just opposite to a constructor; unlike constructors that are
used for initializing an object, destructors destroy (or delete) the object. The purpose of the destructor is to free
the resources that the object may have acquired during its lifetime.

11. What is a copy constructor?


These are a particular type of constructor that takes an object as an argument and copies values of one object’s
data members into another object. In this constructor, we pass the class object into another object of the same
class.

12. How many types of constructors are there?


There are three types of constructors in C++:
- Default constructor
- Parameterized Constructor
- Copy Constructor

13. When should the destructor use delete to free the memory?
If the object is created using `new` or the constructor uses `new` to allocate memory that resides in the heap
memory or the free store, the destructor should use `delete` to free the memory.

14. What is the return type of constructor and destructor?


They have no return type, not even void.

15. What is this pointer?


The `this` pointer is accessible only inside the member functions of a class and points to the object that has called
this member function.

16. When is it necessary to use this pointer?


Suppose we have two local variables with the same name as the data members’ names. If you want to assign the
local variable value to the data members, you won’t be able to do that unless you use the `this` pointer because the
compiler won’t know that you are referring to the object’s data members unless you use this pointer.

17. What is similar between deep copy and shallow copy?


Both are used to copy data between objects.
18. What is the difference between deep copy and shallow copy?
Shallow Copy:
In shallow copy, the copying of data members is performed by copying the references or memory addresses,
meaning that changes made to the copied object will affect the original object if they share memory resources.
Deep Copy:
In deep copy, the entire data members and the resources (such as dynamically allocated memory) are copied.
Changes made to the copied object will not affect the original object since they have their own separate memory
allocation.

19. What is Encapsulation in C++? Why is it called Data hiding?


The process of binding data and corresponding methods (behavior) into a single unit is called encapsulation in
C++.
In other words, encapsulation is a programming technique that binds the class members (variables and methods)
together and prevents them from accessing other classes. Thereby we can keep variables and methods safe from
outside interference and misuse.
If a field is declared private in the class, it cannot be accessed by anyone outside the class and hides the fields.
Therefore, Encapsulation is also called data hiding.

20. What is the difference between Abstraction and Encapsulation?


Abstraction:
Focuses on hiding the implementation details and showing only essential features of an object. It defines what an
object does, rather than how it does it. For example, when you use a smartphone, you don't need to know the
internal workings of the hardware.
Encapsulation:
Refers to wrapping data and methods that operate on that data into a single unit (class). It helps in hiding the
internal state and behavior of the object, thus protecting the data from unauthorized access and misuse.

21. How much memory does a class occupy?


Classes do not consume any memory. They are just a blueprint based on which objects are created. When objects
are created, they initialize the class members and methods and therefore consume memory.

22. Are there any limitations of Inheritance?


Yes, with more powers comes more complications. Inheritance is a very powerful feature in OOPs, but it also has
limitations. Inheritance needs more time to process, as it needs to navigate through multiple classes for its
implementation. Also, the classes involved in Inheritance - the base class and the child class, are very tightly
coupled together. So if one needs to make some changes, they might need to do nested changes in both classes.
Inheritance might be complex for implementation, as well. If not correctly implemented, this might lead to
unexpected errors or incorrect outputs.

23. What is the difference between overloading and overriding?


Overloading is a compile-time polymorphism feature in which an entity has multiple implementations with the
same name—for example, Method overloading and Operator overloading.
Whereas, Overriding is a runtime polymorphism feature in which an entity has the same name, but its
implementation changes during execution. For example, Method overriding.
24. What are the various types of inheritance?
The various types of inheritance include:
- Single inheritance
- Multiple inheritances
- Multi-level inheritance
- Hierarchical inheritance
- Hybrid inheritance

25. What are the advantages of Polymorphism?


The following advantages of polymorphism in C++ are:
a. Using polymorphism, we can achieve flexibility in our code because we can perform various operations by using
methods with the same names according to requirements.
b. The main benefit of using polymorphism is when we can provide implementation to an abstract base class or an
interface.

26. What are the differences between Polymorphism and Inheritance in C++?
The differences between polymorphism and inheritance in C++ are as follows:
a. Inheritance represents the parent-child relationship between two classes. On the other hand, polymorphism
takes advantage of that relationship to make the program more dynamic.
b. Inheritance helps in code reusability in the child class by inheriting behavior from the parent class. On the other
hand, polymorphism enables the child class to redefine already defined behavior inside the parent class. Without
polymorphism, a child class can’t execute its own behavior.

27. Does every virtual function need to be always overridden?


No, it is not always mandatory to redefine a virtual function. It can be used as it is in the base class.

28. What is an abstract class?


An abstract class is a class that has at least one pure virtual function in its definition. An abstract class can never be
instantiated (creating an object). It can only be inherited, and the methods can be overwritten.

29. What is a pure virtual function?


A pure virtual function is a function declared by assigning 0 to it in the base class, meaning that there will be no
implementation for it in the base class. It must be overridden in any derived class.

30. What is a friend function in C++?


A friend function is a function that is not a member of a class but has the ability to access its private and protected
members. This function is declared inside the class using the keyword `friend`. Friend functions are useful when
you need to allow a function or another class to access the internals of your class without using getter and setter
functions. However, it breaks encapsulation to some extent as it allows outside functions to manipulate the internal
state of a class.
31. What is the difference between a friend function and a member function?
Friend Function:
- Not a member of the class.
- Can access private and protected members of the class it is friends with.
- Can be defined outside the class.
- Does not have a `this` pointer as it is not tied to a specific instance of the class.

Member Function:
- A part of the class.
- Can directly access all members (private, protected, and public) of the class.
- Defined within the class.
- Has a `this` pointer that points to the object for which it is called.

32. What are static members in C++?


Static members are variables or functions that belong to the class rather than any particular object instance. They
are shared among all instances of the class, meaning that they have a single copy shared by all objects. Static
members can be accessed using the class name and the scope resolution operator `::`. They are useful for keeping
track of information that is common to all objects of the class, such as counting the number of objects created from
that class.

Questions :
1. Question 1
2. Question 2
3. Question 3
4. Question 4
5. Question 5
32. What is the output of this program?

Answer: 20
We are using the friend function for print width and multiplied the width value by 2, so we got the output as 20.

You might also like