[go: up one dir, main page]

0% found this document useful (0 votes)
71 views31 pages

Oops Notes

Uploaded by

Anuj Kapadia
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)
71 views31 pages

Oops Notes

Uploaded by

Anuj Kapadia
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/ 31

‭OOPS NOTES‬

‭1.What are the types of programming paradigms?‬

‭ . Imperative Programming Paradigm -‬‭focuses on HOW‬‭to execute program logic and‬


A
‭defines control flow as statements that change a program state.‬

‭●‬ P ‭ rocedural Programming Paradigm‬‭- specifies the steps‬‭a program must take to reach‬
‭the desired state, usually read in order from top to bottom.‬
‭●‬ ‭Object-Oriented Programming or OOP‬‭- organizes programs‬‭as objects, that contain‬
‭some data and have some behavior.‬
‭●‬ ‭Parallel Programming‬‭- breaks a task into subtasks‬‭and focuses on executing them‬
‭simultaneously at the same time.‬

‭ . Declarative Programming Paradigm -‬ ‭focuses on‬‭WHAT to execute and defines program‬


B
‭logic, but not a detailed control flow. Declarative paradigm can be further classified into:‬

‭●‬ L ‭ ogical Programming Paradigm‬‭- based on formal logic,‬‭which refers to a set of‬
‭sentences expressing facts and rules about how to solve a problem‬
‭●‬ ‭Functional Programming Paradigm‬‭- programs are constructed‬‭by applying and‬
‭composing functions.‬
‭●‬ ‭Database Programming Paradigm‬‭- used to manage data‬‭and information structured as‬
‭fields, records, and files.‬

‭2. Difference between Procedural programming and structural programming?‬


‭3. What is meant by the term OOPs?‬

‭ OPs refers to Object-Oriented Programming. It is the programming paradigm that is‬


O
‭defined using objects. Objects can be considered as real-world instances of entities‬
‭like class, that have some characteristics and behaviors.‬

‭4. What are some major Object Oriented Programming languages?‬

‭Some of the major Object-Oriented Programming languages include:‬


‭●‬ ‭Java‬
‭●‬ ‭C++‬
‭●‬ ‭Javascript‬
‭●‬ ‭Python‬
‭●‬ ‭PHP‬

‭5. Can we run a Java application without implementing the OOPs concept?‬

‭ o. Java applications are based on Object-oriented programming models or OOPs‬


N
‭concept, and hence they cannot be implemented without it.‬

‭ owever, on the other hand, C++ can be implemented without OOPs, as it also‬
H
‭supports the C-like structural programming model.‬

‭6. What are some advantages of using OOPs?‬

‭ .‬
1 ‭ elpful in solving very complex level of problems.‬
H
‭2.‬ ‭Highly complex programs can be created, handled, and maintained easily using‬
‭object-oriented programming.‬
‭ .‬
3 ‭It promote code reuse, thereby reducing redundancy.‬
‭4.‬ ‭It also helps to hide the unnecessary details with the help of Data‬
‭Abstraction.‬
‭5.‬ ‭OOPs, are based on a bottom-up approach, unlike the Structural programming‬
‭paradigm, which uses a top-down approach.‬
‭ .‬
6 ‭Polymorphism offers a lot of flexibility in OOPs.‬
‭7.‬ ‭With OOPs, the readability and understandability of the code increase multifold.‬
‭8.‬ ‭It provides the feature of data hiding that is good for security concerns.‬
‭9.‬ ‭We can provide the solution to real-world problems if we are using object-oriented‬
‭programming.‬

‭7‬‭)‬‭Explain Is Java a pure Object Oriented language?‬

‭ he programming language is called pure object-oriented language that treats everything inside‬
T
‭the program as an object. The primitive types are not supported by the pure OOPs language.‬
‭ ava is not an entirely pure object-oriented programming language. The following are the‬
J
‭reasons:‬

‭●‬ J ‭ ava supports and uses primitive data types such as int, float, double, char, etc. These‬
‭data types in Java are not treated as objects.‬
‭●‬ ‭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.‬

‭8. What is a class?‬

‭ class is an entity that determines how an object will behave and what the object will contain.‬
A
‭In other words, it is a blueprint or a set of instruction to build a specific type of object. It provides‬
‭initial values for member variables and member functions or methods.‬
‭So when an object is created, it automatically takes the data and functions that are defined in‬
‭the class.‬
‭Therefore the class is basically a template or blueprint for objects.‬

‭9. What is an object?‬

‭ bjects are instances of a class created with specifically defined data. Objects can correspond‬
O
‭to real-world objects or an abstract entity.‬‭So the‬‭objects consume space and have some‬
‭characteristic behavior.‬
‭For example, a specific car.‬

‭10. Difference between class and object?‬


‭11. Difference between class and a structure?‬
‭●‬ ‭The structure is saved in the stack memory, whereas the class is saved in the heap‬
‭memory.‬
‭●‬ ‭Also, Data Abstraction cannot be achieved with the help of structure, but with class,‬
‭Abstraction is majorly used.‬
‭●‬ ‭Structures are exclusively used for data, and it doesn't require strict validation, but‬
‭classes are used to encapsulate and inherent data, which requires strict validation.‬

‭Correction :‬‭Structure supports inheritance in C++.‬

‭12. How much memory does a class occupy?‬

‭ lasses do not consume any memory. They are just a blueprint based on which‬
C
‭objects are created. Now when objects are created, they actually initialize the class‬
‭members and methods and therefore consume memory. And the size of instance is equal to the‬
‭sum of the size of members define in class.‬

‭ 3. What is the size of an empty class?‬


1
‭Empty class have 1 byte of memory. Actually, the standard does not permit objects (or classes)‬
‭of size 0. It is nonzero to ensure that the two different objects will have different addresses‬
‭14) How can we call the base method without creating an instance?‬

‭ es, you are allowed to call the base class without instantiating it but there are some conditions‬
Y
‭that are applicable.‬
‭•If it is a static method‬
‭•The base class is inherited by some other subclass‬

‭ 5. How can you define member functions outside the class?‬


1
‭We can also define member functions outside the class using the scope resolution operator ::‬

int Student::getMarks()‬‭
‭ {‬
return‬‭
‭ marks;‬
}‬

‭16) What are the different types of arguments?‬

‭ parameter is a variable used during the declaration of the function or subroutine, and‬
A
‭arguments are passed to the function body, and it should match with the parameter defined.‬

‭ here are two types of Arguments.‬


T
‭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‬‭in both inside and outside the‬
‭functions and it returns the same or different value‬

‭17. What are Getter and Setter?‬

‭ etters are those functions that allow us to access data members of the object. However, these‬
G
‭functions do not change the value of data members. These are also called accessor functions.‬

‭ etters are the member functions that allow us to change the data members of an object. These‬
S
‭are also called mutator functions.‬

‭18. What are the types of variables in OOP?‬

I‭nstance Variable:‬‭It is an object-level variable.‬‭It should be declared inside a class but must be‬
‭outside a method, block, and constructor. It is created when an object is created by using the‬
‭new keyword. It can be accessed directly by calling the variable name inside the class.‬
‭Static Variable‬‭: It is a class-level variable. It‬‭is declared with keyword static inside a class but‬
‭must be outside of the method, block, and constructor. It stores in static memory. Its visibility is‬
‭the same as the instance variable. The default value of a static variable is the same as the‬
‭instance variable. It can be accessed by calling the class_name.variable_name.‬
‭Local Variable:‬‭It is a method-level variable. It‬‭can be declared in method, constructor, or block.‬
‭Note that the use of an access modifier is not allowed with local variables. It is visible only to the‬
‭ ethod, block, and constructor in which it is declared. Internally, it is implemented at the stack‬
m
‭level. It must be declared and initialized before use.‬
‭Reference Variable:‬‭It is a variable that points to‬‭an object of the class. It points to the location‬
‭of the object that is stored in the memory.‬

‭19. What are the main features of OOPs?‬

‭‬
● I‭nheritance‬
‭●‬ ‭Encapsulation‬
‭●‬ ‭Polymorphism‬
‭●‬ ‭Data Abstraction‬

‭20. What is encapsulation?‬

‭ ncapsulation is the process of binding data members and methods of a program together to do‬
E
‭a specific job, without revealing unnecessary details.‬

‭ ncapsulation can also be defined in two different ways:‬


E
‭1) Data hiding‬‭: Encapsulation is the process of hiding‬‭unwanted information, such as‬
‭restricting access to any member of an object.‬
‭2) Data binding:‬‭Encapsulation is the process of binding‬‭the data members and the‬
‭methods together as a whole, as a class.‬

‭21. Advantages of encapsulation?‬


‭1.‬ ‭Encapsulation is a way to achieve data hiding because other classes will not be able to‬
‭access the data through the private data members.‬
‭2.‬ ‭In Encapsulation, we can hide the data’s internal information, which is better for security‬
‭concerns.‬
‭3.‬ ‭By encapsulation, we can make the class read-only. The code reusability is also an‬
‭advantage of encapsulation.‬
‭4.‬ ‭Encapsulated code is better for unit testing.‬

‭22. What are access specifiers and what is their significance?‬

‭ ccess modifiers determine the scope of the method or variables that can be accessed from‬
A
‭other various objects or classes.. These access specifiers also play a very vital role in achieving‬
‭Encapsulation. There are four types of access modifiers, and they are as follows:‬
‭●‬ ‭Private‬
‭●‬ ‭Protected‬
‭●‬ ‭Public‬
‭●‬ ‭Default (only in java)‬
(‭ For C++)‬
‭i) Public: All the class members with public modifier can be accessed from anywhere(inside and‬
‭outside the class).‬

i‭i) Private: All the class members with private modifier can only be accessed by the member‬
‭function inside the class.‬

i‭ii) Protected: This access modifier is similar to private access modifier, i.e., it can’t be accessed‬
‭outside of its class unless, with the help of friend class, the difference is that the class members‬
‭declared as Protected can be accessed by any subclass(derived class) of that class as well.‬

‭ ote:‬‭If we do not specify any access modifiers for‬‭the members inside the class, then by‬
N
‭default, the access modifier for the members will be Private.‬

‭(For Java)‬

‭ ote:‬‭If we do not specify any access modifiers for‬‭the members inside the class, then by‬
N
‭default, the access modifier for the members will be Default (or package-private).‬

‭23) What are sealed modifiers?‬

‭ ealed modifiers are the access modifiers where the methods can not inherit it. Sealed‬
S
‭modifiers can also be applied to properties, events, and methods. This modifier cannot be used‬
‭to static members.‬

‭24. What is meant by Inheritance?‬

‭ he term “inheritance” means “receiving some quality or behavior from a parent to‬
T
‭an offspring.” In object-oriented programming, inheritance is the capability of a class to derive‬
‭properties and characteristics from another class. Inheritance can also be defined as the Is-A‬
‭relationship, which is also known as the parent-child relationship.‬
‭Advantages:‬
‭●‬ C ‭ ode reusability: We can reuse the code when we inherit the existing class’s methods‬
‭and fields into a new class.‬
‭●‬ ‭The runtime polymorphism (method overriding) can be achieved by inheritance only.‬

‭Modes of Inheritance:‬
‭ .‬ P
1 ‭ ublic mode‬
‭2.‬ ‭Protected mode‬
‭3.‬ ‭Private mode‬

‭25. What are the various types of inheritance?‬

‭The various types of inheritance include:‬


‭●‬ ‭Single inheritance‬
‭●‬ ‭Multiple inheritances (not supported by java)‬
‭●‬ ‭Multi-level inheritance‬
‭●‬ ‭Hierarchical inheritance‬
‭●‬ ‭Hybrid inheritance‬
‭Note‬‭- Interfaces provide an alternative to multiple‬‭inheritance in java‬
‭26. What is base class?‬

‭The base class is the root class- the most generalized class.‬

‭27. What is a subclass and superclass?‬

‭ ubclass‬‭is a part of Inheritance. The subclass is‬‭an entity, which inherits‬


S
‭from another class. It is also known as the child class.‬
‭Superclass‬‭is also a part of Inheritance. The superclass‬‭is an entity,‬
‭which allows subclasses or child classes to inherit from itself.‬

‭28. Are there any limitations of Inheritance?‬

I‭nheritance 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. So if not correctly implemented, this might lead to unexpected errors or incorrect outputs.‬

‭ 9. What is inheritance ambiguity and how to avoid it?‬


2
‭There may be a possibility that a class may inherit member functions with the same name from‬
‭two or more base classes, and the derived class may not have functions with the same name as‬
‭those of its base classes. If the derived class object needs to access one of the same-named‬
‭member functions of the base classes, it results in ambiguity as it is not clear to the compiler‬
‭which base’s class member function should be invoked‬
‭ he ambiguity can be resolved by using the scope resolution operator by specifying the class in‬
T
‭which the member function lies as given below:‬

‭ yntax:‬
S
‭object.class_name::method_name();‬

‭ 0. What is diamond problem and how to avoid it?‬


3
‭The Diamond Problem is an ambiguity that arises in multiple inheritance when two parent‬
‭classes inherit from the same grandparent class, and both parent classes are inherited by a‬
‭single child class. Without using virtual inheritance, the child class would inherit the properties of‬
‭the grandparent class twice, leading to ambiguity.‬

‭ he Person class constructor is called twice: once when the Father class object is created and‬
T
‭next when the Mother class object is created. The properties of the Person class are inherited‬
‭twice, giving rise to ambiguity.‬
‭Since the Person class constructor is called twice, the destructor will also be called twice when‬
‭the Child class object is destructed.‬

‭ he solution to the diamond problem is to use the‬‭virtual‬‭keyword. We make the two parent‬
T
‭classes (who inherit from the same grandparent class) into virtual classes in order to avoid two‬
‭copies of the grandparent class in the child class.‬
‭ e will use the virtual keyword when classes Father and Mother inherit the Person class. This is‬
W
‭usually called “virtual inheritance," which guarantees that only a single instance of the inherited‬
‭class (in this case, the Person class) is passed on.‬
‭In other words, the Child class will have a single instance of the Person class, shared by both‬
‭the Father and Mother classes. By having a single instance of the Person class, the ambiguity is‬
‭resolved.‬

‭ ne thing to note about virtual inheritance is that even if the parameterized constructor of the‬
O
‭Person class is explicitly called by Father and Mother class constructors through initialization‬
‭lists, only the base constructor of the Person class will be called.‬
‭This is because there's only a single instance of a virtual base class that's shared by multiple‬
‭classes that inherit from it.‬
‭To prevent the base constructor from running multiple times, the constructor for a virtual base‬
‭class is not called by the class inheriting from it. Instead, the constructor is called by the‬
‭constructor of the concrete class.‬

‭ hat if you need to execute the parameterized constructor of the base class? You can do so by‬
W
‭explicitly calling it in the Child class rather than the Father or Mother classes.‬

‭31. What is Abstraction?‬

‭ ata abstraction refers to providing only necessary information about the outside world’s data,‬
D
‭hiding the background details or implementation.‬
‭For example, consider a car. You only need to know how to run a car, and not how the‬
‭wires are connected inside it. This is obtained using Abstraction.‬

‭Advantages Of Abstraction‬
‭‬ O
● ‭ nly you can make changes to your data or function, and no one else can.‬
‭●‬ ‭It makes the application secure by not allowing anyone else to see the background‬
‭details.‬
‭●‬ ‭Increases the reusability of the code.‬
‭●‬ ‭Avoids duplication of your code.‬

‭32. How is data abstraction accomplished?‬

‭ ata abstraction is accomplished with the help of abstract methods or abstract classes or‬
D
‭interfaces.‬

‭33. What is abstract method?‬

‭ hese methods are basically declared but not defined and If these methods need to be used‬
T
‭later in some subclass that time those methods have to be exclusively defined in the subclass.‬
‭34. What is an abstract class?‬

‭ n abstract class is a special class containing abstract methods. The significance of‬
A
‭abstract class is that the abstract methods inside it are and only declared and not implemented.‬
‭So as a result, when a subclass inherits the abstract class and needs to use‬
‭its abstract methods, they need to define and implement them.‬

‭‬
● I‭nstantiation of an abstract class is not allowed. It must be inherited.‬
‭●‬ ‭An abstract class can have both abstract and non-abstract methods.‬
‭●‬ ‭An abstract class (in java) must have at least one abstract method.‬
‭●‬ ‭An abstract class (in C++) has at least one pure virtual function (i.e., a function that has‬
‭no definition).‬
‭‬
● ‭It is always public.‬
‭●‬ ‭It is declared using the keyword abstract‬
‭●‬ ‭It can have constructors and static methods also.‬
‭●‬ ‭It can have final methods which will force the subclass not to change the body of the‬
‭method.‬
‭●‬ ‭The purpose of an abstract class is to provide a common definition of the base class that‬
‭multiple derived classes can share.‬

‭35. What is an interface?‬

‭ n interface refers to a special type of class, which contains methods, but not their‬
A
‭definition. Only the declaration of methods is allowed inside an interface. To use an‬
‭interface, you cannot create objects. Instead, you need to implement that interface‬
‭and define the methods for their implementation.‬
‭An interface is a collection of an abstract method. If the class implements an interface, it thereby‬
‭inherits all the abstract methods of an interface.There are mainly three reasons to use interfaces‬
‭in Java. They are given below.‬
‭●‬ ‭It is used to achieve abstraction.‬
‭●‬ ‭By interface, we can support the functionality of multiple inheritance.‬
‭●‬ ‭It can be used to achieve loose coupling.‬

‭ he C++ interfaces are implemented using abstract classes, and these abstract classes should‬
T
‭not be confused with data abstraction, which is a concept of keeping implementation details‬
‭separate from associated data.‬
‭A class is made abstract by declaring at least one of its functions as a pure virtual function. A‬
‭pure virtual function is specified by placing "= 0" in its declaration.‬
‭Example: The following function is a pure virtual function.‬
‭virtual void fun() = 0;‬
‭36. How is an abstract class different from an interface?‬

‭ he main difference between the two is that, when an interface is implemented, the subclass‬
T
‭must define all its methods and provide its implementation. Whereas when an abstract class is‬
‭inherited, the subclass does not need to provide the definition of its abstract method, until and‬
‭unless the subclass is using it.‬

‭37. How many instances can be created for an abstract class?‬

‭ ero instances will be created for an abstract class. In other words, you cannot create an‬
Z
‭instance of an Abstract Class.You first need to create a subclass that implements all the‬
‭methods before an object can be initialized.‬
‭38. Difference between extends and implements.‬

‭39. What is composition in oops?‬

‭ omposition describes a class that references one or more objects of other classes in instance‬
C
‭variables. This allows you to model a has-a association between objects.‬
‭The main benefits of composition are:‬
‭●‬ ‭Reuse existing code‬
‭●‬ ‭Design clean APIs‬
‭●‬ ‭Change the implementation of a class used in a composition without adapting any‬
‭external clients.‬

‭40. What is polymorphism? Explain its types.‬

‭ olymorphism is composed of two words - “poly” which means “many”, and “morph”‬
P
‭which means “shapes”. Therefore Polymorphism refers to something that has many‬
‭Shapes.‬

I‭n OOPs, Polymorphism refers to the process by which some code, data, method, or‬
‭object behaves differently under different circumstances or contexts.‬
‭Compile-time polymorphism‬‭and‬‭Run time polymorphism‬‭are the two types of‬
‭polymorphisms in OOPs languages.‬
‭41. What is compile time polymorphism?‬

‭ ompile time polymorphism, also known as Static Polymorphism, refers to the type of‬
C
‭Polymorphism that happens at compile time.‬
‭What it means is that the compiler decides what shape or value has to be taken by‬
‭the entity in the picture.It can be achieved through Method overloading or operator overloading.‬

‭●‬ M ‭ ethod Overloading :‬ ‭Overloading is a compile-time‬‭polymorphism feature in which an‬


‭entity has multiple implementations with the same name.‬‭When there are multiple‬
‭functions in a class with the same name but different parameters, these functions are‬
‭overloaded. The main advantage of function overloading is it increases the readability of‬
‭the program. Functions can be overloaded by using different numbers of arguments and‬
‭by using different types of arguments.‬
‭●‬ ‭Operator Overloading :‬‭C++ also provides options to‬‭overload operators. For example,‬
‭we can make the operator (‘+’) for the string class to concatenate two strings. We know‬
‭that this is the addition operator whose task is to add two operands. A single operator,‬
‭‘+,’ when placed between integer operands, adds them and concatenates them when‬
‭placed between string operands.‬

‭Points to remember while overloading an operator:‬


‭1.‬ I‭t can be used only for user-defined operators(objects, structures) but cannot be‬
‭used for in-built operators(int, char, float, etc.).‬
‭2.‬ ‭Operators = and & are already overloaded in C++, so we can avoid overloading‬
‭them.‬
‭3.‬ ‭Precedence and associativity of operators remain intact.‬

‭ C++ supports compile-time polymorphism with the help of features like templates, function‬
#
‭overloading, and default arguments.‬

‭42. What is Runtime Polymorphism?‬

‭ untime polymorphism, also known as Dynamic Polymorphism, refers to the type of‬
R
‭Polymorphism that happens at the run time. What it means is it can't be decided by the‬
‭compiler. Therefore what shape or value has to be taken depends upon the execution. Hence‬
‭the name Runtime Polymorphism.It can be achieved with the help of method overriding.‬
‭●‬ ‭Method overriding‬‭:Overriding is a runtime polymorphism‬‭feature in which an entity has‬
‭the same name, but its implementation changes during execution. It is a feature that‬
‭allows you to redefine the parent class method in the child class based on its‬
‭requirement. In other words, whatever methods the parent class has by default are‬
‭available in the child class. But, sometimes, a child class may not be satisfied with‬
‭parent class method implementation. The child class is allowed to redefine that method‬
‭based on its requirement. This process is called method overriding.‬

‭Rules for method overriding:‬


‭1.‬ T ‭ he method of the parent class and the method of the child class must have the‬
‭same name.‬
‭2.‬ ‭The method of the parent class and the method of the child class must have the‬
‭same parameters.‬
‭3.‬ ‭It is possible through inheritance only.‬

‭ C++ supports Runtime polymorphism with the help of features like virtual functions. Virtual‬
#
‭functions take the shape of the functions based on the type of object in reference and are‬
‭resolved at runtime.‬

‭43. What are all the operators that cannot be overloaded?‬

‭Following are the operators that cannot be overloaded -.‬

‭1. Scope Resolution (::)‬

‭2. Member Selection (.)‬

‭3. Member selection through a pointer to function (.*)‬

‭4. Ternary Operator (? :)‬

‭5. Size of operator‬

‭44. Is Operator overloading supported in Java?‬

‭ perator overloading is not supported by Java as, It makes the interpreter put more effort to‬
O
‭understand the actual functionality of the operator making code complex and difficult to‬
‭compile.Operator overloading makes programs more error-prone.However, the feature of‬
‭operator overloading can be achieved in method overloading in a simple, clear, and error-free‬
‭way.‬

‭45. Can we overload the main() method in Java ?‬

‭ es, we can also overload the main() method in Java. Any number of main() methods can be‬
Y
‭defined in the class, but the method signature must be different.‬

‭ 6. What is a constructor?‬
4
‭A constructor is a special type of member function that is called automatically when an object is‬
‭created. The constructors serve the special purpose of initializing the objects.‬
‭●‬ ‭A constructor has the same name as the class itself.‬
‭●‬ ‭Constructors don’t have a return type, not even void.‬
‭●‬ ‭If we do not specify a constructor, the C++ compiler generates a default constructor for‬
‭us (expects no parameters and has an empty body).‬
‭●‬ ‭It cannot be marked as static.‬
‭●‬ ‭It cannot be marked as abstract or virtual.‬
‭‬
● I‭t cannot be overridden.‬
‭●‬ ‭It cannot be final.‬
‭●‬ ‭They are public.‬
‭●‬ ‭You cannot refer to their address‬
‭●‬ ‭They cannot be inherited though derived class can call the base class constructor‬

‭ ypes of Constructors‬
T
‭There are three types of constructors in C++ they are:‬

‭ )‬ D
1 ‭ efault constructor‬
‭2)‬ ‭Parameterized Constructor‬
‭3)‬ ‭Copy Constructor‬

‭1.‬ D
‭ efault Constructor (No argument constructor)‬
‭The default constructor is the constructor that doesn’t take any argument. It has no‬
‭parameters.‬

‭2.‬ ‭Parameterized Constructor‬


‭ constructor with parameters is known as a parameterized constructor. The‬
A
‭parameterized constructor takes its arguments provided by the programmer.‬

‭3.‬ ‭Copy Constructor‬


‭ copy constructor is a member function that initializes an object using another object of‬
A
‭the same class.It clones an object and its values,into another object, is provided that‬
‭both the objects are of the same class.‬

‭47. Explain constructor overloading.‬

‭ he constructor overloading can be defined as the concept of having more than one constructor‬
T
‭with different parameters so that every constructor can perform a different task.With constructor‬
‭overloading, objects can be created in different ways. Various Collection classes in Java API are‬
‭examples of constructor overloading.‬

‭48. What is constructor chaining?‬

I‭n 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.‬

‭49. Is it possible for a class to inherit the constructor of its base class?‬

‭No, a class cannot inherit the constructor of its base class.‬


‭50. Difference between copy constructor and assignment operator?‬

‭ he copy constructor and the assignment operator (=) both are used to initialize one‬
T
‭object using another object.‬

‭51. Why should we pass by reference in copy constructor?‬

I‭f we pass by value in copy constructor, we will stuck in infinite loop. A call to the copy‬
‭constructor would be made to call the copy constructor which becomes a non-terminating chain‬
‭of calls. Thats why we should pass by reference.‬

‭52. Does C++ compiler create default constructor when we write our own?‬
‭ o, the C++ compiler doesn’t create a default constructor when we initialize our own, the‬
N
‭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. This is so because the default‬
‭constructor does not take any argument and if two default constructors are created, it is difficult‬
‭for the compiler which default constructor should be called.‬

‭53. What is a destructor?‬

‭ estructors are also special methods. But destructors free up the resources and‬
D
‭memory occupied by an object. Destructors are automatically called when an object‬
‭is being destroyed.‬
‭ he destructor also recovers the heap space which was allocated to the destroyed object. It‬
T
‭also start closing the files and database connections of the object, etc.‬

‭When is a destructor called?‬


‭A destructor function is called automatically when the object goes out of scope:‬
‭1.‬ ‭the function ends‬
‭2.‬ ‭the program ends‬
‭3.‬ ‭a block containing local variables ends‬
‭4.‬ ‭a delete operator is called‬

‭Destructor rules‬

‭ .‬
1 ‭ he name should begin with a tilde sign(~) and must match the class name.‬
T
‭2.‬ ‭There cannot be more than one destructor in a class.‬
‭3.‬ ‭Unlike constructors that can have parameters, destructors do not allow any parameter.‬
‭4.‬ ‭They do not have any return type, just like constructors.‬
‭5.‬ ‭When you do not specify any destructor in a class, the compiler generates a default‬
‭destructor and inserts it into your code.‬

‭ ote:‬‭Destructor is called automatically for the object‬‭formed by static allocation whereas you‬
N
‭have to call destructor manually for dynamic allocation.‬

‭54. What is Shallow copy and deep copy?‬

‭Shallow Copy‬
‭ n object is created by copying all of the member field values. Here, the pointer will be copied‬
A
‭but not the memory it points to. It means that the original object and the created copy will now‬
‭point to the same memory address, which is generally not preferred. Since both objects will‬
‭reference the same memory location, then changes made by one will reflect those changes in‬
‭another object. Since we wanted to create a replica of the object, this purpose will not be filled‬
‭by Shallow copy.‬

‭Note: The assignment operator and the default copy constructor make a shallow copy.‬

‭Deep Copy‬
‭ n object is created by copying all the fields, and it also allocates similar memory resources with‬
A
‭the same value to the object. To perform Deep copy, we need to explicitly define the copy‬
‭constructor and assign dynamic memory as well if required. Also, it is necessary to allocate‬
‭memory to the other constructors’ variables dynamically.‬
‭55‬‭. What is an exception?‬

‭ n exception can be considered as a special event, which is raised during the‬


A
‭execution of a program at runtime, that brings the execution to a halt. The reason for‬
‭the exception is mainly due to a position in the program, where the user wants to do‬
‭something for which the program is not specified, like undesirable input.‬

‭56. What is meant by exception handling?‬

‭ o one wants its software to fail or crash. Exceptions are the major reason for software failure.‬
N
‭Exceptions can be of any type – Runtime exception, Error exceptions. The exceptions can be‬
‭handled in the program beforehand and prevent the execution from stopping. This is known as‬
‭exception handling.‬
‭So exception handling is the mechanism for identifying the undesirable states that‬
‭the program can reach and specifying the desirable outcomes of such states.‬
‭Try-catch is the most common method used for handling exceptions in the program.‬

‭57. Is an error basically the same as an exception?‬

‭ n error means a problem that the program should not catch while the exception implies a‬
A
‭condition that should be caught by the program.‬

‭58. What is a try/ catch block?‬

‭ try/ catch block helps to handle exceptions. The try block explains a set of statements that‬
A
‭may have an error. The catch block basically catches the exception.‬

‭59. What is meant by Garbage Collection in OOPs world?‬

‭ arbage collection refers to this mechanism of handling the memory in the program.‬
G
‭Through garbage collection, the unwanted memory is freed up by removing the‬
‭objects that are no longer needed.‬

‭60. What are Manipulators?‬

‭ anipulators are the functions which can be used in conjunction with the insertion (<<) and‬
M
‭extraction (>>) operators on an object. Examples are endl and setw.‬

‭ 1. What is an Inline function?‬


6
‭An inline function is a technique used by the compilers and instructs to insert complete body of‬
‭the function wherever that function is used in the program source code.‬
‭62. What is 'this' pointer?‬

“‭ This” pointer holds the current object’s address; in simple words, this pointer points to the‬
‭class’s current object.‬

‭There can be three main usages of this keyword in C++.‬


‭●‬ ‭It can be used to pass the current object as a parameter to another method.‬
‭●‬ ‭It can be used to refer to a current class instance variable.‬
‭●‬ ‭It can be used to declare indexers.‬

‭ HIS keyword is used as a pointer which differentiates between the current object with the‬
T
‭global object. It refers to the current object When class attributes and parameterized‬
‭constructors both have the same name, this keyword is used. Keywords this invokes the current‬
‭class constructor, method of the current class, return the object of the current class, pass an‬
‭argument in the constructor, and method call.‬

‭63. What are tokens?‬

‭ token is the smallest element of a program that is meaningful to the compiler. Tokens can be‬
A
‭classified as follows:‬
‭1.‬ ‭Keywords‬
‭2.‬ ‭Identifiers‬
‭3.‬ ‭Constants‬
‭4.‬ ‭Strings‬
‭5.‬ ‭Special Symbols‬
‭6.‬ ‭Operators‬

‭64. What does static keyword do in java?‬

‭The static keyword is a non-access modifier in Java that is applicable for the following:‬
‭1.‬ ‭Blocks‬
‭2.‬ ‭Variables‬
‭3.‬ ‭Methods‬
‭4.‬ ‭Classes‬
‭When a member is declared static, it can be accessed before any objects of its class are‬
‭created, and without reference to any object.‬
‭Static data members belong to class. There is no need to create objects.‬

‭Static blocks‬
I‭f you need to do the computation in order to initialize your static variables, you can declare a‬
‭static block that gets executed exactly once, when the class is first loaded.‬
‭Static variables‬
‭ hen a variable is declared as static, then a single copy of the variable is created and shared‬
W
‭among all objects at the class level. Static variables are, essentially, global variables. All‬
‭instances of the class share the same static variable.‬
‭Important points for static variables:‬
‭●‬ ‭We can create static variables at the class level only.‬
‭●‬ ‭static block and static variables are executed in the order they are present in a program.‬

‭Static methods‬
‭ hen a method is declared with the static keyword, it is known as the static method. The most‬
W
‭common example of a static method is the main( ) method. Any static member can be accessed‬
‭before any objects of its class are created, and without reference to any object. Methods‬
‭declared as static have several restrictions:‬
‭●‬ ‭They can only directly call other static methods.‬
‭●‬ ‭They can only directly access static data.‬
‭●‬ ‭They cannot refer to this or super in any way.‬

‭ hen to use static variables and methods?‬


W
‭Use the static variable for the property that is common to all objects. For example, in class‬
‭Student, all students share the same college name. Use static methods for changing static‬
‭variables.‬

‭Static Classes‬

‭ class can be made static only if it is a nested class. We cannot declare a top-level class with a‬
A
‭static modifier but can declare‬‭nested classes‬‭as‬‭static. Such types of classes are called Nested‬
‭static classes. Nested static class doesn’t need a reference of Outer class. In this case, a static‬
‭class cannot access non-static members of the Outer class.‬

‭65. Whether static method can use nonstatic members?‬

‭False‬

‭66. What is the use of finalize method?‬

‭ inalize method helps to perform cleanup operations on the resources which are not currently‬
F
‭Used and also help to clean before Garbage Collection(GC). It performs memory management‬
‭tasks. Finalize method is protected, and it is accessible only through this class or by a derived‬
‭class.‬
‭67. What is a finally block?‬

‭ finally block executes when the try block exits and It also executes even in case some‬
A
‭unexpected exception is encountered. Finally block normally consists of some important part of‬
‭the program.‬

‭68. What does final keyword do in java?‬

‭Final Variables‬
‭ hen a variable is declared with the final keyword, its value can’t be modified, essentially, a‬
W
‭constant. This also means that you must initialize a final variable. It always refers to the same‬
‭object by the property of non-transversity.‬
‭If the final variable is a reference, this means that the variable cannot be re-bound to reference‬
‭another object, but the internal state of the object pointed by that reference variable can be‬
‭changed i.e. you can add or remove elements from the final array or final collection.‬

‭ ote‬‭- the difference between C++ const variables‬‭and Java final variables. const variables in‬
N
‭C++ must be assigned a value when declared. For final variables in Java, it is not necessary as‬
‭we see in the above examples. A final variable can be assigned value later, but only once.‬
‭As we all know that a final variable cannot be re-assign. But in the case of a reference final‬
‭variable, the internal state of the object pointed by that reference variable can be changed. Note‬
‭that this is not re-assigning. This property of final is called non-transitivity.‬
‭The non-transitivity property also applies to arrays, because arrays are objects in Java. Arrays‬
‭with the final keyword are also called final arrays.‬

‭Final classes‬
‭ hen a class is declared with final keyword, it is called a final class. A final class cannot be‬
W
‭extended(inherited).‬
‭There are two uses of a final class:‬
‭Usage 1: One is definitely to prevent inheritance, as final classes cannot be extended. For‬
‭example, all Wrapper Classes like Integer, float, etc. are final classes. We can not extend them.‬
‭Usage 2: The other use of final with classes is to create an immutable class like the predefined‬
‭String class. One can not make a class immutable without making it final.‬

‭Final Methods‬
‭ hen a method is declared with final keyword, it is called a final method. A final method cannot‬
W
‭be overriden. The‬‭Object‬‭class does this—a number‬‭of its methods are final. We must declare‬
‭methods with the final keyword for which we are required to follow the same implementation‬
‭throughout all the derived classes.‬
‭69. What does const keyword do in c++?‬

‭ henever const keyword is attached with any method(), variable, pointer variable, and with the‬
W
‭object of a class it prevents that specific object/method()/variable to modify its data items value.‬

‭Constant Variables:‬
‭There are a certain set of rules for the declaration and initialization of the constant variables:‬
‭●‬ ‭The const variable cannot be left un-initialized at the time of the assignment.‬
‭●‬ ‭It cannot be assigned value anywhere in the program.‬
‭●‬ ‭Explicit value needed to be provided to the constant variable at the time of declaration of‬
‭the constant variable.‬

‭Const Keyword With Pointer Variables:‬


‭ ointers can be declared with a const keyword. So, there are three possible ways to use a‬
P
‭const keyword with a pointer, which are as follows:‬
‭1.‬ ‭When the pointer variable point to a const value:‬
‭Syntax:‬
‭const data_type* var_name;‬
‭2.‬ ‭When the const pointer variable point to the value:‬
‭Syntax:‬
‭data_type* const var_name;‬
‭3.‬ ‭When const pointer pointing to a const variable:‬
‭Syntax:‬
‭const data_type* const var_name;‬

‭ ass const-argument value to a non-const parameter of a function cause error‬‭: Passing const‬
P
‭argument value to a non-const parameter of a function isn’t valid it gives you a compile-time‬
‭error.‬
‭Constant Objects:‬
‭ ike member functions and member function arguments, the objects of a class can also be‬
L
‭declared as const. An object declared as const cannot be modified and hence, can invoke only‬
‭const member functions as these functions ensure not to modify the object.‬
‭Syntax:‬
‭const Class_Name Object_name;‬
‭●‬ ‭When a function is declared as const, it can be called on any type of object, const object‬
‭as well as non-const objects.‬
‭●‬ ‭Whenever an object is declared as const, it needs to be initialized at the time of‬
‭declaration. However, the object initialization while declaring is possible only with the‬
‭help of constructors.‬

‭Constant Function Parameters And Return Type:‬


‭●‬ A ‭ function() parameters and return type of function() can be declared as constant.‬
‭Constant values cannot be changed as any such attempt will generate a compile-time‬
‭error.‬
‭●‬ ‭For const return type: The return type of the function() is const and so it returns a const‬
‭integer value to us.‬
‭●‬ ‭There is no substantial issue to pass const or non-const variable to the function because‬
‭the value that will be returned by the function will be constant automatically. As the‬
‭argument of the function is non-const.‬
‭●‬ ‭For const return type and const parameter: Here, both return type and parameter of the‬
‭function are of const types.‬

‭70. What is a friend function?‬

‭ friend function is a friend of a class that is allowed to access to Public, private, or protected‬
A
‭data in that same class. If the function is defined outside the class cannot access such‬
‭information.‬
‭A class’s friend function is defined outside that class’s scope, but it has the right to access all‬
‭private and protected members of the class. Even though the prototypes for friend functions‬
‭appear in the class definition, friends are not member functions.‬

‭A friend function in C++ is a function that is preceded by the keyword “friend.”‬

‭ yntax:‬
S
‭class class_name {‬
‭friend data_type function_name(argument); // syntax of friend function.‬
‭};‬

‭ he function can be defined anywhere in the program like a normal C++ function. The function‬
T
‭definition does not use either the keyword friend or scope resolution operator.‬
‭Characteristics of friend function:‬
‭‬
● ‭ friend function can be declared in the private or public section of the class.‬
A
‭●‬ ‭It can be called a normal function without using the object.‬
‭●‬ ‭A friend function is not in the scope of the class, of which it is a friend.‬
‭●‬ ‭A friend function is not invoked using the class object as it is not in the class’s scope.‬
‭●‬ ‭A friend function cannot access the private and protected data members of the class‬
‭directly. It needs to make use of a class object and then access the members using the‬
‭dot operator.‬
‭ ‬ ‭A friend function can be a global function or a member of another class.‬

‭71. What is a virtual function?‬

‭ virtual function is a member function of a class, and its functionality can be overridden in its‬
A
‭derived class. This function can be implemented by using a keyword called virtual, and it can be‬
‭given during function declaration.These functions help to achieve runtime polymorphism.‬
‭A virtual function can be declared using a token(virtual) in C++. It can be achieved in C/Python‬
‭Language by using function pointers or pointers to function.‬

‭ ote :‬‭Every non-static method in Java is a virtual‬‭function except for final and private methods.‬
N
‭The methods that cannot be used for the polymorphism is not considered as a virtual function.‬

‭72. What is a pure virtual function?‬

‭ pure virtual function is only declared in the parent class. It is also referred to as an abstract‬
A
‭function. Pure virtual functions do not contain any definition in the base class. They must be‬
‭redefined in the subclass for the implementation needed.‬

‭ pure virtual function is a function which can be overridden in the derived class but cannot be‬
A
‭defined. A virtual function can be declared as Pure by using the operator =0.‬

‭73. What does the keyword virtual represented in the method definition?‬

‭It means we can override the method.‬

‭74. What is the super keyword?‬

‭ he super keyword is used to invoke the overridden method, which overrides one of its‬
T
‭superclass methods. This keyword allows to access overridden methods and also to access‬
‭hidden members of the superclass‬
‭When do you use the super keyword:‬
‭●‬ ‭Super is a Java keyword used to identify or refer parent (base) class.‬
‭●‬ ‭We can use super to access super class constructor and call methods of the super‬
‭class.‬
‭●‬ W ‭ hen method names are the same in super class and sub class, to refer super class,‬
‭the super keyword is used.‬
‭●‬ ‭To access the same name data members of parent class when they are present in‬
‭parent and child class.‬
‭●‬ ‭Super can be used to make an explicit call to no-arg and parameterized constructors of‬
‭the parent class.‬
‭●‬ ‭Parent class method access can be done using super, when child class has method‬
‭overridden.‬

‭75. Why new keyword is used in Java?‬

‭ hen we create an instance of class, i.e. objects, we use the Java keyword new. It allocates‬
W
‭memory in the heap area where JVM reserve space for an object. Internally, it invokes the‬
‭default constructor as well.‬

‭76. What is static and dynamic Binding?‬

‭ inding is a mechanism creating link between method call and method actual implementation.‬
B
‭Binding is nothing but the association of a name with the class.‬

‭●‬ p
‭ rivate, final and static members (methods and variables) use static binding while for‬
‭virtual methods (In Java methods are virtual by default) use dynamic binding.‬

‭77. Where is variable stored in dynamic allocation?‬

‭In dynamic allocation variable is stored in heap.‬

‭78. What is initializer list in c++?‬

‭ he initializer list is used to directly initialize data members of a class. An initializer list starts‬
T
‭after the constructor name and its parameters. The list begins with a colon ( : ) and is followed‬
‭by the list of variables that are to be initialized – all of​the variables are separated by a comma‬
‭ ith their values in curly brackets. Using an initialization list is almost identical to doing direct‬
w
‭initialization (or uniform initialization in C++11).‬

‭Syntax:‬
‭ onstructorname(datatype value1, datatype value2):datamember(value1),datamember(value2)‬
C
‭{‬
‭...‬
‭}‬

‭ 9. What is greedy alignment and structure padding?‬


7
‭https://www.includehelp.com/cpp-tutorial/size-of-a-class-in-cpp-padding-alignment-in-class-s‬
‭ize-of-derived-class.aspx‬

‭Extra Questions:‬

‭1.‬ W
‭ hat is return type of an constructor‬
‭Ans‬‭.‬‭Constructors don’t have a return type, not even‬‭void.‬

‭2.‬ D
‭ oes python support overloading and overriding.‬
‭Ans.‬‭Python does not support method overloading. We‬‭may overload the methods but‬
‭can only use the latest defined method.‬
‭But it supports overriding.‬

‭3.‬ W
‭ hat is singleton class?‬
‭Ans.‬‭In object-oriented programming, a singleton class‬‭is a class that can have only one‬
‭object (an instance of the class) at a time. After the first time, if we try to instantiate the‬
‭Singleton class, the new variable also points to the first instance created.‬

‭ emember the key points while defining class as a singleton class that is while‬
R
‭designing a singleton class:‬
‭●‬ ‭Make a constructor private.‬
‭●‬ ‭Write a static method that has the return type object of this singleton class. Here,‬
‭the concept of Lazy initialization is used to write this static method.‬

‭4.‬ W
‭ hy multiple inheritance is supported in C++ and not in java?‬
‭Ans.‬‭Java does not support multiple inheritance.The‬‭reason behind this is to prevent‬
‭ambiguity.Consider a case where class B extends class A and Class C and both class A‬
‭and C have the same method display(). Now java compiler cannot decide, which display‬
‭method it should inherit. To prevent such situation, multiple inheritances is not allowed in‬
‭java. However, a class can implement one or more interfaces, which has helped Java‬
‭get rid of the impossibility of multiple inheritances.‬
‭In C++ we have virtual functions to avoid diamond ambiguity.‬
‭5.‬ I‭f a class with all static methods has better performance, Why don't we make all‬
‭functions static.‬
‭Ans‬‭. Static methods are more efficient in terms of‬‭memory and time. They are slightly‬
‭faster than instance methods because in instance methods, you are also working with an‬
‭implicit “this” parameter.‬‭There is no need to pass‬‭the "this" reference in static‬
‭methods.That reference is passed in the ECX register so there is no additional stack‬
‭space required.‬‭Eliminating that parameter gives a‬‭slight performance boost in most‬
‭programming languages.‬
‭But static methods has its limitations too:‬
‭1.‬ ‭Static methods can not be overridden‬‭, since they are‬‭resolved using static‬
‭binding by the compiler at compile time. However, we can have the same name‬
‭methods declared static in both superclass and subclass, but it will be called‬
‭Method Hiding‬‭as the derived class method will hide‬‭the base class method.‬
‭2.‬ ‭Static methods can’t access instance methods and instance variables‬
‭directly.‬‭They must use reference to object. And static‬‭method can’t use‬‭this‬
‭keyword as there is no instance for ‘this’ to refer to.‬

‭6. W‬‭hat will happen if I don't write initialise‬‭main method with static in JAVA,‬
‭Ans.‬‭Bootstrap class loader searches for main function‬‭in the class file, if main function‬
‭is not declared as static, it will throw an error because declaring function as static allows‬
‭it to be called without instantiating that class file where the main function is.‬
‭main method should always be‬‭public static void main(String[]‬‭args)‬

‭if you try to change or miss anything, it will give you an error.‬

‭●‬ m ‭ ain method should be public because JVM should find it when class is‬
‭loaded.‬
‭●‬ ‭it should be static because there is no instance of the main method. It should‬
‭be called after loading the class. Hence static.‬
‭●‬ ‭it is void because it doesn’t have any return type‬

‭7. What is assignment operator overloading?‬

‭●‬ O ‭ verloading assignment operator in C++ copies all values of one object to‬
‭another object‬
‭●‬ ‭The object from which values are being copied is known as an instance‬
‭variable.‬
‭●‬ ‭A non-static member function should be used to overload the assignment‬
‭operator.‬
‭8. What is the difference between encapsulation and abstraction?‬

‭9. Can a C++ class have an object of self type?‬


‭ ns‬‭. A class declaration can contain static object‬‭of self type, it can also have pointer to self‬
A
‭type, but it cannot have a non-static object of self type.‬

‭ 0. How to stop a method from getting inherited?‬


1
‭Ans.‬‭In Java, there are 2 ways to stop or prevent‬‭inheritance. By using final keyword with a‬
‭class or by using a private constructor in a class.‬
‭In C++, You can't prevent inheritance (before C++11's final keyword) - you can only prevent‬
‭instantiation of inherited classes.The best you can do is prevent objects of type B from being‬
‭instantiated.‬
‭ 1.‬‭Can encapsulation be implemented using abstraction or vice versa ?‬
1
‭Ans.‬‭Abstraction shields the implementation details‬‭and encapsulation hides the object details.‬
‭The object is the abstract form of the real world and its details are hidden using encapsulation.‬
‭Thus encapsulation is required for abstraction.‬

‭12. Implement all the concepts practically.‬

‭ ------------------------------------------------------------------------------------------------------------------‬

‭What is “auto” keyword and where we can not use it?‬

‭ ypes of operators in C++‬


T
‭What is namespace‬
‭Struct vs class (Structure in c++ can have a constructor)‬
‭OOPs 6 features‬
‭Composition, Aggregation‬
‭Virtual function‬

‭ rite an example of dynamic polymorphism‬


W
‭STL, array container‬
‭STL, difference b/w map, unordered_map, unorderedmulti_map‬
‭Overview of implementation of the above containers‬
‭Iterators‬
‭Inheritance, can we create a private: constructors‬
‭&& operator ?‬
‭Types of constructors‬
‭Move constructors?‬
‭Difference b/w push_back and emplace_back‬
‭Smart pointers‬
‭Lambda functions‬
‭Enums‬
‭Struct unions‬

‭—--------------------------------------------------------------------------------------------------------------------‬

You might also like