C++ Interview Questions Part 3

How to reverse a vector in C++?

“syntax: reverse(the index you want to start reversing at, the index you want to end reversing at)
vector value = {11,22,33};
reverse(value.begin(),value.end());”

How to return a vector in C++?


“”#include

include

using namespace std;

vector fun(vector value_c)
{
vector value_d;
for(int j=0;j<value_c.size();j++)

    value_d.push_back(value_c[j]+4); 

    return value_d; 
    //returning the vector

}

int main()
{

    vector<int> value_a,value_b; 

    //We put values in vector value_a
    value_a.push_back(1); 
    value_a.push_back(2); 
    value_a.push_back(3); 


    value_b = fun(value_a); 
    //receiving the vector

    return 0; 

} “””

How to reverse an array in C++?


“”#include
using namespace std;

int main()
{
int n;
cin >> n;
int a[n];
int i;
for(i = 0; i < n; i++) { cin >> arr[i];
}
for(i = n-1; i >= 0; i–)
{
cout << arr[i] << ” “;
}
cout << endl;
return 0;

}
“””

C++ Interview Questions

How to split a string in C++?


“”#include

include

int main ()
{
char * flag_pointer;
char string_value[10]=””””Great Learning!””””;
flag_pointer =strtok (string_value,”””” !””””);

while (flag_pointer!=NULL)
{
cout<<string_value;
flag_pointer=strtok(NULL,”””” !””””);
}

return 0;
}”””

What is constructor in C++?

“Constructor is a method in class which has the same name as that of class and is followed by parentheses (). It is automatically called when an object of a class is created.

class Hello { // The class
public: // Access specifier
Hello() { // Constructor
cout << “”””Hello World!””””;
}
};

int main() {
Hello obj; // Create an object of Hello (this will call the constructor)
return 0;
}
“”

What is inheritance in C++?

“Just like a child inherits some features and attributes from his parent similarly a class inherit attributes and methods from another class. The parent class is called base class and the child class is called derived class.

// Base class
class Food_Item{
public:
void taste() {
cout << “”””The taste of every food item is different. \n”””” ;
}
};

// Derived class
class Chips: public Food_ Item{
public:
void taste() {
cout << “”””The taste of chips is salty \n”””” ; }
};
“””

Advance C++ Interview Questions

What is object in C++?

“Class in C++ provides a blueprint for object, that means, object is created from the class.

For example,
class Circle{

public:
float radius;
}

Circle C1;
Circle C2;”

What is encapsulation in C++?

“To prevent access to data directly, Encapsulation is the process that combines data variables and functions in a class. This is achieved by doing the following:

  1. Making all data variables private.
  2. Creating getter and setter functions for data variables.”

What is abstraction in C++?

“Abstraction in C++ means showing only what is necessary. It’s part of Object oriented Programming concept. Abstraction is used to hide any irrelevant data to the outside world and only showing what is absolutely necessary for the outside world to use.
eg. Classes use the abstraction concept to only show relevant data types or elements. This is done through access specifiers such as: public, private, protected.”

C++ Interview Questions

What is oops in C++?

“OOP or Object Oriented Programming in C++ is a type of programming in which you create objects and classes to emulate real world concepts such as Abstraction, Polymorphism, Encapsulation, Inheritance.
Here classes are data types that allow you list several types of data within it and even functions. You can access these classes with the help of class objects.”

What is member function in C++?

“Member functions are those functions that you declare within a class, they are members of the class. You can reference them using class objects. Eg.

class A
{
public:
int add(int b)
{
a = b * 10;
return a;
};
};”””

What is virtual base class in C++?

“Let’s understand this with an example.

You Have 4 classes: W,X,Y,Z
Here X & Y inherit from W. So they both have similar features being inherited from W.
Now, Z inherits from both X & Y

Here Z may inherit similar features from X & Y as they both have inherited them from W. This can cause issues and that’s why we use virtual base classes as they stop multiple features of a class from appearing in another class.”

Advance C++ Interview Questions

How to access private members of a class in C++?

“Private members of the class are not accessible by object or function outside the class. Only functions inside the class can access them or friend functions. However, pointers can be used to access private data members outside the class. Sample code is as follows:

include

using namespace std;

class sample_test{
private:
int n;

public:
sample_test() { n = 45; }
int display() {
return n;
}
}; “

How to call base class constructor from derived class in C++?

A base class constructor will be called whenever the derived class constructor is called. Upon the creation of a dervied class object the order of constructor execution is : base class constructor then Default class constructor.

What is an abstract class in C++?

“An abstract class in C++ is such that cannot be used directly and is used to form a base class for others to inherit from.
If you create an object for an abstract class the compiler will throw an error at you.”

C++ Interview Questions

What is containership in C++?

Containership in C++ is a relationship in which a class’s object is nested within another class. The class that contains the object is called a container class and the class whose object is stored is called a contained class.

What is data hiding in C++?

An object oriented technique of hiding data members is called data hiding. In other words, giving restricted access to the data members so as to maintain object integrity.

What is runtime polymorphism in C++?

“Polymorphism means having many forms either it is a function or operator in programming.
Runtime polymorphism is achieved by function overriding.

include

using namespace std;

class parent
{
public:

void print() 
{ cout<< """"base class""""; } 

};

class child:public parent
{
public:

void print() 
{ cout<< """"derived class""""; } 

};

int main()
{
parent *p;
child c;
p = &c;

//virtual function, binded at runtime (Runtime polymorphism) 
p->print();  
return 0; 

}
“””

Advance C++ Interview Questions

What is copy constructor in C++?

“A copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The syntax for copy constructor is as follows:

classname (const classname &obj) {
// body of constructor
}”

How is modularity introduced in C++?

“Modularity is a way of mapping encapsulated abstractions into real and physical modules which is closely related to Encapsulation. It is a concept in which separate programs are divided into separate modules.

For example, when building a house it is built in modular way. First foundation is laid, then structure is made and so on.”

What is the size of empty class in C++?

“Size of an empty class is 1 byte generally just to ensure that the two different objects will have different addresses.

This brings us to end of the blog on C++ Interview Questions. We hope you are now well-equipped with the kind of questions that may be asked during an Interview. Wondering where to learn the highly coveted in demand skills for free? Check out the courses on Great Learning”

C++ Interview Questions

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.”

What are class and object in C++?

“A class is a user-defined data type that has data members and member functions. Data members are the data variables and member functions are the functions that are used to perform operations on these variables.

An object is an instance of a class. Since a class is a user-defined data type so an object can also be called a variable of that data type.

A class is defined as-

class A{
private:
int data;
public:
void fun(){

}
};”

What is polymorphism in C++?

“Polymorphism in simple means having many forms. Its behavior is different in different situations. And this occurs when we have multiple classes that are related to each other by inheritance.

For example, think of a base class called a car that has a method called car brand(). Derived classes of cars could be Mercedes, BMW, Audi – And they also have their own implementation of a cars

The two types of polymorphism in C++ are:

Compile Time Polymorphism
Runtime Polymorphism”

Advance C++ Interview Questions

Explain constructor in C++

“The constructor is a member function that is executed automatically whenever an object is created. Constructors have the same name as the class of which they are members so that compiler knows that the member function is a constructor. And no return type is used for constructors.

Example:

class A{
private:
int val;
public:
A(int x){ //one argument constructor
val=x;
}
A(){ //zero argument constructor
}
}
int main(){
A a(3);

return 0;
}”

Tell me about virtual function

Virtual function is a member function in the base class that you redefine in a derived class. A virtual function is declared using the virtual keyword. When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer.

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;
}”

C++ Interview Questions

What are the C++ access specifiers?

“In C++ there are the following access specifiers:

Public: All data members and member functions are accessible outside the class.

Protected: All data members and member functions are accessible inside the class and to the derived class.

Private: All data members and member functions are not accessible outside the class.”

Define inline function

If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. One of the important advantages of using an inline function is that it eliminates the function calling overhead of a traditional function.

What is a reference in C++?

“A reference is like a pointer. It is another name of an already existing variable. Once a reference name is initialized with a variable, that variable can be accessed by the variable name or reference name both.

For example-

int x=10;
int &ref=x; //reference variable
If we change the value of ref it will be reflected in x. Once a reference variable is initialized it cannot refer to any other variable. We can declare an array of pointers but an array of references is not possible.”

Advance C++ Interview Questions

What do you mean by abstraction in C++?

Abstraction is the process of showing the essential details to the user and hiding the details which we don’t want to show to the user or hiding the details which are irrelevant to a particular user.

Is deconstructor overloading possible? If yes then explain and if no then why?

No destructor overloading is not possible. Destructors take no arguments, so there’s only one way to destroy an object. That’s the reason destructor overloading is not possible.

What do you mean by call by value and call by reference?

“In call by value method, we pass a copy of the parameter is passed to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the main function.

In call by reference method, we pass the address of the variable and the address is used to access the actual argument used in the function call. So changes made in the parameter alter the passing argument.”

C++ Interview Questions

What is an abstract class and when do you use it?

A class is called an abstract class whose objects can never be created. Such a class exists as a parent for the derived classes. We can make a class abstract by placing a pure virtual function in the class.

What are destructors in C++?

“A constructor is automatically called when an object is first created. Similarly when an object is destroyed a function called destructor automatically gets called. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde.

Example:

class A{
private:
int val;
public:
A(int x){
val=x;
}
A(){
}
~A(){ //destructor
}
}
int main(){
A a(3);
return 0;
}”

What are the static members and static member functions?

“When a variable in a class is declared static, space for it is allocated for the lifetime of the program. No matter how many objects of that class have been created, there is only one copy of the static member. So same static member can be accessed by all the objects of that class.

A static member function can be called even if no objects of the class exist and the static function are accessed using only the class name and the scope resolution operator ::”

Advance C++ Interview Questions

Explain inheritance?

Inheritance is the process of creating new classes, called derived classes, from existing classes. These existing classes are called base classes. The derived classes inherit all the capabilities of the base class but can add new features and refinements of their own.

What is a copy constructor?

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

Example-

class A{
int x,y;
A(int x, int y){
this->x=x;
this->y=y;
}

};
int main(){
A a1(2,3);
A a2=a1; //default copy constructor is called
return 0;
}”

What is the difference between virtual functions and pure virtual functions?

“A virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword.

Example-

class base{
public:
virtual void fun(){

}
};
A pure virtual function is a function that has no implementation and is declared by assigning 0. It has no body.

Example-

class base{
public:
virtual void fun()=0;
};”

C++ Interview Questions

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;
}
}”

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;”

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”

Advance C++ Interview Questions

What is the difference between function overloading and operator overloading?

“Function overloading: Function overloading is defined as we can have more than one version of the same function. The versions of a function will have different signature means that they have a different set of parameters.

Operator overloading: Operator overloading is defined as the standard operator can be redefined so that it has a different meaning when applied to the instances of a class.”

What is a class template?

“A class template is used to create a family of classes and functions. For example, we can create a template of an array class which will enable us to create an array of various types such as int, float, char, etc. Similarly, we can create a template for a function, suppose we have a function add(), then we can create multiple versions of add().

The syntax of a class template:

template
class class name
{
// body of class;
};
Syntax of a object of a template class:

classname objectname(arglist); “

What is a pure virtual function?

“The pure virtual function is a virtual function which does not contain any definition. The normal function is preceded with a keyword virtual. The pure virtual function ends with 0.

Syntax of a pure virtual function:

virtual void abc()=0; //pure virtual function.
Let’s understand this through an example:

include

using namespace std;
class Base
{
public:
virtual void show()=0;
};

class Derived:public Base
{
public:
void show()
{
cout<<“”javaTpoint””; } }; int main() { Base* b; Derived d; b=&d; b->show();
return 0;
} “

C++ Interview Questions

What is the difference between delete and delete[]?

Delete [] is used to release the array of allocated memory which was allocated using new[] whereas delete is used to release one chunk of memory which was allocated using new.

What does Scope Resolution operator do?

A scope resolution operator(::) is used to define the member function outside the class.

What is the purpose of the “delete” operator?

The “delete” operator is used to release the dynamic memory created by “new” operator.

Advance C++ Interview Questions

C++ Part 1C++ Part 2
Back to top