C++ Interview Questions

Q1. What is OOPS?

OOPS stands for Object Oriented Programming System.

Q2. What is a class?

  • A Class acts as a description or blueprint for objects.
  • The properties of class are called attributes or fields.
  • The behaviors of an object of a class are known as operations, and are defined using functions.
  • Fields and methods(functions) are collectively called member of class.
  • We declare a class by using class keyword.

Q3. What is Object?

  • The process of creating objects from a class is called class instantiation.
  • An object is an instance of class.
  • Object contains data (variable) and behaviors (functions) declared in the class.

Q4. How many access specifiers in C++?

There are three access specifiers: private, protected, or public. These specifiers modify the access rights for the members.

  • Private members of a class are accessible only within the same class (or from their "friends").
  • Protected members are accessible only within the same class (or from their "friends"), but also from members of their derived classes.
  • Public members are accessible from anywhere where the object of that class is visible.

Q5. How to create an object of class ?

The process of creating objects are usually in two ways:

Let's assume class name is Test

  1. Value Type:
    Test ob;
  2. Pointer Type:
    Test *ob = new Test();

Q6. How to select(call) a member ?

Calling or selection of member depends upon type of object.
For value type variable of object we use dot ( . ) operator.
For pointer ( * ) type variable of object we use arrow ( -> ) operator.
For reference ( & ) type variable of object we use dot ( . ) operator.

Q7. What are OOP concepts?

OOP consist the following 4 concepts:

  1. Encapsulation: Encapsulation is a mechanism which binds the data and associated operations together and thus hide the data from outside world. Encapsulation is also known as data hiding. In C++, It is achieved using the access specifiers i.e. public, private and protected .
  2. Abstraction: Abstraction is used to hide the internal implementations and show only the necessary details to the outer world. Data abstraction is implemented using interfaces and abstract classes in C++.
  3. Polymorphism: means many-form.In OOPs, polymorphism deals with behavior(method or function) part of the object.
    If an object is able to perform single functionality in multiple ways, this is called polymorphic behavior of that object.
  4. Inheritance: Inheritance is used to inherit the property of one class into another class. It facilitates you to define one class in term of another class.

Q8. What is a Constructor?

C++ provides a special member function called constructor . A constructor initializes an instance(object) of its class.

  • It is special because its name same as the class.
  • Constructor does'nt have return type,even void.
  • Its invoked automatically when the object is created.
  • Like other C++ functions, constructor can also have default arguments.
  • Constructor can be overload.
  • Constructors may have any accessibility, public, protected or private.
  • Note If you don't define any constructors, the compiler will generate a default constructor that takes no parameters; you can override this behavior by declaring a default constructor as deleted.

Q9. What is destructor?

Destructor is used to delete any extra resources allocated by the object.

  • A destructor's name will same as class name preceding by tilde (~) sign.
  • Destructor does not have any return type like constructor and never takes any parameter.
  • Destructor cannot be overload.

Q10. What is copy constructor?

  • Its a special type of constructor which takes an object of same class as argument.
  • It is used to copy values of data members of one object into other object.
  • We use reference ( & )type of argument to receive the object in copy constructor.

Q11. What is an inline function?

When we prefix a keyword inline before any normal function, the function becomes inline function. These types of functions are considered as macros and they execute faster than the normal functions.

Q12. What is pure virtual function in C++?

It is a virtual function with no function body and assigned with a value zero is known as pure virtual function.
Example: void show () = 0;

Q13. What is an abstract class in C++?

The class with at least one pure virtual function is called abstract class. One cannot instantiate abstract class. Objects are created out of its derived classes only.

Q14. What is a reference variable in C++?

This is another name for the variable. Both variable and reference variable will point to the same memory location. Hence changing the value of variable of reference variable will change the variable value.

Q15. What is role of static keyword on class member variable?

Static keyword is used for class member variables to share the common memory. That means, when objects are created using the class which has static member variable, then all the object’s static member variable will address same memory location. Hence this member variable will have same value in all the objects.

Q16. Explain the static member function.

A static member function can be called using the class name since static member exists before class objects comes into existence. It can access only static members of the class.

Q17. What is function overloading?

Define the same name of function more than one time with different argument is called function overloading.

Q18. Name the default standard streams in C++.

There are four default standard streams in C++: cin, cout, cerr and clog.

Q19. Which access specifier/s can help to achieve data hiding in C++?

Private & Protected are the two access specifiers used to achieve data hiding.

Q20. What is the use of 'delete' operator?

delete operator is used to release the memory of the object which is allocated using new operator.

Q21. Which operator is used in C++ to allocate dynamic memory?

new operator is used for dynamic memory allocation to the objects.

Q22. Can we use malloc () function of C language to dynamically allocate memory in C++?

One can use malloc also to allocate dynamic memory, since C++ supports all the functionalities of C.

Q23. What is a friend function?

A friend function is a function that is not a member of a class but has access to the class's private and protected members.

A friend function declared inside the class but define outside of the class.

Q24. What is the difference between structure and class in C++?

The main difference between structure and class is: in structure members are public by default whereas in class, members are private by default.

Q25. What is the scope resolution operator ( :: )?

The scope resolution operator is used to

  • Determine the scope of global variables.
  • Links the function definition to the class if the function is defined outside the class.

Q26. What is a namespace?

A namespace is the logical portion of the code which can be used to resolve the name conflict among the identifiers by placing them under different name space.

Q27. What is a class template?

A template class is a generic class. The keyword template is used to define a class template.

Q28. What is a container class?

A class with at least one member variable of another class type in it is called container class. In other words, a container class is the one which contains at least one object of another class as its member variable.

Q29. Can we resize the allocated memory which was allocated using ‘new’ operator?

This operator does not allow to resize the allocated memory using new operator. This operator is used for allocating new memory to the objects.

Q30. Who developed C++ programming language?

Bjarne Stroustrup designed C++ programming language.

Q31. Which operator can be used to determine the size of a data type/class or variable/object?

sizeof is used to find the size of the variable or object.

Q32. What is the difference between delete and delete []?

delete will release the memory allocated to single object whereas delete [] will release the memory allocated to array of objects (all the memory assigned to the array of objects will be released).

Q33. What is function overriding?

If a function is declared in base class as virtual and is defined in the derived class with same name and signature, then it is called as function overriding.

Q34. What are the stream class use to read/write from/to file?

ifstream and ofstream is used to perform read/write operation on files.

Q35. Which function is used to move the stream pointer for the purpose of reading/writing data from stream?

seekg () is used for reading. Seekp () is used for writing.

Q36. Are class functions taken into consideration as part of the object size?

Only the class members (variables) are considered to calculate the object size.

Q37. Can we create and empty class? What would be the size of such object.

Yes, we can create an empty class.
Size of the object of the empty class will be 1 byte.

Q38. What is the difference b/w include preprocessor using < and " ?
#include<filename> and #include "filename"?

The difference is in the location where the preprocessor searches for the included file.
For #include "filename" the preprocessor searches in the same directory as the file containing the directive. This method is normally used to include programmer-defined header files.
For #include < filename > the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

Q39. What is the use of return value (int) of main function in C++?

C++'s main function returns an int... that int goes to the program which executed it (the parent process, if you will) as an exit status code.

Specifically, on most operating systems, a 0 exit code signifies a normal run (no real errors), and non-zero means there was a problem and th

Q40. What are reserved words / Keywords?

Reserved words are words that are part of the standard C++ language library.

This means that reserved words have special meaning and therefore cannot be

Q41. What is variable initialization and why is it important?

This refers to the process wherein a variable is assigned an initial value before it is used in the program.

Without initialization, a variable would have an unknown value, which can lead to unpredictable outputs when used in computations or other operations.

Q42. What is a virtual destructor?

As we know, in C++ a destructor is generally used to deallocate memory and do some other cleanup for a class object and it’s class members whenever an object is destroyed.

In order to define a virtual destructor add the keyword virtual before the tilde symbol.

Need of Virtual Destructor? :

Virtual destructor is to destruct the resources in a proper order, when you delete a base class pointer pointing to derived class object.

Here are some points:

  • The destructor of base class can be virtual.
  • Whenever upcasting is done, destructor of base class must be declared virtual for proper destruction of the object.

Q43. What is the order of objects destroyed in the memory?

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

Q44. What is the difference between macro and inline?

Inline follows strict parameter type checking, macros do not.
Macros are always expanded by preprocessor, whereas compiler may or may not replace the inline definitions.

Q45. Explain function template?

Function template provides a means to write generic functions for different data types such as integer, long, float or user defined objects.

Q46.What is the advantages of inheritance?

Code reusability

Q47. What are virtual functions?

  • A virtual function is a function in base class that is declared using the keyword virtual.
  • The virtual functions can be re-define in derived class, this practice is called function overriding.
  • They cannot be static members.
  • A virtual function can be a friend of another class.

Q48. What is upcasting?

Upcasting is using the Super class's reference or pointer to refer to a Sub class's object

Q49. What is object slicing?

  • In C++, a derived class object can be assigned to a base class object, but the other way is not possible..
  • Object slicing happens when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object..

Q50. What is 'this' pointer?

  • The this pointer is an implicit parameter to all member functions.
  • this pointer represents the current or invoking object of class.
  • this pointer is not accessible in static member function.
  • Friend functions do not have this pointer, because friends are not members of class.
  • Only member functions/non-static function have this pointer.

Get it on Google Play

(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017,, All rights reserved