Important C++ Interview Questions & Programs with Answers | FACE Prep

In this article, we will be discussing some of the most important C++ interview questions and answers for freshers. A quick recap before the interview will get you prepared for most of the questions.

To receive Placement preparation tips and Placement related updates, come join the largest placement Community on FACE’s Telegram and WhatsApp, and take your placement preparation to the next level.
 
Join here:
Telegram: https://t.me/placements2020
WhatsApp: http://bit.ly/2MgoYMR

C++ Interview Questions and Answers

1) What is the difference between C and C++?

CC++
C is a procedure-oriented programming language.C++ is a partial Object-Oriented programming language
C does not allow functions to be defined inside structuresIn C++, functions can be used inside a structure
C contains 32 keywordsC++ contains 52 keywords
Data in C as we know is subject to security infiltrations and hence data is less securedC++ is particularly oriented towards protecting data. This is done using modifiers for class members. Hence data is more secured

 

2) Why is C++ called a partial object-oriented programming language?

C++ can be called as a partial object-oriented language due to the following reasons.

  • For any language to be completely object-oriented,  no method should exist without an object. But in case of C++, we can write a valid C++ program without usage of a class or objects as the main() function need not be part of any class.
  • In C++, globally declared variables can be accessed from anywhere and this violates the law of encapsulation (data protection).
  • Also, C++ violates the law of inheritance with its principle of multiple-Inheritance.

3) What are the advantages of C++?

  • C++ is a highly portable language, i.e. the software developed using C++ language can run on any platform
  • C++ is a partial object-oriented programming language and hence includes concepts such as classes, objects, inheritance, polymorphism, abstraction, data hiding
  • C++ contains a rich function library
  • C++ is a powerful, efficient and fast language and hence is the popular language for competitive coding

4) Which is an extra data type available in C++ when compared to C?

  • Name: Wide character
  • Keyword: wchar_t
  • Size: 2 or 4 bytes
  • Purpose: A wide character is a character datatype that generally has a size greater than the traditional 8-bit character data type

5) What are the OOPs concepts available in C++?

  • Object
  • Class
  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

Object
Any entity which has its own characteristics and behaviors is called as an object. 
Example: Car is an object which has color, size as its characteristics and driving, applying brakes as its behaviors.

Class
Class is like a blueprint of an object. It is used for creating objects and initialize member variables.

Inheritance
Inheritance allows one class to acquire all properties of another class. The class which acquire all properties is called as child or derived class. The class using which the properties can be acquired is called as parent or base class. The main usage of Inheritance is, we can reuse the code from the existing parent class.

Encapsulation
It is the process of combining data and functions. Using which direct access of data is not possible. The access is given only using the functions of the respective class.

Abstraction
Abstraction is the process of hiding the implementation of any application from outside world. 

Polymorphism
Polymorphism refers to many forms, which enables an application to change its behavior during compile and run time.

6) What is a class?

A class is an expanded concept of a data structure. Instead of holding only data, it can hold both data and functions.

7) What is Data Abstraction in C++?

Data Abstraction is a technique of providing only essential information of any application to the user and hiding its implementation, thus making the data more secure.

Example: Consider WhatsApp, we as users can only have a look at the elements useful for us and operate using the same but we never know what happens in the background.

8) What is a default constructor in C++?

Constructor that doesn’t take any arguments is called default constructor, i.e it has no parameters. Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.

Example:
#include<iostream>
using namespace std;
class default_constructor
{
   public:
   default_constructor()
   {
       cout << “This is a default constructor”;
   }
};
int main()
{
   default_constructor d;
}

9) List all types of inheritance supported in C++

  • Single
  • Multilevel
  • Multiple
  • Hierarchical
  • Hybrid

10) What is the use of namespace in C++

Consider you are writing a code that has a function named as xyz() and there is another library available which is also having the same function named xyz(). Now the compiler will be confused with the function you are referring to within your code.

Namespace has been designed to overcome this difficulty. Using namespace, you can define the context in which the name is defined. C++ consists of a standard namespace, i.e., std which contains inbuilt classes and functions. So, by using the statement “using namespace std;”, the compiler includes the namespace “std” in our program.

If you don’t include this line, then a program to print “Hello World” should be written as shown below.

cpp interview questions and answers

11) What are the various C++ access specifiers and mention their types?

Access specifiers are used to define the accessibility of the class members. There are three types of access specifiers in C++ namely,

  • Public – can be accessed by any other class
  • Private – cannot be accessed outside the class in which they are declared
  • Protected – can be accessed by other class which inherits the class in which they are declared

12) What is the difference between new() and malloc()?

new()malloc()
It is an operatorIt is a function
No need to specify the memory size when using new() operatorYou need to specify the memory size that has to be created
new() operator allocates memory and calls the constructor for object initializationmalloc() function doesn’t call constructor when it allocates memory

13) What is a destructor and can there be more than one destructor in a class?

Destructors are used to de-allocate the memory that has been allocated for an object by the constructor. It has the same name as of the class name with a tilde symbol in front of the class name.

 Syntax: ~class_name() { }

Also, there should be only one destructor in a class even if there are more than one constructors in a single class with no parameters and no return type.

14) What are the types of Overloading?

cpp interview questions

Function overloading is a type of compile-time polymorphism where we have more than one function inside a class with same name but with different numbers and types of arguments. Such functions have to be invoked depending on the number of arguments and the type of arguments in the argument list.

Operator overloading allows an operator to work in multiple forms. 

Example: The arithmetic operator (+), is used to add two numbers and also used to concatenate two strings.

15) What is Pure Virtual Function?

A pure virtual function is a virtual function which does not contain any definition. The normal virtual function is preceded with a keyword virtual. Whereas the pure virtual function is preceded with the keyword ‘virtual’ and ended with the value 0.

Example: virtual void add() = 0;

16) What is an abstract class in C++?

  1. Consider a class with four functions. But, implementation is available only for two functions. In this case, that class can be an abstract class
  2. A class with at least one pure virtual function is called as an abstract class
  3. Abstract class can also be used as a base class
  4. We cannot instantiate an abstract class

17) Which access specifier/s can help to achieve data hiding in C++?

Private & Protected.

18) What do you mean by scope resolution operator?

Scope resolution operator is represented as ‘::’.

This operator can be used to associate the function definition to a particular class, when a class member is defined outside the class?

19) Can we use malloc() function of C language to allocate dynamic memory in C++?

Yes, as C is the subset of C++, we can all the functions of C in C++ too.

20) What is a Copy Constructor?

A copy constructor is the constructor which takes same class object as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class.

A copy constructor has the following general function prototype: ClassName (const ClassName &old_obj){ }

21) What is the difference between the keywords struct and class in C++?

By default the members of struct are public and by default the members of the class are private.

22) What is the difference between C struct and C++ struct?

C struct

C++ struct

struct keyword is necessary to define structure variable

struct keyword is not necessary to define structure variable

Data member cannot be intialized directly inside structure

Data member can be intialized directly inside the structure

Data member cannot be static

Data member can be static

Size of an empty structure is 0 byte

Size of an empty structure is 1 byte

Member functions are not allowed in structure

Member functions are allowed in structure

 

23) Can a program be compiled without main() function in C++?

Yes, it can be compiled. But cannot be executed, as the execution requires main() function definition.

24) Does an abstract class in C++ need to hold all pure virtual functions?

No, a class having at least one pure virtual function is abstract class too.

25) Are exceptions and errors the same?

No. Errors occur because of any mistakes in the syntax of the program. i.e. errors occur during compile time. Whereas exceptions during run time of the program.

Example: If you forget to give semicolon at the end of an assignment statement, then it is an error. If you give 6 input values to an array which is declared only to store 5 elements, then that is an exception.

26) What is the order of objects destroyed in the memory?

The objects are destroyed in the reverse order of their creation.

27) What is reference variable in C++?

A reference variable is just like a pointer with few differences. It is declared using ‘&’ symbol. In other words, reference variable is another name for an already existing variable.

28) What is a static member in C++?

Static is a keyword in C++, used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.

29) What is friend function in C++?

It is a function that is defined outside a particular class and preceded with the keyword ‘friend’. Even though it is defined outside the class, it can access the private and protected data of that class.

Syntax:

class class_name   
{   
    friend data_type function_name(argument/s);           
};

30) How can a C function be called in C++ program?

Using the keyword ‘extern’ this can be done as shown below,

//C code
void func(int i)
{
//statements
}

void print(int i)
{
//statements
}
//C++ code
extern "C"{
void func(int i);
void print(int i);
}
void myfunc(int i)
{
   func(i);
   print(i);
}

 

31)  Predict the output of the following program.

#include <iostream>
int main(int argc, const char * argv[])
{
    int a[] = {1, 2, 3, 4, 5, 6};
    std::cout << (1 + 3)[a] - a[0] + (a + 1)[2];
}

Output: 8
Explanation:

According to the precedence rule, the brackets gets executed first.
(1 + 3)[a] will become (4)[a]
According to pointer arithmetic (4)[a] is same as a[4] and (a + 1)[2] is same as a[3]
So, the print statement becomes, a[4] – a[0] + a[3] = 5 – 1 + 4 = 8.

32) Is it possible to have Virtual Constructor? If yes, how? If not, Why?

There is nothing like Virtual Constructor in C++. The Constructor can’t be virtual as the constructor is a code which is responsible for creating an object.

33) What is virtual function?

When derived class overrides the base class method by redefining the same function, then if the user wants to access the redefined method from derived class through a pointer from base class object, then you must define this function in base class as virtual function.

34) What is Function overriding?

Defining a function in the child class with the same name and same numbers and types of arguments as that of the parent class.

35) Types of Polymorphism

polymorphism cpp interview questios

Compile time Polymorphism is achieved using operator or function overloading. Run time Polymorphism is achieved using function overriding.

36) How an exception can be handled in C++?

Exception occurs during run time of a program. This can be handled using try, throw, catch block.

  • try: A piece of code which can arise exception is written inside the try block.
  • throw: This block is used to throw an exception. But, doesn’t handle itself.
  • catch: Catch block defines an action to be taken, when an exception occurs.

Consider an example code which throws an exception if the input is negative value.

#include<stdio.h>
using namespace std;
int main()
{
int x;
scanf("%d", &x);
try
{
if (x < 0)
{
throw x;
}
}
catch (int x )
{
cout << "Exception Caught";
}
if(x > 0)
cout << "There is no exception";
return 0;
}