Top 20 Most Frequently Asked C++ Interview Questions And Answers

Preparing for your job interview? Wondering if you would answer all the C++ interview questions right and ace the job? Well, wonder no more, you’ve landed at the right page. With curated research, I’ve compiled the top 20 C++ interview questions and answers asked in most of the Accenture placement and off-campus interviews. Learn up and succeed!

20 Most Frequently Asked C++ Interview Questions

Simple Difficulty C++ Interview Questions

Let’s begin with simple difficulty C++ interview questions and answers.

Q1. What is the skeleton of a C++ program?

A C++ program begins with the preprocessor directive, #include<iostream.h> followed by the main function, int main(). Within the curly braces that denote the beginning of the code block, the programming instructions (code) are written. The final line of the code ends with the return statement, which returns the value to the main function.

// Skeleton Of C++ Program 
#include <iostream> 
int main() 
{ 
cout<< "Hello World!"; 
return 0;
 }

Q2. What is a comment and what are its types in C++?

A comment in C++ is generally ignored by the compiler and is written only by the programmer only for explaining the source code. There are two ways to write a comment in C++

  • /*block comment*/
  • //single line comment

Q3. When a global and a local variable with the same name are written in the program, which is given priority?

The compiler gives higher precedence to the local variable.

Q4. Differentiate ++a and a++ with an example?

While both ++a and a++ are increment operators, ++a (pre-increment) increments the value before evaluating the statement and a++ (post-increment) increments after evaluating the statement.

Example: Let’s assume that a=1 and b=2

Then, the value of a will be first assigned to the variable then it will be incremented to 2.

Whereas in ++b, the value will be first incremented to 3 and then it will be assigned to the variable.

Q5. What is the role of the Friend function in C++?

The friend function is used to access the members of a class that are protected and under private mode.

Q6. Explain the role of getch() in C++

The getch() function is used to keep the output screen running and wait until user gives an input and obtain the output from the screen.

Q7. How does C++ offer data security?

With the access specifiers public, private, and protected, data can be protected. Public classes are accessible by all the members of the program. A private class can be accessed only by its own function. Protected classes are used in inheritances where accessibility is granted for a derived or subclass while access is denied for members outside the main class.

Q8. How are values passed in a function in C++?

There are two ways of passing values: call by value and call by reference.

In call by value, only the value of the parameter (variable) is being passed to the function argument so that changes implemented to it does not affect the original parameter.

In call by reference, we pass the address of the value, so that any change implemented in it affects the original parameter.

Q9. State the differences between class and structure.

The members of a class are by default viewed in private mode while that of the structure are viewed in public mode.

A class typically contains a group of objects and its related variables and functions, whereas, to perform a specific function, the user defines the structure data type.

Q10. How is malloc() different from calloc() in C++?

The malloc() function does not initialize the memory with zero while allocating a single block of memory, whereas, the calloc() function sets the memory to zero while allocating multiple memory blocks.

Q11. What do you mean by data abstraction in C++?

The process by which internal implementations are hidden while only the necessary details are revealed to the outside world is known as data abstraction. It can be implemented through the utilization of abstract classes and interfaces.

Q12. What do you mean by inheritance in C++?

The process by which the properties of one class can be defined or inherited by another class is called inheritance.

Q13. List the different types of storage classes available in C++

  • Auto
  • Register
  • Static
  • Extern
  • Mutable

Q14. How do you create a reference variable in C++?

A reference variable in C++ is created by adding the ampersand (&) after the last alphabet of the datatype.

Example:

Supposing int a= 20

Then, int& b= a, or int &b=a access the address of the integer variable a with the value 20.

Q15. How does a pointer differ from a reference variable?

  • A NULL value can be assigned to a pointer and not to a reference since addresses can never be associated with null.
  • Re-pointing is possible with pointers while re-initialization is not possible with reference variables
  • Pointers have unique address memory and stack storage, whereas, reference variables share a common memory with a unique location on the stack.

Advanced Difficulty C++ Interview Questions

Now let’s see advanced difficulty c++ interview questions and answers.

Q16. How can you solve the Diamond problem in C++ program?

When two of the superclasses in a class consisting of the same base class, it leads to ambiguities in the form of the Diamond problem. For instance, consider a class Company which has two superclasses, Project Heads and Team Members. Let the superclass be Person and its role is to collect the name and age of the members of the Project Heads and Team Members. In such a case, the constructor would be called twice causing the collection of two sets of data from the superclasses, thus, leading to ambiguities.

This diamond problem could be overcome by introducing a virtual keyword wherein the superclasses of Company are defined as virtual base classes to prevent two copies of Person in the Company class.

Q17. How is shallow copy different from that of deep copy in C++?

When an object is shallow copied, only the member field values are copied. So, for dynamically allotted memory, only the pointer would be copied and not the values stored by the memory. By default, constructors and assignment operators follow shallow copying.

On the flip side, when an object is deep copied both the member field values and the dynamically allotted memory that the pointer points to would be copied.

Read more about Copy Constructor in C++

Q18. Explain the role of RAII in C++?

Resource Acquisition is Initialisation (RAII) encapsulates every resource as a class, wherein the resource is acquired by the constructor to establish the class invariants. RAII is used by the Boost library to lock and unlock classes such as boost::lock_guard. 

Q19. What are the components of a translational unit in C++?

  • Source file contents
  • Directly or indirectly included files with their contents
  • Source codes that have been disregarded by the preprocessing directives

Q20. When is a copy constructor called?

A copy constructor is generally called to initialize the copy of an object. The compiler typically assigns a default copy when the user does not specify it, which copies all the members of the source object to the target object.

Leave a Comment