C++ Interview Questions and Answers with example part 1

C++ Interview Questions and Answers with examples. this c++ question list covers all the fundamental topics and categories of questions according to the topics.

Que 1: Can a constructor throw an exception? How to handle the errors when the constructor throws an exception?

Ans: The constructor never throws an error.

Que 2: What is the default constructor?

Ans: A Constructor in C++ without arguments or with the arguments has default values.

Que 3: What is a copy constructor?

Ans: Constructor which initializes its object member variables with another object of the same class. If you do not implement a constructor in your class then the compiler implements one for you.

Copy Constructor example:

(a) MyClass Obj1(10); // calling MyClass constructor
(b) MyClass Obj2(Obj1); // calling MyClass  copy constructor
(c) MyClass Obj2 = Obj1;// calling MyClass  copy constructor

Que 4:  Difference between a copy constructor and an overloaded assignment operator?

Ans: The copy constructor constructs a new object by using the content of the argument object and
the overloaded assignment operator is assigns the contents of an existing object to another existing object of the same class.

Que 5: When are copy constructors called in C++?

Ans: Copy constructors are called in the following cases:
(a) Copy constructor is called when a function returns an object of that class by value.
(b) Copy constructor is called when the object of that class is passed by value as an argument to a function.
(c) Copy constructor is called when you construct an object based on another object of the same class.
(d) Copy constructor is called When the compiler generates a temporary object.

Que 6: Is copy constructor accept an object of the same class as a parameter, instead of a reference
of the object in C++?

Ans: No.

It is defined in the definition of the copy constructor itself. It should generate an error if
a programmer specifies a copy constructor with a first argument that is an object and not a
reference.

Que 7: What is Virtual Destructor in C++?

ANS: C++ contains Virtual Destructor. A destructor in C++ can be virtual as it is possible at runtime
depending on the type of object the caller is calling to, a proper destructor will be called.

Que 8: Is it possible to have a Virtual Constructor? If yes, how? If not, Why not possible in C++?

Ans: C++ Does not have a Virtual Constructor. The Constructor can’t be virtual as the constructor is a code that is responsible for creating an instance of a class and it can’t be delegated to any other object using a virtual keyword.

Que 9: What is constructor or ctor in C++?

Ans: Constructor creates an object and initializes it. It also creates a vtable for virtual functions. It is
different from other methods in a class.

Que 10: What is a conversion constructor in C++?

Ans: constructor with a single argument makes that constructor as conversion ctor and it can be
used for type conversion.

Conversion constructor example:

class Boo
{
public:
Boo( int i );
};
Boo BooObject = 10 ; // assigning int 10 Boo object

Que 11: What is a conversion operator in C++?

Ans: class can have a public method for specific data type conversions.
for example:

class MyClass
{
double value;
public:
Boo(int i )
operator double()
{
return value;
}
};
MyClass MyClassObject;
double i = MyClassObject; // assigning object to variable i of type double. now the conversion operator gets called to assign the value.

Que 12: How to handle a constructor that fails in C++

Ans: By using exception handling, throw an exception. Constructors don’t have a return type, so it’s not possible to use the return keyword. The best way to signal constructor failure is to throw an exception.

Que 13: How to handle a destructor that fails in C++

Ans: Write a message to a log-_le. But do not throw an exception.

The C++ rule is that you must never throw an exception from a destructor that is being called during the “stack unwinding” process of another exception.

For example: if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding. During stack unwinding, all the local objects in all those stack frames are destructed.

If one of those destructors throws an exception (say it throws a Bar object), the C++ runtime system is in a no-win situation: should it ignore the Bar and end up in the } catch (Foo e) { where it was originally headed? Should it ignore the Foo and look for a }
catch (Bare) { handler? There is no good answer: either choice loses information.

So the C++ language guarantees that it will call terminate() at this point, and terminate() kills the process. Bang you’re dead.

Q 14: What is Virtual Destructor in C++?

Ans: Using virtual destructors, you can destroy objects without knowing their type – the correct
destructor for the object is invoked using the virtual function mechanism. Note that destructors
can also be declared as pure virtual functions for abstract classes.

if someone will derive from your class, and if someone will say “newly Derived”, where “Derived” is derived from your class, and if someone will say delete p, where the actual object’s type is “Derived” but the pointer p’s
type is your class.

Que 15: Can a copy constructor accept an object of the same class as a parameter, instead of a reference of the object in C++

Ans: No. It is specified in the definition of the copy constructor itself. It should generate an error if
a programmer specifies a copy constructor with a first argument that is an object and not a
reference.

Que 16: What is the order that local objects are destructed in C++?

Ans: In the reverse order: First constructed, Last destructed.
In the following example, b’s destructor will be executed first, then a’s destructor:

void userCode()
{
Fred a;
Fred b;
...
}

Que 17: What’s the order that objects in an array are destructed in C++?

Ans: In reverse order of construction: First constructed, last destructed.
In the following example, the order for destructors will be a[9], a[8], …, a[1], a[0]:

void userCode()
{
Fred a[10];
...
}

Que 18: Can I overload the destructor for my class in C++

Ans: No.

You can have only one destructor for a class Fred. It’s always called Fred::~Fred(). It never takes
any parameters and it never returns anything.

You can not pass parameters to the destructor anyway since you never explicitly call a destructor.

Que 19: Should I explicitly call a destructor on a local variable in C++

Ans: No!

The destructor will get called again at the close } of the block in which the local was created.
This is a guarantee of the language. it happens automagically. there’s no way to stop it from
happening.

But you can get really bad results from calling a destructor on the same object a
second time! Bang! You’re dead!

Que 20: What if I want a local to “die” before the close } of the scope in which it was created? Can I call a destructor on a local if I really want to?

Ans: No! [For context, please read the previous FAQ].

Suppose the (desirable) side effect of destructing a local File object is to close the File. Now
suppose you have an object f of a class File and you want File f to be closed before the end of the
scope (i.e., the }) of the scope of object f:

void someCode()
{
File f;
...insert code that should execute when f is still open...
We want the side-effect of f's destructor here!
...insert code that should execute after f is closed...
}

There is a simple solution to this problem. But in the meantime, remember: Do not explicitly
call the destructor!