• +91 9723535972
  • info@interviewmaterial.com

CPlusPlus Interview Questions and Answers

CPlusPlus Interview Questions and Answers

Question - 21 : - What is a template?

Answer - 21 : - Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones: template function_declaration; template function_declaration; The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

Question - 22 : - Define a constructor - What it is and how it might be called (2 methods).

Answer - 22 : - 1. constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized. Ways of calling constructor: 1) Implicitly: automatically by complier when an object is created. 2) Calling the constructors explicitly is possible, but it makes the code unverifiable. 2. class Point2D{ int x; int y; public Point2D() : x(0) , y(0) {} //default (no argument) constructor }; main(){ Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the default constructor is implicitly called. Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory on HEAP we call the default constructor. You have two pairs: new() and delete() and another pair : alloc() and free().

Question - 23 : - Explain differences between eg. new() and malloc()

Answer - 23 : - 1. 1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont use brackets will calling new or delete]. 2.) no need of allocate the memory while using “new” but in “malloc()” we have to use “sizeof()”. 3.) “new” will initlize the new memory to 0 but “malloc()” gives random value in the new alloted memory location [better to use calloc()] 2. new() allocates continous space for the object instace malloc() allocates distributed space. new() is castless, meaning that allocates memory for this specific type, malloc(), calloc() allocate space for void * that is cated to the specific class type pointer.

Question - 24 : - What is the difference between class and structure?

Answer - 24 : - Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

Question - 25 : - What is RTTI?

Answer - 25 : - Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many Interview Questions - Homegrown versions with a solid, consistent approach.

Question - 26 : - What is encapsulation?

Answer - 26 : - Packaging an object’s variables within its methods is called encapsulation.

Question - 27 : - Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE

Answer - 27 : - 1. POLYMORPHISM : A phenomenon which enables an object to react differently to the same function call. in C++ it is attained by using a keyword virtual Example public class SHAPE { public virtual void SHAPE::DRAW()=0; } Note here the function DRAW() is pure virtual which means the sub classes must implement the DRAW() method and SHAPE cannot be instatiated public class CIRCLE::public SHAPE { public void CIRCLE::DRAW() { // TODO drawing circle } } public class SQUARE::public SHAPE { public void SQUARE::DRAW() { // TODO drawing square } } now from the user class the calls would be like globally SHAPE *newShape; When user action is to draw public void MENU::OnClickDrawCircle(){ newShape = new CIRCLE(); } public void MENU::OnClickDrawCircle(){ newShape = new SQUARE(); } the when user actually draws public void CANVAS::OnMouseOperations(){ newShape->DRAW(); } 2. class SHAPE{ public virtual Draw() = 0; //abstract class with a pure virtual method }; class CIRCLE{ public int r; public virtual Draw() { this->drawCircle(0,0,r); } }; class SQURE public int a; public virtual Draw() { this->drawRectangular(0,0,a,a); } }; Each object is driven down from SHAPE implementing Draw() function in its own way.

Question - 28 : - What is an object?

Answer - 28 : - Object is a software bundle of variables and related methods. Objects have state and behavior.

Question - 29 : - How can you tell what shell you are running on UNIX system?

Answer - 29 : - You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

Question - 30 : - What do you mean by inheritance?

Answer - 30 : - Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

NCERT Solutions


Share your email for latest updates


Our partners