top of page
  • Facebook
  • Twitter
  • Instagram

Top C-plus-plus Interview Questions

Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.

Question

29

Explanation

What do you mean by virtual functions in C++?

A virtual function is a member function that is declared as virtual within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.

Example:
#include<iostream.h>
class shape()
{public:
virtual void area()
{
cout<<”dummy”;
}

class circle:public shape
{
int r;
public:
circle(int n)
{ r=n;
}
void area()
{float a=3.14*r*r;
cout<<”area is”<<a;
}
};

main()
{ shape *p;
p=new circle(5);
p->area();
}

Question

30

Explanation

Explain "this" pointer in C++.

> Every object has a special pointer "this" which points to the object itself.
> This pointer is accessible to all members of the class but not to any static members of the class.
> Can be used to find the address of the object in which the function is a member.
> Friend functions do not have a this pointer, because friends are not members of a class.

EXAMPLE

class student
{ int rollno;
char name[20];
public:
student(int rollno,char name[])
{ this->rollno=rollno;
strcpy(this->name,name);
}
void showInfo()
{ cout<<”Roll No is “<<rollno<<endl;
cout<<”Name is “<<name<<endl;
}
student* getAddress() // function returning Address of object
{ return this;
}
student getObject()// function returning object
{ return *this;
}
};
void main()
{student a(4117,”Amit”);
student *p=a.getAddress();
student b=a.getObject();
}

Question

31

Explanation

Differentiate between run time polymorphism and compile time polymorphism.

1. In Compile time Polymorphism, call is resolved by the compiler. In Run time Polymorphism, call is not resolved by the compiler .
2. Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature and different return type. Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass.
3. Overloading is achieved by function overloading and operator overloading. While, overriding is achieved by virtual functions and pointers.
4. Compile time polymorphism is less flexible as all things execute at compile time.Run time polymorphism is more flexible as all things execute at run time.

Question

32

Explanation

What are constructors. Write characterists of constructors.

Constructors Are special member functions of a class which gets automatically invoked when ever an object of its class is created.
Characteristics
> They have the same name of the class name.
> They doesn’t have any return type not even void.
> They are used for initialization of the objects.
> Multiple constructors can be defined in a class
> Constructors can have default arguments.
> C++ automatically adds an empty constructor without arguments if constructor is not defined
> Constructors are implicitly called when objects are created but they can also be explicitly called if required.

There are three types of constructors:
1.Default constructor: while declaring constructor we can assign some default value to its argument.so that same constructor will be used to initialize different ways of object.
2. Copy Costructor: It is used to initialize an object by using data of another object of same type. The copy constructor is automatically called when an object is assigned to it.
3. Parameterised constructor: while creating an object we can pass some data. This data is passed on to constructor as an argument where it is used for initialization of object.

bottom of page