Menu Close

C++ interview questions

What is a class?

  • Represents the complete functionality of Object.
  • It is a blue print of object.
  • It consists attributes and actions.

What is protected access specifier?

  • If a class has protected member then it is accessible in the inherited class.
  • Private and protected members are not accessible outside.

What is Encapsulation?

  • The concept of binding data and the functions.
  • Encapsulation is the concept of protecting object information.
  • Encapsulation technically implemented by class in Object oriented programming.

What is abstraction?

  • Hiding unnecessary details of object and shows essential functionality.
  • It explains what object can do instead how it does it.

What is inheritance?

  • Inheritance is the process of acquiring the properties of the exiting class into the new class.
  • The existing class is called as base/parent class and the inherited class is called as derived/child class.

What are virtual functions?

  • Virtual functions are used with inheritance, they are called according to the type of object pointed or referred, not according to the type of pointer or reference.
  • In other words, virtual functions are resolved late, at runtime. Virtual keyword is used to make a function virtual.

What is this pointer?

  • ‘this’ pointer is passed as a hidden argument to all instance function.
  • It is available as a local variable within the body of all those instance functions.
  • ‘this’ pointer is a constant pointer that holds the memory address of the current object.

What are the Comments in C++?

  • Comments in C++ are simply a piece of source code ignored by the compiler.
  • They are only helpful for a programmer to add a description or additional information about their source code.
    • //single-line comment
    • /* block comment */

What is Mutable Storage class?

  • The variable of a constant class object’s member cannot be changed.
  • However, by declaring the variables as “mutable”, we can change the values of these variables.

What is a Reference Variable?

  • A reference variable is an alias name for the existing variable.
  • This means that both the variable name and the reference variable point to the same memory location.
  • Hence, whenever the variable is updated, the reference is updated too.

What is a Storage Class?

  • In the declaration of variable, we need to specify the storage class along with data type.
  • If we don’t specify the storage class, it uses default type depends on the location of variable.
  • Storage class determines the life or scope of symbols such as variable or functions.

List of storage classes?

  1. Auto
  2. Static
  3. Extern
  4. Register
  5. Mutable

What is a Static Variable?

  • A static variable is a local variable that retains its value across the function calls. Static variables are declared using the keyword “static”.
  • Numeric variables which are static have the default value as zero.

What is register storage class?

  • Register variable should be used whenever the variable is used repeatedly for example loop counters.
  • When a variable is declared with a “register” specifier, then the compiler gives CPU register for its storage to speed up the lookup of the variable.

Differentiate Class and Structure?

  • Structure: Structure is a user defined data type. It holds more than one element of different types. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name.
  • Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default all the members inside the class are private.

What is the use of ‘using’ declaration?

  • Using Declaration is used to refer a name from the namespace without the scope resolution operator.

What is a Default Constructor?

  • Default constructor is a constructor that either has no arguments or if there are any, then all of them are default arguments.

What is Function Overloading?

  • Function overloading is a technique that allows the programmer to have more than one function with the same name but different parameter list.
  • In other words, we overload the function with different arguments i.e. be it the type of arguments, number of arguments or the order of arguments.
  • Function overloading is never achieved on its return type.

What is Operator Overloading?

  • In operator overloading, an operator is overloaded, so that it can operate on the user-defined types as well with the operands of the standard data type.
  • But while doing this, the standard definition of that operator is kept intact.

What is Compile-time Polymorphism?

  • In compile-time polymorphism, we achieve many forms by overloading. Hence, we have an Operator overloading and function overloading.

What is Run-time Polymorphism?

  • This is the polymorphism for classes and objects.
  • General idea is that a base class can be inherited by several classes.
  • A base class pointer can point to its child class and a base class array can store different child class objects.

What is a friend function?

  • C++ class does not allow its private and protected members to be accessed outside the class.
  • But this rule can be violated by making use of the “Friend” function.
  • As the name itself suggests, friend function is an external function that is a friend of the class.
  • For friend function to access the private and protected methods of the class, we should have a prototype of the friend function with the keyword “friend” included inside the class.

What is a template?

  • Templates allow creating functions that are independent of data type (generic) and can take any data type as parameters and return value without having to overload the function with all the possible data types.
  • Templates nearly fulfill the functionality of a macro.

What is Exception Handling?

  • Exception is a runtime error.
  • When exception occurs, the program terminates abnormally.
  • Exception need to be handle to avoid the abnormal termination of program and informal information to End-user.

What is an Iterator class?

  • In C++ a container class is a collection of different objects.
  • If we need to traverse through this collection of objects, we cannot do it using simple index variables.
  • Hence, we have a special class in STL called an Iterator class which can be used to step through the contents of the container class.


CC++Core JavaServlets
Data structuresSpring BootAngularMicro Services