0% found this document useful (0 votes)
6 views

OOPS through C via va questions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOPS through C via va questions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

OOPS through C++ Via va

1) What are the limitations of OOPs?


 Requires intensive testing processes.
 Solving problems takes more time as compared to Procedure Oriented
Programming.
 Software developed using this approach requires a substantial amount of pre-
work and planning.
 OOP code is difficult to understand if you do not have the corresponding class
documentation.
 In certain scenarios, these programs can consume a large amount of memory.
 Takes more time to solve problems.
2) What are the differences between object-oriented programming and structural
programming?
Object-oriented Programming Structural Programming

It follows a bottom-up approach. It follows a top-down approach.

It is used to solve moderate


It is used to solve complex problems
problems.

It focuses on the process or logical


It focuses on data.
structure.
3) What is the purpose of using OOPs concepts?
 The aim of OOP is to implement real-world entities like inheritance, hiding,
polymorphism in programming.
 The main purpose of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data except
that function.
4) What do you understand by pure object-oriented language?
 Abstraction
 All predefined types are objects
 All user-defined types are objects
 Encapsulation
 Inheritance
 Polymorphism
5) What are the disadvantages of OOP?
 Programmer should be well skilled.
 Classes tend to be overly generalized.
 Proper planning is required.
 Program design is tricky.
6) What are the differences between class and object?
Class Object

It is conceptual It is real

It is a logical entity It is a real-world entity

It is declared once. Multiple objects can be declared as and when required


7) What do you understand by class and object?
 A class is simply a representation of a type of object. It is the
blueprint/plan/template that describes the details of an object.
 It is a user-defined data type. Inside a class, we define variables, constants,
member functions, and other functionality.
 It does not consume memory at run time. Note that classes are not considered
as a data structure. It is a logical entity. It is the best example of data binding.

 An object is an instance of a class. It has its own state, behavior, and identity.
 An object is a real-world entity that has attributes, behavior, and properties.
 It is referred to as an instance of the class. It contains member functions,
variables that we have defined in the class. It occupies space in the memory.
Different objects have different states or attributes, and behaviors.
8) What are the differences between the constructor and the method in Java?
Constructor Method

It creates an instance of a class It is used to execute Java code

It cannot be declared as static It can be declared as static

It creates an instance of a class It is used to execute Java code


9) What are the manipulators in OOP and how it works?
 Manipulators are helping functions. It is used to manipulate or modify the
input or output stream.
 The modification is possible by using the insertion (<<) and extraction (>>)
operators.
10) What is the concept of access specifiers when should we use these?
 In OOPs language, access specifiers are reserved keyword that is used to set
the accessibility of the classes, methods and other members of the class.
 It includes public, private, and protected.
 There is some other access specifier that is language-specific.
 These access specifiers play a vital role in achieving one of the major
functions of OOP, i.e. encapsulation.
11) What are the key differences between class and structure?
Class Structure

It deals with data members and member functions It deals with data members only

It supports inheritance It does not support inheritance

keyword class defines a class keyword struct defines a structure


12) What are the rules for creating a constructor?
 It cannot be marked as abstract.
 It cannot be overridden.
 It cannot be final.
 It cannot have a return type.
 It must have the same name as the Class name.
 It cannot be marked as static.
13) Name any seven widely used OOP languages.
 Dart
 C++
 C#
 Ruby
 Python
 Java
 Go
14) What is the difference between new and override?
override: Overrides the functionality of a virtual method in a base class, providing different
functionality.
New: Hides the original method (which doesn’t have to be virtual), providing different
functionality. This should only be used where it is absolutely necessary.
15) Explain overloading and overriding with example?
 Overloading is a concept in OOP when two or more methods in a class with
the same name but the method signature is different.
 If a method with the same method signature is presented in both child and
parent class is known as method overriding.
16) Name the operators that cannot be overload.
1. Scope Resolution Operator (::)
2. Ternary Operator (? )
3. Member Access or Dot Operator (.)
4. Pointer to Member Operator (.*)
5. sizeof operator
17) How does procedural programming be different from OOP differ?
Procedural Oriented Programming OOPS Differ

Reuse of code is not allowed The code can be reused

It follows a top-down approach It follows a bottom-up approach.

Modification and extension of code are not easy We can easily modify and extend code
18) What do you understand by OOP?
 OOP stands for object-oriented programming.
 It is a programming paradigm that revolves around the object rather than
function and procedure.
 In other words, it is an approach for developing applications that emphasize on
objects. An object is a real word entity that contains data and code.
19) What is Coupling in OOP and why it is helpful?
 In programming, separation of concerns is known as coupling.
 It means that an object cannot directly change or modify the state or behaviour
of other objects.
 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.
20) What are the advantages of OOP?
 It follows a bottom-up approach.
 It models the real word well.
 It allows us the reusability of code.
 Decompose a complex problem into smaller chunks.
 Programmer are able to reach their goals faster.
 Minimizes the complexity.
21) What are the limitations of inheritance?
 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
22) What are the differences between Inheritance and Polymorphism?
Inheritance Polymorphism

In order to achieve polymorphism,


It is required in order to achieve polymorphism
inherence is not required

It is applied to classes. It is applied to functions and methods.

Inheritance is one in which a derived class inherits Polymorphism is one that you can define
the already existing class’s features in different forms
23) What is constructor chaining?
 In OOPs, constructor chaining is a sequence of invoking constructors upon
initializing an object. It is used when we want to invoke a number of
constructors, one after another by using only an instance.
24) What are the differences between copy constructor and assignment operator?
Copy Constructor Assignment Operator

The copy constructor is used when a new object is It is used when we want to assign an existing
created with some existing object. object to a new object

It is an overloaded constructor It is an operator

If no copy constructor is defined in the class, the If the assignment operator is not overloaded
compiler provides one then the bitwise copy will be made
25) What is the difference between Composition and Inheritance?
 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.
26) What is composition?
 Composition is one of the vital concepts in OOP.
 It describes a class that references one or more objects of other classes in
instance variables.
 It allows us to model a has-a association between objects.
27) Why OOP is so popular?
 OOPs, programming paradigm is considered as a better style of programming.
 Not only that, the main pillar of OOPs – Data Abstraction, Encapsulation,
Inheritance, and Polymorphism, makes it easy for programmers to solve
complex scenarios.
28) What are the characteristics of an abstract class?
 Instantiation of an abstract class is not allowed. It must be inherited.
 An abstract class can have both abstract and non-abstract methods.
 You must declare at least one abstract method in the abstract class.
 It is always public.
 An abstract class must have at least one abstract method.
29) What are the differences between error and exception?
Exception Error

It can be classified into two categories i.e. checked and


All errors in Java are unchecked
unchecked

It occurs at compile time or run time It occurs at run time

Exception can be recovered by using the try-catch block An error cannot be recovered
30) What are the four main features of OOPs?
 Inheritance
 Encapsulation
 Polymorphism
 Data Abstraction
31) What is OOPS?
OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.
32) What is Encapsulation?
Encapsulation is an attribute of an object, and it contains all data which is hidden. That
hidden data can be restricted to the members of that class.
Levels are Public, Protected, Private, Internal, and Protected Internal.
33) What is Polymorphism?
Polymorphism is nothing but assigning behavior or value in a subclass to something that was
already declared in the main class. Simply, polymorphism takes more than one form.
34) What is Inheritance?
Inheritance is a concept where one class shares the structure and behavior defined in another
class. If Inheritance applied to one class is called Single Inheritance, and if it depends on
multiple classes, then it is called multiple Inheritance.
35) Explain the term constructor
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 a class name.
 A constructor must have no return type.
36) Define Destructor?
A destructor is a method which is automatically called when the object is made of scope or
destroyed. Destructor name is also same as class name but with the tilde symbol before the
name.
37) What is an Inline function?
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.
38) What is a virtual function?
 A virtual function is a member function of a class, and its functionality can be
overridden in its derived class. This function can be implemented by using a keyword
called virtual, and it can be given during function declaration.
 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.
39) What is a friend function?
 A friend function is a friend of a class that is allowed to access to Public, private, or
protected data in that same class. If the function is defined outside the class cannot
access such information.
 A friend can be declared anywhere in the class declaration, and it cannot be affected
by access control keywords like private, public, or protected.
40) What is function overloading?
Function overloading is a regular function, but it is assigned with multiple parameters. It
allows the creation of several methods with the same name which differ from each other by
the type of input and output of the function.

Example
void add(int& a, int& b);
void add(double& a, double& b);
void add(struct bob& a, struct bob& b);

41) What is operator overloading?


Operator overloading is a function where different operators are applied and depends on the
arguments. Operator,-,* can be used to pass through the function, and it has its own
precedence to execute
42) What is an abstract class?
An abstract class is a class which cannot be instantiated. Creation of an object is not possible
with an abstract class, but it can be inherited. An abstract class can contain only an Abstract
method. Java allows only abstract method in abstract class while other languages allow non-
abstract method as well.
43) What is a ternary operator?
The ternary operator is said to be an operator which takes three arguments. Arguments and
results are of different data types, and it depends on the function. The ternary operator is also
called a conditional operator.
44)What are the different types of arguments?
A parameter is a variable used during the declaration of the function or subroutine, and
arguments are passed to the function body, and it should match with the parameter defined.
There are two 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 in both inside and outside the
functions and it returns the same or different value.
45) What is method overriding?
Method overriding is a feature that allows a subclass to provide the implementation of a
method that overrides in the main class. It will override the implementation in the superclass
by providing the same method name, same parameter, and same return type.
46) What is the main difference between overloading and overriding?
 Overloading is static Binding, whereas Overriding is dynamic Binding. Overloading
is nothing but the same method with different arguments, and it may or may not return
the equal value in the same class itself.
 Overriding is the same method names with the same arguments and return types
associated with the class and its child class.
47) What is an abstraction?
Abstraction is a useful feature of OOPS, and it shows only the necessary details to the client
of an object. Meaning, it shows only required details for an object, not the inner constructors,
of an object. Example – When you want to switch on the television, it is not necessary to
know the inner circuitry/mechanism needed to switch on the TV. Whatever is required to
switch on TV will be shown by using an abstract class.
48) What are the access modifiers?
Access modifiers determine the scope of the method or variables that can be accessed from
other various objects or classes. There are five types of access modifiers, and they are as
follows:
 Private
 Protected
 Public
 Friend
 Protected Friend
49) What is exception handling?
An exception is an event that occurs during the execution of a program. Exceptions can be of
any type – Runtime exception, Error exceptions. Those exceptions are adequately handled
through exception handling mechanism like try, catch, and throw keywords.
50) What are tokens?
A compiler recognizes a token, and it cannot be broken down into component elements.
Keywords, identifiers, constants, string literals, and operators are examples of tokens.
Even punctuation characters are also considered as tokens. Example: Brackets, Commas,
Braces, and Parentheses.
51) How can we call the base method without creating an instance?
Yes, it is possible to call the base method without creating an instance. And that method
should be “Static method.”
Doing Inheritance from that class.-Use Base Keyword from a derived class.
52) What are the various types of constructors?
There are three types of constructors:
– Default Constructor – With no parameters.
– Parametric Constructor – With Parameters. Create a new instance of a class and also
passing arguments simultaneously.
– Copy Constructor – Which creates a new object as a copy of an existing object.
53) What is early and late Binding?

Types of Polymorphism
Ty

TyCompile Time Polymorphism


TyRun-time Polymorphism
(Static or Early Binding) (Dynamic or Late Binding)
 Compile Time Polymorphism: Compile time polymorphism, also known as Static
Polymorphism, refers to the type of 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.
 Runtime Polymorphism: Runtime polymorphism, also known as Dynamic
Polymorphism, refers to the type of 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.
 Early binding refers to the assignment of values to variables during design time,
whereas late Binding refers to the assignment of values to variables during run time.
54) What is ‘this’ pointer?
THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which
differentiates between the current object with the global object. It refers to the current object.
55) What is the difference between structure and a class?
The default access type of a Structure is public, but class access type is private. A structure is
used for grouping data, whereas a class can be used for grouping data and methods.
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.
56) What is the default access modifier in a class?
The default access modifier of a class is Internal and the default access modifier of a class
member is Private.
57) What is a pure virtual function?
A pure virtual function is a function which can be overridden in the derived class but cannot
be defined. A virtual function can be declared as Pure by using the operator =0.

Example –
Virtual void function1() // Virtual, Not pure
Virtual void function2() = 0 //Pure virtual

58) 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 (.*)
59) What is dynamic or run time polymorphism?
Dynamic or Run time polymorphism is also known as method overriding in which call to an
overridden function is resolved during run time, not at the compile time. It means having two
or more methods with the same name, same signature but with different implementation.
60) Do we require a parameter for constructors?
No, we do not require a parameter for constructors.
61) What does the keyword virtual represented in the method definition?
It means we can override the method.
62) Whether static method can use nonstatic members?
False.
63) What are a base class, subclass, and superclass?
 The base class is the most generalized class, and it is said to be a root class.
 A Subclass is a class that inherits from one or more base classes.
 The superclass is the parent class from which another class inherits.
64) What is static and dynamic Binding?
 Binding is nothing but the association of a name with the class. Static Binding is a
binding in which name can be associated with the class during compilation time, and
it is also called as early Binding.
 Dynamic Binding is a binding in which name can be associated with the class during
execution time, and it is also called as Late Binding.
65) How many instances can be created for an abstract class?

Zero instances will be created for an abstract class. In other words, you cannot create an
instance of an Abstract Class.
66) Which keyword can be used for overloading?
Operator keyword is used for overloading.
67)Which OOPS concept is used as a reuse mechanism?
Inheritance is the OOPS concept that can be used as a reuse mechanism.
68) Which OOPS concept exposes only the necessary information to the calling
functions?
Encapsulation
69) 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:
 Imperative Programming Paradigm
 Declarative Programming Paradigm
Now, these paradigms can be further classified based:

1. Imperative Programming Paradigm: Imperative programming focuses on HOW to


execute program logic and defines control flow as statements that change a program state.
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.
2. Declarative Programming Paradigm: Declarative programming focuses on 70) WHAT
to execute and defines program logic, but not a detailed control flow. Declarative paradigm
can be further classified into:
a) Logical Programming Paradigm : Logical programming paradigm is based on formal
logic, which 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
70) What is the difference between realloc( ) and free( ) ?
The free subroutine frees a block of memory previously allocated by the malloc subroutine.
Undefined result s occur if the Pointer param eter is not a valid pointer. I f the Pointer
parameter is a null value, no action will occur. The realloc subroutine changes the size of the
block of memory pointed to by the Pointer parameter to the number of bytes specified by the
Size parameter and returns a new pointer to the block. The pointer specified by the Pointer
parameter must have been created with the malloc, calloc, or realloc subroutines and not been
deallocated with the free or realloc subroutines. Undefined result s occur if the Pointer
parameter is not a valid pointer
71) How much memory does a class occupy?
Classes do not consume any memory. They are just a blueprint based on which objects are
created. Now when objects are created, they actually initialize the class members and
methods and therefore consume memory.
72). Is it always necessary to create objects from class?
No. An object is necessary to be created if the base class has non-static methods. But if the
class has static methods, then objects don’t need to be created. You can call the class
method directly in this case, using the class name.
73) Are there any limitations of Inheritance?
Yes, with more powers comes more complications. Inheritance is a very powerful feature in
OOPs, but it has some limitations too. 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. So if not correctly implemented,
this might lead to unexpected errors or incorrect outputs.
74)What are the various types of inheritance?
The various types of inheritance include:
 Single inheritance
 Multiple inheritances
 Multi-level inheritance
 Hierarchical inheritance
 Hybrid inheritance
75) What is a subclass?
The subclass is a part of Inheritance. The subclass is an entity, which inherits from another
class. It is also known as the child class.
76) Define a superclass?
Superclass is also a part of Inheritance. The superclass is an entity, which allows subclasses
or child classes to inherit from itself.
77) What is namespace in C++?
If there are two or more functions with the same name defined in different libraries then how
will the compiler know which one to refer to? Thus namespace came to picture. A namespace
defines a scope and differentiates functions, classes, variables etc. with the same name
available in different libraries. The namespace starts with the keyword “namespace”. The
syntax for the same is as follows:
namespace namespace_name {
// code declarations
}
78) What is meant by Garbage Collection in OOPs world?
 Object-oriented programming revolves around entities like objects. Each object
consumes memory and there can be multiple objects of a class. So if these objects
and their memories are not handled properly, then it might lead to certain memory-
related errors and the system might fail.
 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
79) What is the difference between reference and pointer?
Reference Pointer

Reference behaves like an alias for an


The pointer is a variable which stores the
existing variable, i.e., it is a temporary
address of a variable.
variable.

Reference variable does not require any


indirection operator to access the value. A Pointer variable requires an indirection operator
reference variable can be used directly to to access the value of a variable.
access the value.

Once the reference variable is assigned, The pointer variable is an independent variable
then it cannot be reassigned with different means that it can be reassigned to point to
address values. different objects.

A null value cannot be assigned to the A null value can be assigned to the reference
reference variable. variable.

80) What are the different data types present in C++?

The 4 data types in C++ are given below:


• Primitive Datatype(basic datatype). Example- char, short, int, float, long, double, bool,
etc.
• Derived datatype. Example- array, pointer, etc.
• Enumeration. Example- enum
• User-defined data types. Example- structure, class, etc
81) What is template in C++?
A template in C++ is used to pass data types as parameters . These make it easier and more
simpler to use classes and functions.

template(class type)ret-type func-name(parameter list){


//body of the function
}

82) What is pointer in C++?


Pointers in C++ are a data type that store the memory address of another variable.
Pointer Declaration Syntax
datatype *variable_name;

83) Explain what is the use of void main () in C++ language?


To run the C++ application it involves two steps, the first step is a compilation where
conversion of C++ code to object code take place. While second step includes linking, where
combining of object code from the programmer and from libraries takes place. This function
is operated by main () in C++ language.
84) What is a scope resolution operator?
A scope resolution operator is represented as :
• This operator is used to associate function definition to a particular class.
• The scope operator is used for the following purposes:
• To access a global variable when you have a local variable with the same name.
• To define a function outside the class.
85) Comment on Local and Global scope of a variable in C++.

The scope of a variable is defined as the extent of the program code within which the variable
remains active i.e. it can be declared, defined or worked with.
There are two types of scope in C++:
Local Scope: A variable is said to have a local scope or is local when it is declared inside a
code block. The variable remains active only inside the block and is not accessible outside the
code block.
Global Scope: A variable has a global scope when it is accessible throughout the program. A
global variable is declared on top of the program before all the function definitions.
#include <iostream.h>
int globalResult=0; //global variable
int main()
{
int localVar = 10; //local variable.

86) When there are a Global variable and Local variable with the same name, how will
you access the global variable?
When there are two variables with the same name but different scope, i.e. one is a local
variable and the other is a global variable, the compiler will give preference to a local
variable.
In order to access the global variable, we make use of a “scope resolution operator (::)”.
Using this operator, we can access the value of the global variable.
#include<iostream.h>
int x= 10;
int main()
{
int x= 2;
cout<<"Global Variable x = "<<::x;
cout<<"\nlocal Variable x= "<<x;
}

Output:
Global Variable x = 10
local Variable x= 2

87) What is a Storage Class? Mention the Storage Classes in C++.


Storage class determines the life or scope of symbols such as variable or functions.

88) What is the difference between a++ and ++a?


• a++ refers to post-increment operation. The C++ compiler first uses the value of a and
then increments it.
• ++a refers to the pre-increment operation. The C++ compiler first increments the value of
a++ and then uses a.
89) Explain dangling pointer.
When the address of an object is used after its lifetime is over, dangling pointer comes into
existence.
Some examples of such situations are : Returning the addresses of the automatic variables
from a function or using the address of the memory block after it is freed.
90) What is the keyword auto for?
By default, every local variable of the function is automatic i.e. auto. In the below function
both the variables ‘i’ and ‘j’ are automatic variables.
void func(){
int i;
auto int j;
}
A global variable is not an automatic variable.
91) What is the purpose of the Extern Storage Specifier?
“Extern” specifier is used to resolve the scope of a global symbol.
#include <iostream >
using nam espace std;
main(){
extern int i;
cout<<i<<endl;
}
int i = 20;
In the above code, “i” can be visible outside the file where it is defined.
92) What is stl in C++?
Stl is the standard template library. It is a library that allows you to use a standard set of -
templates for things such as: Algorithms, functions, Iterators in place of actual code.
93) What is stream in C++?
Stream refers to a stream of characters to be transferred between program thread and i/o.
94) What is enum in C++?
enum is abbreviation of Enumeration which assigns names to integer constant to make a
program easy to read. Syntax for the same:
enum enum_name{const1, const2, ……. };

95) What are void pointers?


A void pointer is a pointer which is having no datatype associated with it. It can hold
addresses of any type.
For example-
void *ptr;
char *str;
p=str; // no error
str=p; // error because of type mismatch
We can assign a pointer of any type to a void pointer but the reverse is not true unless you
typecast it as
str=(char*) ptr;

96) How do you allocate and deallocate memory in C++?


The new operator is used for memory allocation and deletes operator is used for memory
deallocation in C++.
For example-
int value=new int; //allocates memory for storing 1 integer
delete value; // deallocates memory taken by value

int *arr=new int[10]; //allocates memory for storing 10 int


delete []arr; // deallocates memory occupied by arr
97) How are constructor called?
There are two ways in which the constructors are called:
Implicitly: Constructors are implicitly called by the compiler when an object of the class is
created. This creates an object on a Stack.
Explicit Calling: When the object of a class is created using new, constructors are called
explicitly. This usually creates an object on a Heap.
class A{
int x; int y;
public A() : x(0) , y(0) {} //default (no argument) constructor
};
main()
{
A Myobj; // Implicit Constructor call. In order to allocate memory on stack,
//the default constructor is implicitly called.
A * pPoint = new A(); // Explicit Constructor call. In order to allocate
//memory on HEAP we call the default constructor.
}
98) What is the role of the Static keyword for a class member variable?
The static member variable shares a common memory across all the objects created for the
respective class. We need not refer to the static member variable using an object. However, it
can be accessed using the class name itself.
99) What’s the order in which the local objects are destructed?
Consider following a piece of code:
Class A{
...
};
int main()
{
A a;
A b;
...
}
In the main function, we have two objects created one after the other. They are created in
order, first a then b. But when these objects are deleted or if they go out of the scope, the
destructor for each will be called in the reverse order in which they were constructed. Hence,
the destructor of b will be called first followed by a. Even if we have an array of objects, they
will be destructed in the same way in the reverse order of their creation.
100) Name the Operators that cannot be Overloaded.
sizeof – sizeof operator
. – Dot operator
.* – dereferencing operator
-> – member dereferencing operator
:: – scope resolution operator
?: – conditional operator
101) What are Multiple Inheritances (virtual inheritance)? What are its advantages and
disadvantages?
In multiple inheritances, we have more than one base classes from which a derived class can
inherit. Hence, a derived class takes the features and properties of more than one base class.
For Example, a class driver will have two base classes namely, employee and a person
because a driver is an employee as well as a person. This is advantageous because the driver
class can inherit the properties of the employee as well as the person class.
But in the case of an employee and a person, the class will have some properties in common.
However, an ambiguous situation will arise as the driver class will not know the classes from
which the common properties should be inherited. This is the major disadvantage of multiple
inheritances.
102) What do you know about friend class and friend function?
A friend class can access private, protected, and public members of other classes in which it
is declared as friends. Like friend class, friend function can also access private, protected, and
public members. But, Friend functions are not member functions.
For example -
class A{
private:
int data_a;
public:
A(int x){
data_a=x;
}
friend int fun(A, B);
}
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
}
friend int fun(A, B);
}
int fun(A a, B b){
return a.data_a+b.data_b;
}
int main(){
A a(10);
B b(20);
cout<<fun(a,b)<<endl;
return 0;
}
103) What is Name Mangling?
C++ compiler encodes the parameter types with function/method into a unique name. This
process is called name mangling. The inverse process is called as demangling.
Example:
A::b(int, long) const is mangled as ‘b__C3Ail’.
For a constructor, the method name is left out.
That is A:: A(int, long) const is mangled as ‘C3Ail’.
104) What are Virtual Functions?
A virtual function allows the derived classes to replace the implementation provided by the
base class. Whenever we have functions with the same name in the base as well as derived
class, there arises an ambiguity when we try to access the child class object using a base class
pointer. As we are using a base class pointer, the function that is called is the base class
function with the same name.
To correct this ambiguity we use the keyword “virtual” before the function prototype in the
base class. In other words, we make this polymorphic function Virtual. By using a Virtual
function, we can remove the ambiguity and we can access all the child class functions
correctly using a base class pointer.
105) What do you mean by Pure Virtual Functions?
A Pure Virtual Member Function is a member function in which the base class forces the
derived classes to override. Normally this member function has no implementation. Pure
virtual functions are equated to zero.
Example:
class Shape { public: virtual void draw() = 0; };
Base class that has a pure virtual function as its member can be termed as an “Abstract class”.
This class cannot be instantiated and it usually acts as a blueprint that has several sub-classes
with further implementation.
106) What is a friend function?
C++ class does not allow its private and protected members to be accessed outside the class.
But this rule can be violated by making use of the “Friend” function.
As the name itself suggests, friend function is an external function that is a friend of the class.
For friend function to access the private and protected methods of the class, we should have a
prototype of the friend function with the keyword “friend” included inside the class.
107) What is a friend class?
Friend classes are used when we need to override the rule for private and protected access
specifiers so that two classes can work closely with each other.
Hence, we can have a friend class to be a friend of another class. This way, friend classes can
keep private, inaccessible things in the way they are.
When we have a requirement to access the internal implementation of a class (private
member) without exposing the details by making the public, we go for friend functions.
108) How to use map in C++?
Associative containers storing a combination of a key value or mapped value is called Maps.
Syntax: map<key_type , value_type> map_name;
#include <iostream>
#include <iterator>
#include <map>

using namespace std;

int main()
{
map<int, int> test;

// inserting elements
test.insert(pair<int, int>(1, 2));
test.insert(pair<int, int>(2, 3));

map<int, int>::iterator itr;


for (itr = test.begin(); itr != test.end(); ++itr) {
cout << itr->first
cout << itr->second << '\n';
}
return 0;
)
109) What is namespace std; and what is consists of?
Namespace std; defines your standard C++ library, it consists of classes, objects and
functions of the standard C++ library. You can specify the library by using namespace std or
std: : throughout the code. Namespace is used to differentiate the same functions in a library
by defining the name.
110) What is an Iterator class?
 In C++ a container class is a collection of different objects. If we need to traverse
through this collection of objects, we cannot do it using simple index variables.
Hence, we have a special class in STL called an Iterator class which can be used to
step through the contents of the container class.
 The various categories of iterators include input iterators, output iterators, forward
iterators, bidirectional iterators, random access, etc.

111) What is the difference between an External Iterator and an Internal Iterator?
Describe an advantage of the External Iterator.
An internal iterator is implemented with member functions of the class that has items to step
through.
An external iterator is implemented as a separate class that can be bound to the object that has
items to step through. The basic advantage of an External iterator is that it’s easy to
implement as it is implemented as a separate class.
Secondly, as it’s a different class, many iterator objects can be active simultaneously.
112) What is vector in C++?
A sequence of containers to store elements, a vector is a template class of C++. Vectors are
used when managing ever-changing data elements. The syntax of creating vector.
vector (type) variable (number of elements)
113) Explain what is upcasting in C++?
Upcasting is the act of converting a sub class references or pointer into its super class
reference or pointer is called upcasting.
114) Explain what is pre-processor in C++?
Pre-processors are the directives, which give instruction to the compiler to pre-process the
information before actual compilation starts.
115) What is a virtual destructor?
 A virtual destructor in C++ is used in the base class so that the derived class object
can also be destroyed. A virtual destructor is declared by using the ~ tilde operator
and then virtual keyword before the constructor.
 Constructor cannot be virtual, but destructor can be virtual.
116) Can we call a virtual function from a constructor?
Yes, we can call a virtual function from a constructor. But the behavior is a little different in
this case. When a virtual function is called, the virtual call is resolved at runtime. It is always
the member function of the current class that gets called. That is the virtual machine doesn’t
work within the constructor.
For Example:
class base{
private:
int value;
public:
base(int x){
value=x;
}
virtual void fun(){

}
}

class derived{
private:
int a;
public:
derived(int x, int y):base(x){
base *b;
b=this;
b->fun(); //calls derived::fun()
}
void fun(){
cout<<"fun inside derived class"<<endl;
}
}
117) What are Default Parameters? How are they evaluated in the C++ function?
Default Parameter is a value that is assigned to each parameter while declaring a function.
This value is used if that parameter is left blank while calling to the function. To specify a
default value for a particular parameter, we simply assign a value to the parameter in the
function declaration.
If the value is not passed for this parameter during the function call, then the compiler uses
the default value provided. If a value is specified, then this default value is stepped on and the
passed value is used.
int multiply(int a, int b=2)
{
int r;
r = a * b;
return r;
}

int main()
{

cout<<multiply(6);
cout<<"\n";
cout<<multiply(2,3);
}

118) Why are arrays usually processed with for loop?


Array uses the index to traverse each of its elements.
If A is an array then each of its elements is accessed as A[i]. Programmatically, all that is
required for this to work is an iterative block with a loop variable i that serves as an index
(counter) incrementing from 0 to A.length-1. This is exactly what a loop does and this is the
reason why we process arrays using for loops.
119) How would you establish the relationship between abstraction and encapsulation?
Data abstraction refers to representing only the important features of the real-world object in
the C++ program. This entire process does not include any explanation and background
details of the code. Abstraction is implemented with the help of classes and objects, whereas,
data encapsulation is the most significant feature of a class.
Data encapsulation in C++ refers to the wrapping up of data members and member functions
that operate on the data as a single unit, called the class. Encapsulation prevents free access to
the members of the class.
120) How to reverse a string in C++?
To reverse a string, a sample code is mentioned below.
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char n[50], t;
int i, j;
cout << "Enter a string : ";
gets(n);
i = strlen(n) - 1;
for (j = 0; j < i; j++,i--)
{
t = s[j];
s[j] = s[i];
s[i] = t;
}
cout << "\nReverse string : " << s;
return 0;
}

You might also like