C++ Easy Interview Questions – part2

by Saurabh Manchanda on December 18, 2009


This post is a part of an ongoing series of C++ Interview Questions. This is Second part of the two parts of C++ interview questions(First part here). Each part consists of 5 questions along with detailed answers. What’s common in all the questions in this set is that the level of difficulty is Easy.

These questions were handpicked by the author, who happens to be an active member in one of the largest C++ communities in Orkut which boasts to have over 146,000 members.

6. What is polymorphism? Explain with an example?

Polymorphism is a way of providing a uniform interface with which different types of objects can interact and receive response based on the type itself. It can be said as “Same Work, Different Types”.

It is a common situation that objects of different types might have some common method, but depending on the type, the method needs to work differently.
For ex: All shapes have area, but each shape has a different way of calculating it.

In C++, polymorphism is implemented using

  1. Function Overloading
  2. Operator Overloading
  3. Function Overriding (using virtual functions)

In function overloading and operator overloading, the function to be used is decided at the compile-time itself. But the binding of the call to the function in function overriding is done at the run-time.

For Example:

#include<iostream>

float area(float a)
{
      std::cout<<"(Circle)";
      return 3.14*a*a;
}

float area(float a, float b)
{
      std::cout<<"(Rectangle)";
      return a*b;
}

int main()
{
      std::cout<<area(15)<<"\n";
      std::cout<<area(10,12.50);
}

Decision regarding the function to be called is based upon the functions’ signatures, which include the function name and the arguments(number and type).

7. What is the use of the ‘volatile’ keyword?

The volatile keyword is a type-qualifier and is a signal to the compiler telling it

not to do any optimizations on the variable with this qualification.
that the state of the variable can change anytime and the changes in the state could be due to some external control.

A variable should be declared volatile if its value can change unexpectedly.

For Example:

A variable shall be made volatile if

  1. It’s being shared between concurrent processes(threads).
  2. Its value can be changed by some interrupt service routine.

For further reading on volatile keyword, see here.

8. What is Encapsulation?

Encapsulation can be defined as a process of binding the attributes of an object with the associated methods, and way of implementing data hiding.

Using this feature of the object-oriented approach, one can implement a class such that its instances satisfy the definition of an object, i.e., each object has its attributes and properties, depending upon the type, and that no object, without specific permissions, can have access to the methods/attributes of a different type.

For Example:

In the following, an object of type Cat can’t bark. Similarly, a Dog can’t meow !!

#include<iostream>
#include<string>

using std::string;

class Cat
{
      string Name;
      int Age;

public:
      Cat(string _name, int _age) : Name(_name), Age(_age){}
      void meow(){ std::cout<<"Meow Meow!!";}
};

class Dog
{
      string Name;
      int Age;

public:
      Dog(string _name, int _age) : Name(_name), Age(_age){}
      void bark(){ std::cout<<"Ruff Ruff!!";}
};

9. What are dangling pointers?

A pointer that doesn’t point to a valid memory location, i.e., it points to a storage that is no longer allocated is a dangling pointer.

For Example:

int main()
{
      int *p = new int;
      /*Some Processing on p*/
      delete p;
      // Now p is a dangling pointer
}

It’s very useful to assign a deleted pointer to 0(zero) or NULL after delete-ing.

10. What is the Constructor Initialization List?

Whenever you create an object in C++, the constructor is invoked. It is responsible for the initialization of the data members of the class and for calling its parent class’ constructor. The constructor initialization list is used for initializing the non-static data members of a class. The list follows the constructor signature after a single colon.

For Example:

class myClass
{
      int x;
      string data;
public:
      myClass() : x(0), data() {}
      myClass( int a, string inp) : x(a), data(inp) {}
};

Here, the default constructor initializes x to 0 and makes data an empty string and the parametrised constructor initializes x to a and data to inp.

The order of members in the initialization list doesn’t matter because the initialization is always done in the order in which the data members are declared in the class. Similarly, the order of invocation of the parent class constructor depends upon the order in which parent classes are mentioned in the derivation list.

For Example :

class base1
{
      int i;
public:
      base1() : i(0){}
      base1(int x) : i(x){}
};

class base2
{
      int j;
public:
      base2() : j(0){}
      base2(int x) : j(x){}
};

class der: public base1, public base2
{
      string data;
public:
      der(): data(), base1(), base2() {}
      der(int a, int b, string inp) : base2(b), data(inp), base1(a) {}
};

So, in the above initialization list for the class der, the order of initialization would be :
Invoke base1′s constructor
Invoke base2′s constructor
Initialize “data


About the Author:  Saurabh Manchanda is a whiz C/C++ programmer. He is currently pursuing his Bachelors of Technology degree in Computer Science. His passions are programming, studying algorithms and solving puzzles. He loves reading books, technical textbooks and fiction novels. When not programming, he could be seen playing pool or cricket or watching movies, listening to songs or staring at his pet fishes! You can connect him at Orkut, Facebook, follow him on Twitter or contact him at saurabh29789@gmail.com If you are thankful for his posts, you may want to gift an item from his Amazon Wish List.


2,250 readers have already subscribed to email updates!

Enter your email address:

  • http://www.interviewmantra.net/2009/12/cpp-easy-interview-questions-part1.html C++ Easy Interview Questions – part1 | Interview Mantra

    [...] C++ Easy Interview Questions – part2 | Interview Mantra linked to this post on December 18, 2009 [...] series of C++ Interview Questions. This is Second part of the two parts of C++ interview questions(First part here). Each part consists of 5 questions along with detailed answers. What’s common in all the [...] [...]

  • Prasoon

    Add this too: “The codes where we must use ‘Constructor Initialization List’ “. :)

  • Saurabh Manchanda

    I’ll surely add that question in the upcoming set !

Previous post:

Next post: