Question:- What are the differences between copy constructor and assignment operator?
Answer:- The copy constructor and the assignment operator (=) both are used to initialize one object using another object. The main difference between the two is that the copy constructor allocates separate memory to both objects i.e. existing object and newly created object while the assignment operator does not allocate new memory for the newly created object. It uses the reference variable that points to the previous memory block (where an old object is located).
Question:- What is the difference between Composition and Inheritance?
Answer:- Inheritance means an object inheriting reusable properties of the base class. Compositions mean that an object holds other objects. In Inheritance, there is only one object in memory (derived object) whereas, in Composition, the parent object holds references of all composed objects. From a design perspective, inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.
Question:- What is constructor chaining?
Answer:- In OOPs, constructor chaining is a sequence of invoking constructors (of the same class) upon initializing an object. It is used when we want to invoke a number of constructors, one after another by using only an instance. In other words, if a class has more than one constructor (overloaded) and one of them tries to invoke another constructor, this process is known as constructor chaining. In C++, it is known as constructor delegation and it is present from C++ 11.
Question:- What are the limitations of inheritance?
Answer:- • The main disadvantage of using inheritance is two classes get tightly coupled. That means one cannot be used independently of the other. If a method or aggregate is deleted in the Super Class, we have to refactor using that method in SubClass. • Inherited functions work slower compared to normal functions. • Need careful implementation otherwise leads to improper solutions.
Question:- What is Coupling in OOP and why it is helpful?
Answer:- In programming, separation of concerns is known as coupling. It means that an object cannot directly change or modify the state or behavior of other objects. It defines how closely two objects are connected together. There are two types of coupling, loose coupling, and tight coupling. Objects that are independent of one another and do not directly modify the state of other objects is called loosely coupled. Loose coupling makes the code more flexible, changeable, and easier to work with. Objects that depend on other objects and can modify the states of other objects are called tightly coupled. It creates conditions where modifying the code of one object also requires changing the code of other objects. The reuse of code is difficult in tight coupling because we cannot separate the code. Since using loose coupling is always a good habit.
Question:- Name the operators that cannot be overload.
Answer:- 1. Scope Resolution Operator (::) 2. Ternary Operator (? :) 3. Member Access or Dot Operator (.) 4. Pointer to Member Operator (.*) 5. sizeof operator
Question:- What is the difference between new and override?
Answer:- The new modifier instructs the compiler to use the new implementation instead of the base class function. Whereas, Override modifier helps to override the base class function. • virtual: indicates that a method may be overridden by an inheritor • override: Overrides the functionality of a virtual method in a base class, providing different functionality. • new: Hides the original method (which doesnt have to be virtual), providing different functionality. This should only be used where it is absolutely necessary. When you hide a method, you can still access the original method by upcasting to the base class. This is useful in some scenarios, but dangerous.
Question:- Explain overloading and overriding with example?
Answer:- • Overloading Overloading is a concept in OOP when two or more methods in a class with the same name but the method signature is different. It is also known as compile-time polymorphism. For example, in the following code snippet, the method add() is an overloaded method. • Overriding If a method with the same method signature is presented in both child and parent class is known as method overriding. The methods must have the same number of parameters and the same type of parameter. It overrides the value of the parent class method. It is also known as runtime polymorphism. For example, consider the following program.
Question:- What is Cohesion in OOP?
Answer:- In OOP, cohesion refers to the degree to which the elements inside a module belong together. It measures the strength of the relationship between the module and data. In short, cohesion represents the clarity of the responsibilities of a module. It is often contrasted with coupling. It focuses on a how single module or class is intended. Higher the cohesiveness of the module or class, better is the object-oriented design. There are two types of cohesion, i.e. High and Low. • High cohesion is associated with several required qualities of software including robustness, reliability, and understandability. • Low cohesion is associated with unwanted qualities such as being difficult to maintain, test, reuse, or even understand. High cohesion often associates with loose coupling and vice versa.
Question:- Give a real-world example of polymorphism?
Answer:- The general meaning of Polymorphism is one that has different forms. The best real-world example of polymorphism is a person that plays different roles at different palaces or situations. • At home a person can play the role of father, husband, and son. • At the office the same person plays the role of boss or employee. • In public transport, he plays the role of passenger. • In the hospital, he can play the role of doctor or patient. • At the shop, he plays the role of customer.
Question:- What is the difference between a base class and a superclass?
Answer:- The base class is the root class- the most generalized class. At the same time, the superclass is the immediate parent class from which the other class inherits.
Question:- What is data abstraction and how can we achieve data abstraction?
Answer:- It is one of the most important features of OOP. It allows us to show only essential data or information to the user and hides the implementation details from the user. A real-world example of abstraction is driving a car. When we drive a car, we do not need to know how the engine works (implementation) we only know how ECG works. There are two ways to achieve data abstraction • Abstract class • Abstract method
Question:- What are the levels of data abstraction?
Answer:- There are three levels of data abstraction: • Physical Level: It is the lowest level of data abstraction. It shows how the data is actually stored in memory. • Logical Level: It includes the information that is actually stored in the database in the form of tables. It also stores the relationship among the data entities in relatively simple structures. At this level, the information available to the user at the view level is unknown. • View Level: It is the highest level of data abstraction. The actual database is visible to the user. It exists to ease the availability of the database by an individual user.
