41+ Advanced C++ Interview Questions And Answers

Spread the love

Advanced C++ Interview Questions

Question 1. How To Initialize Constant And Reference Member Variable?

Using initialization listing.

Question 2. What Is Constant In A Const Function?

Variable ‘this’.

Question 3. What Is The Issue In The Following Program?

#embody <iostream>

int major(int argc, char **argv)


const int & r1 = 100;

int v = 200;

int &r2 = v;

int & r3 = 200;

return 0;


Issue is within the initialization of r3 at line 8, rvalue needs to be a variable.

Question 4. Can The Destructor Be Pure Virtual Function?

Yes, however you continue to need to outline it!

Question 5. What Is The Memory Structure Of An Object?

Usually C++ objects are made by concatenating member variables.

For instance;

class Test


int i;

float j;


is represented by an int adopted by a float.

class TestSub: public Test


The above class is represented by Test after which an int(for int okay). So lastly it is going to be int, float and int.

In addition to this every object can have the vptr(digital pointer) if the category has digital operate, normally as the primary factor in a category.

Question 6. What Is The Difference Between Std::vector<int> X; And Std::vector<int> X();?

First one declares a variable x of kind std::vector<int>. Second one declares a operate x which returns std::vector<int>.

Question 7. What Is A Default Constructor?

A constructor which takes no argument
A constructor which has argument(s) however is(are) with default worth

Question 8. Can I Use This Pointer In The Constructor?

Yes, however attempt to keep away from calling digital operate from the constructor and passing this pointer from the initialization listing to different courses.

Question 9. Does Friends Are Inherited?


Question 10. What Is The Difference Between Calling Just Throw And Throw With An Object In A Catch Block?

A replica of the item is created if we throw with an object. With simply throw, no copy is created.

Question 11. What Is A Possible Replacement For C Static Function In C++?

Unnamed namespaces.

Question 12. What Is The Size Of An Empty Class, Or Class With Only Normal Functions?

Not zero, 1 for many compilers. The cause for that is to have completely different tackle for various object.

Question 13. What Is The Size Of Class With Only Virtual Functions?

Four with a lot of the compilers for a 32bit binary.

Question 14. How To Declare A Namespace Alias?

namespace MyLongNameSpaceName



namespace MLNSN = MyLongNameSpaceName;

Question 15. How To Declare C Function In C++?

By utilizing extern “C”.

extern “C” void print();


extern “C” {

void print();


Question 16. What Is The Difference Between Exit And Abort?

exit does a sleek course of termination, it calls the destructors for all of the constructed objects, with abort they don’t seem to be referred to as.

With exit the native With variables of the calling operate and its callers is not going to have their destructors invoked.

Question 17. Can I Have Static Members In An Union?


Question 18. Can I Overload Destructor?


Question 19. Can I Call Destructor Explicitly?

Yes, however you solely wish to do this when you will have used placement new.

Question 20. Where Virtual Inheritance Should Be Used In A Hierarchy?

If we’ve a diamond class hierarchy we must always use the digital inheritance just under the highest of the diamond.

Question 21. How Do You Link A C++ Program To C Functions?

By utilizing the extern “C” linkage specification across the C operate declarations.

Question 22. Explain The Scope Resolution Operator?

It permits a program to reference an identifier within the international scope that has been hidden by one other identifier with the identical title within the native scope.

Question 23. What Are The Differences Between A C++ Struct And C++ Class?

The default member and base-class entry specifiers are completely different.

Question 24. How Many Ways Are There To Initialize An Int With A Constant?

Two. There are two codecs for initializers in C++ as proven within the instance that follows.

The first format makes use of the standard C notation.

The second format makes use of constructor notation.

int foo = 123;

int bar (123);

Question 25. How Does Throwing And Catching Exceptions Differ From Using Setjmp And Longjmp?

The throw operation calls the destructors for computerized objects instantiated since entry to the attempt block.

Question 26. What Is Your Reaction To This Line Of Code?

It’s not a great observe.

Question 27. What Is A Conversion Constructor?

A constructor that accepts one argument of a unique kind.

Question 28. What Is The Difference Between A Copy Constructor And An Overloaded Assignment Operator?

A replica constructor constructs a brand new object through the use of the content of the argument object. An overloaded task operator assigns the contents of an current object to a different current object of the identical class.

Question 29. When Should You Use Multiple Inheritance?

There are three acceptable solutions: “Never,” “Rarely,” and “When the issue area can’t be precisely modeled another approach.”

Question 30. What Is A Virtual Destructor?

The easy reply is {that a} digital destructor is one that’s declared with the digital attribute.

Question 31. Explain The Isa And Hasa Class Relationships. How Would You Implement Each In A Class Design?

A specialised class “is” a specialization of one other class and, due to this fact, has the ISA relationship with the opposite class. An Employee ISA Person. This relationship is finest applied with inheritance. Employee is derived from Person. A category might have an occasion of one other class. For instance, an worker “has” a wage, due to this fact the Employee class has the HASA relationship with the Salary class. This relationship is finest applied by embedding an object of the Salary class within the Employee class.

Question 32. When Is A Template A Better Solution Than A Base Class?

When you might be designing a generic class to comprise or in any other case handle objects of different varieties, when the format and habits of these different varieties are unimportant to their containment or administration, and notably when these different varieties are unknown (thus, the genericity) to the designer of the container or supervisor class.

Question 33. What Is A Mutable Member?

One that may be modified by the category even when the item of the category or the member operate doing the modification is const.

Question 34. What Is An Explicit Constructor?

A conversion constructor declared with the express key phrase. The compiler doesn’t use an specific constructor to implement an implied conversion of varieties. It’s goal is reserved explicitly for building.

Question 35. What Is The Standard Template Library?

A library of container templates accredited by the ANSI committee for inclusion in the usual C++ specification.

A programmer who then launches right into a dialogue of the generic programming mannequin, iterators, allocators, algorithms, and such, has a better than common understanding of the brand new expertise that STL brings to C++ programming.

Question 36. Describe Run-time Type Identification.?

The means to find out at run time the kind of an object through the use of the typeid operator or the dynamic_cast operator.

Question 37. What Problem Does The Namespace Feature Solve?

Multiple suppliers of libraries would possibly use frequent international identifiers inflicting a reputation collision when an software tries to link with two or extra such libraries. The namespace function surrounds a library’s exterior declarations with a novel namespace that eliminates the potential for these collisions. This answer assumes that two library distributors don’t use the identical namespace identifier, in fact.

Question 38. Are There Any New Intrinsic (built-in) Data Types?

Yes. The ANSI committee added the bool intrinsic kind and its true and false worth key phrases.

Spread the love