C++ Interview Questions

Interview Questions on C++

  1. What is the difference between c and c++?

Ans . C is a procedural programming language whereas C++ supports both procedural and Object-Oriented programming.

  1. What are the various features of c++?

Ans .

  • Inheritance
  • Data Abstraction
  • Encapsulation
  • Modularity
  • Polymorphism
  • Objects and Classes
  1. Define Object

Ans . Any entity that has state and behaviour is known as an object.  Objects represent instances of a class. Objects are basic run time entities in an object-oriented system.

  1. Define class

Ans . It is a blue print of an object that describe the behaviour if the object.

  1. Define Inheritance:

Ans . Inheritance is the process of forming a new class from an existing class or base class. The base class is also known as parent class or super class.

  1. Define Polymorphism

Ans . An ability to take more than one form is called as polymorphism.

C++, we use Function overloading and Function overriding to achieve polymorphism.

  1. Define Abstraction

Ans . Hiding internal details and showing functionality is known as abstraction.

In C++, we use abstract class and interface to achieve abstraction.

  1. Define Encapsulation

Ans . Binding or wrapping of code and data together into a single unit is called as encapsulation

  1. Define Modularity

Ans . The act of partitioning a complex program into simpler fragments called modules is called as modularity.

  1. What are the various types of access specifiers in C++ programming language?

Ans . There are three main types of access specifiers in C++ programming language:

  • private: Class members declared as private can be used only by member functions and friends of the class.
  • public: Class members declared as protected can be used by member functions and friends of the class. Additionally, they can be used by classes derived from the class.
  • Protected: Class members declared as public can be used by any function.
  1. How to create the object of the class?

Ans . Creating object or defining the object of a class:

The syntax of defining the object of a class is: –

Class_name object_name;

Example: Student stud1; // declare stud1 of type Student

  1. What is the general syntax for accessing data member of a class is?

Ans . Object_name.Data_member = value;

  1. Advantage of OOPs over Procedure-oriented programming language.

Ans . In Object oriented programming language following important feature are present which is not present in procedural language

  • Class
  • Object
  • Polymorphism
  • Abstraction
  • Encapsulation
  1. Which function we used in c++ to perform input and output operations.

Ans . cin() and cout()

cin() for the reading the data from the user.

cout() is used to print data on to the screen

  1. Explain C++ Constructor

Ans . Constructor is a special method which invoked automatically at the time of object creation.  It is used to initialize the data members of new object generally.  The constructor in C++ has the same name as that of class or structure.

  1. Write a Sample code to define constructor

Ans . #include <iostream>

using namespace std;

class Student

{

public:

Student ()

{

cout<<“Default Constructor is Invoked”<<endl;

}

};

int main(void)

{

Student s1; //creating an object of Student

Student s2;

return 0;

}

Output:

Default Constructor is Invoked

Default Constructor is Invoked

  1. What are the types of constructors?

Ans .

  • Default Constructor
  • Parameterized Constructors
  1. Explain Default Constructor

Ans . A constructor that accepts no parameter is called the Default Constructor. If you don’t declare a constructor or a destructor, the compiler makes one for you.

  1. Explain Parameterized Constructors:

Ans . A constructor that accepts parameters for its invocation is known as parameterized Constructors, also called as Regular Constructors.

  1. Explain destructor in c++:

Ans . A destructor is also a member function whose name is the same as the class name but is preceded by tilde (“~”). It is automatically by the compiler when an object is destroyed.