Few Interview Questions on C++

C++ is an extension of C and was designed to allow developers to use all of the existing features of C but provides a number of extensions to support object-oriented programming techniques in an intermediate-level programming language.

Due to its multifaceted features,C++ has ample applications.And it is must for job seekers to be aware of possible interview questions on C++.

Here we have gathered few interview questions on C++.


Q1. What is Shallow Copy and Deep Copy ?

A shallow copy of an object copies all the member field values.This works well, but may not work for fields that point to dynamically allocated memory.The pointer will be copied but the memory it points to will not be copied.

The default copy constructor and assignment operator make shallow copies.

A deep copy copies all fields and make copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator.

A class that requires deep copies needs:

  1. Constructor to make initial allocation.
  2. Destructor to deallocate memory.
  3. Copy Constructor to make copy of the dynamically allocated memory.
  4. Overloaded Assignment operator to make a copy of dynamically allocated memory.


Q2.What is object slicing in C++ ?

Slicing” is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information – some of it is “sliced” away. 

eg :- class A {int xyz};

class B : public A {int war;};

So here there are two data members are assigned to B that is xyz and war.

Then if you write this :

B b;

A a = b;

Then the information in b about member war is lost in a .


Q3.What is Virtual function?

A virtual function a member function which is declared within base class and is re-defined (Overriden) by derived class.When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

  • Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
  • They are mainly used to achieve Runtime polymorphism.
  • Functions are declared with a virtual keyword in base class.
  • The resolving of function call is done at Run-time.
Rules for Virtual Functions
  1. They must be declared in public section of class.
  2. Virtual functions cannot be static and also cannot be a friend function of another class.
  3. Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism.
  4. The prototype of virtual functions should be same in base as well as derived class.
  5. They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the virtual function), in that case base class version of function is used.
  6. A class may have virtual destructor but it cannot have a virtual constructor.


Q4.Explain virtual tables?

 Virtual table for a class is a static array that contains one entry for each virtual function that can be called by the objects of that class. Runtime Polymorphism is C++ is achived with virtual function. Hence in order to implement the runtime behaviour C++ compiler inserts Virtual Table for every class having virtual function or class inherited from the class that has virtual function.


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

The C++ standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That’s why even empty classes must have a size of (at least) 1.

The C++ standard also states that no two objects shall have the same memory addresses. There are several good reasons for this.

  1. To guarantee that new will always return a pointer to a distinct memory address.
  2. To avoid some divisions by zero. For instance, pointer arithmetics (many of which done automatically by the compiler) involve dividing by sizeof(T).


Q6.What are the differences between C++ and java ?

  1. Design Goal : Java was created initially as an interpreter for printing systems and support network computing. It relies on a virtual machine to be secure and highly portable. While C++ was designed for system and application programming , extending the C programming language.
  2. Execution: At compilation time Java Source code converts into byte code .The interpreter execute this byte code at run time and gives output. Java is interpreted for the most part and hence platform independent. While C++ is run and compiled using compiler which converts source code into machine level languages so c++ is platform dependent.
  3. Memory: Java is a memory-safe language, whereas C++ is not. This means that errors in Java programs are detected in defined ways – for example, attempting a bad cast or indexing an array out of bounds results in an exception. Similar errors in C++ lead to undefined behaviour, where instead of raising an exception or crashing, your program might keep running and crash later or even give the wrong answer or behaviour.
  4. Libraries: Java contains standard libraries for solving specific tasks while C++ rely on non-standard third party libraries.
  5. Inheritance: Java does not support multiple inheritance, so no virtual functions are there while C++ supports multiple inheritance, hence virtual functions.
  6. Memory management: Java has an automatic system for allocating and freeing memory (garbage collection). While C++ uses memory allocation and de-allocation functions so that constructor and destructor.
  7. Operator overloading: Java does not support operator overloading while C++ supports operator overloading.
  8. String Handling : Java has a string class as part of the java.lang.package while null-terminated array of characters are used in C and C++.
  9. typedef: Java does not support typedef or #define while C++ supports typedef and #define.
  10. struct, union and pointer: Java does not support the struct, union and pointer data type while C++ supports all these data types.
  11. Calling a method: Java supports call by value only. There is no call by reference in java while C++ supports both call by value and call by reference.
  12. Thread : Java has built in support for threads while C++ does not have built in support for threads. It relies on third-party libraries for thread support.


Q7.What is the difference between preprocessor directive #include”file.h” and #include <file.h> ?

#include<file.h> is used to attach system defined header file.

#include “file.h ” is used to attach user defined header file.

In case of <…> , it searches the specified header file only in default “include” directory.

Where as in case of “ ” , it first searches the local directory .If the header file is not found in the local directory, it searches the default “include” directory.


Q8.What is the difference between class and struct in C++ ?

In C++,Struct is collection of different data type means we can store different data in a structure while a class is collection of datatype and function as well.

We can say in C++ class = Struct in C++ with functions.

By default, all members in struct are public while in class it is private.


Q9.Discuss the relationship between parent and child classes in C++ ?

To implement inheritance (an Object Oriented Concept) in C++, we use parent class and child class. As in Inheritance one object can acquire the property of another object so that it can inherits behaviour of other objects and also add its own behaviour.

To implement this, in C++ one class can inherit another class and can add own data and functions to new class.

The class which have been inherited is called parent class or  super class ,and new class which is being derived is called child class or sub class.


Q10.Compare and contrast inline function and macro in C++ ?

Macro is an instruction which expands at the time of its invocation. Functions can also be defined, like macros. Similarly, the inline functions also expand at the point of its invocation. One primary difference between inline and macro function is that the inline functions are expanded during compilation, and the macros are expanded when the program is processed by the preprocessor.

  1. Inline functions are parsed by the compiler. Macros are expanded by the preprocessor.
  2. Syntax

inline return_type funct_name ( parameters ){ . . . }

#define macro_name char_sequence

  1. Inline function can be defined inside or outside the class. Macros are always defined at the start of the program.
  2. The compiler may not inline and expand all the functions while Macros are always expanded.
  3. Debugging is easy for an inline function as error checking is done during compilation. Debugging becomes difficult for macros as error checking does not occur during compilation.


Q11.What are the advantage of OOP over non-OOP programming ?

Procedural programming are structural programming languages which executed the code sequentially depending on the instruction received through some subroutine and function calls. Languages like C,cobol are procedural language.

OOP brings the concept of class, which is a collection of data and methods. The concept of class and object brings the dynamisms within a code and most importantly make the code reusable unlike procedural language. Languages like java and C++ uses the concept of OOP.

The major advantages of using OOP are:-

1.Concept of Inheritance which is useful in extending the properties of base class into child class along with its existing methods which increases code reusability and minimises code length.

2.Concept of Encapsulation which is a data hiding property within a class so that a particular data member can remain private.

3.Polymorphism concept using virtual methods which is useful in overriding class properties and methods.

4.Abstract class concept which is just a placeholder for a class and object can not be created out of it and the use of it comes in task and function call.

5.Introduces shallow copy,deep copy and object copy concept which are missing in procedural language.

6.Introduces multiple Inheritance and composition concept where a single child class can inherit the properties of more than one class at the same time which helps in tremendous reduction of code and minimises debugging effort within a code.

7.Introduces concept of observer pattern so that if a particular object within a class got modified then extended data member which is dependent on that object gets updated without any further modification of the code.

8.The concept of OOP related features brings the idea of reusability, dynamic nature, inter communication within the blocks, multithreading etc. which are missing in procedural language.