• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

CPlusPlus Interview Questions Answers

Question - 91 : - What are virtual functions?

Answer - 91 : - A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

Question - 92 : - What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?

Answer - 92 : - virtual

Question - 93 : - How do you find out if a linked-list has an end? (i.e. the list is not a cycle)

Answer - 93 : - You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle.

Question - 94 : - What is the use of ‘using’ declaration. ?

Answer - 94 : - A using declaration makes it possible to use a name from a namespace without the scope operator.

Question - 95 : - Detemine the code below, tell me exectly how many times is the operation sum++ performed ?

Answer - 95 : - for ( i = 0; i < 100; i++ ) for ( j = 100; j > 100 - i; j–) sum++; (99 * 100)/2 = 4950 The sum++ is performed 4950 times.

Question - 96 : - Does delete p delete the pointer p, or the pointed-to-data *p?

Answer - 96 : - The keyword should really be delete_the_thing_pointed_to_by.  The same abuse of English occurs when freeing the memory pointed to by a pointer in C: free(p) really means free_the_stuff_pointed_to_by(p).

Question - 97 : - 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 - 97 : - 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 - 98 : - Define a constructor - What it is and how it might be called (2 methods).

Answer - 98 : - 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 - 99 : - What are the advantages of inheritance?

Answer - 99 : - • It permits code reusability. • Reusability saves time in program development. • It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.

Question - 100 : - When should you use multiple inheritance?

Answer - 100 : - There are three acceptable answers: "Never," "Rarely," and "When the problem domain cannot be accurately modeled any other way."

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners