C++ Interview Questions (Frequently Asked) : For Free Interview Preparation Check The Links Below
C++ Interview Questions (Frequently Asked) : For Free Interview Preparation Check The Links Below
C++ Interview Questions (Frequently Asked) : For Free Interview Preparation Check The Links Below
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Akhil Bhadwal
Share:
5 Comments
Table of Contents
C++ is still as relevant today as it was during its advent in the mid-80s. The imperative, object-
oriented programming language is widely used as a general-purpose programming language.
As such, several jobs require candidates to have a profound understanding of C++. If you wish
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
to sharpen your C++ Language skills further, here are some of the best C++ tutorials that can
help you out.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
However, the process followed in transferring the data through email is not displayed because
it is of no use to you.
Question: Define an Inline Function in C++? Write its syntax. Is it possible for the
C++ compiler to ignore inlining?
Answer: In order to reduce the function call overhead, C++ offers inline functions. As the
name suggests, an inline function is one that is expanded in line when it is called.
As soon as the inline function is called, the whole code of the same gets either inserted or
substituted at the particular point of the inline function call. The substitution is complete by the
C++ compiler at compile time. Small inline functions might increase program efficiency.
The syntax of a typical inline function is:
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
As the inlining is a request, not a command, the compiler can ignore it.
Question: Explain the significance of vTable and vptr in C++ and how the
compiler deals with them
Answer: vTable is a table containing function pointers. Every class has a vTable. vptr is a
pointer to vTable. Each object has a vptr. In order to maintain and use vptr and vTable, the
C++ compiler adds additional code at two places:
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
• C++ has destructors, which are invoked automatically when an object is destroyed.
Java has something called automatic garbage collection
• C++ supports multiple inheritance, operator overloading, pointers, structures,
templates, and unions. Java doesn’t have any of them
• Java has a Thread class that is inherited in order to create a new thread. C++ has no
inbuilt support for threads
• In C++, a goto statement offers a way to jump from a location to some labeled
statement in the same function. There is no goto statement in Java
• C++ run and compile using the compiler, which converts the source code into
machine level language. Hence, it is platform-dependent. Java compiler, on the other
hand, converts the source code into JVM bytecode, which is platform-independent.
#include
int main()
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
sum += numbers[i];
return 0;
Question: What are the most important differences between C and C++?
Answer:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
• Mainly used input and output in C are scanf() and printf(), respectively. In C++, cin is
the standard input stream while cout serves as the standard output stream
• While C is a procedural programming language, C++ provides support for both
procedural and object-oriented programming approaches
• A base class
• A derived class
• A function with the same name in both the classes i.e. the base class and the derived
class
• A pointer or reference of base class type that points or refers, respectively to an
object of the derived class
An example demonstrating the use of virtual functions (or runtime polymorphism at play) is:
#include
class Base {
public:
};
public:
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
};
int main(void) {
return 0;
In the aforementioned program bp is a pointer of type Base. A call to bp->show() calls show()
function of the Derived class. This is because bp points to an object of the Derived class.
1. When deriving a structure from a class or some other structure, the default access
specifier for the base class or structure is public. On the contrary, default access
specifier is private when deriving a class.
2. While the members of a structure are public by default, the members of a class are
private by default
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
• Private – Such class members can’t be accessed outside the class in which they are
declared and are only accessible within the same class. Even child classes are
disabled to access private members of its parent class
• Protected – In addition to the class in which they are declared, the child classes can
access the protected members of its parent class
• Public – Class members declared as public can be accessed throughout the program
(code)
Question: Define the Copy Constructor used in C++ along with its general
function prototype. Also, explain the various scenarios in which it is called.
Answer: A member function that initializes an object using another object of the same class is
known as a copy constructor in C++. The Copy Constructor can also be made private. A call
to the Copy Constructor can happen in any of the following 4 scenarios, when:
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
When any of these operators precede a variable, the value of the variable is first modified and
then this modified value is used. However, when any of the two operators follow a variable,
the value is first used and then it is modified.
Therefore, in the code above j is set to the unmodified value of 5 and then i is incremented to
store 6.
Question: Take a look at the following two code examples for printing a vector:
Sample Code 1:
vector vec;
/* ... .. ... */
itr->print();
Sample Code 2:
vector vec;
/* ... .. ... */
itr->print();
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Question: Suppose you have the GPA (Grade Point Average) of n number of
students and you need to store and display it using C++. Can you write a
program that accomplishes this?
Answer:
#include
#include
int main()
int num;
float* ptr;
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
delete [] ptr;
return 0;
Question: What is a mutable storage class specifier? How can they be used?
Answer: A mutable storage class specifier is applied only on non-static and non-constant
member variable of the class. It is used for altering the constant class object's member by
declaring it. This can be done by using a storage class specifier.
Question: What are the differences between a shallow copy and a deep copy?
Answer: The differences between a shallow copy and a deep copy can be stated as under.
It allows memory dumping on a bit by bit basis from It allows the copy field, which is done by field from
one object to another. one object to another.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
Question: What is the main difference between the keyword struct and class?
Answer: The keyword struct is used for resembling public members by default, while the
keyword class is used for resembling private members by default.
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
#include <iostream>
using namespace std;
int my_var = 0;
int main(void) {
int my_var = 0;
::my_var = 1; // set global my_var to 1
my_var = 2; // set local my_var to 2
cout << ::my_var << ", " << my_var;
return 0;
}
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
Question: What is the ‘diamond problem’ that occurs with multiple inheritance
in C++? Explain using an example.
Answer: The diamond problem in C++ represents the inability of the programming language
to support hybrid inheritance using multiple and hierarchical inheritance.
Suppose we have a university with some faculty members and some graduate students. A
simple inheritance scheme in this scenario might have different types of people in different
roles. However, all of them inherit from the same Person class.
The Person class defines an abstract getRole() method that would then be overridden by its
subclasses in order to return the correct role type. Things up till this point is simple, however,
if we wish to model the role of a TA or Teaching Assistant then things get weird.
A Teaching Assistant is both a student and a faculty member. This will yield the diamond
problem, as illustrated in the figure below:
The problem generates an inheritance diagram resembling a diamond, hence the name,
diamond problem.
Which getRole() implementation should the Teaching Assistant inherit? Graduate Student or
the Faculty Member? A potential answer might be to have the Teaching Assistant class override
the getRole() method and return a newly-defined role, say TA.
However, such an answer would also be far from complete as it will hide the fact that a
Teaching Assistant is someone who is both a faculty member as well as a graduate student.
Conclusion
This article also covers some C++ Coding interview questions and answer to help you.
Tutorials are an excellent means of learning. Moreover, you might get access to additional
resources. Don’t miss out on them. Here are some important C++ tutorials to strengthen your
C++ knowledge.
There is much more required than having adequate knowledge of all the concepts that are likely
to be asked during a job interview. The demeanor and mannerism is something that is also paid
attention to by the interviewer. So, be clear and precise, don’t wander off the topic.
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials
Checkout Algo and DS book and crack the coding interview. 88+ Chapters with Most
frequently asked HR questions answered. Just at 249 rs. Click here to know more:
https://www.instamojo.com/aj_guides/ajs-guide-to-algorithm-and-data-structure-in/
For every interview, you are also expected to know the Data Structures and Algorithms basics.
To prepare for the same, take this Udemy course: Mastering Data Structures & Algorithms
using C and C++.
If you prefer reading physical books then here is the best C++ interview questions
book: Elements of Programming Interviews: The Insiders' Guide 2nd Edition.
Also, you need not beat around the bush. If you don’t know the answer, it’s ok. Simply accept
it, rather than ranting meaninglessly. Remember, it’s not only what you say, but also how you
say it!
All the very best!
Click here for more Interview tips and free placement materials, join the telegram channel:
https://t.me/FreePlacementMaterials