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 - 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

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."