• +91 9971497814
  • info@interviewmaterial.com

CPlusPlus Interview Questions Answers

Question 1 : Anything wrong with this code? T *p = 0; delete p;

Answer 1 : Yes, the program will crash in an attempt to delete a null pointer.

Question 2 : Anything wrong with this code? T *p = new T[10]; delete p;

Answer 2 : Everything is correct, Only the first element of the array will be deleted”, The entire array will be deleted, but only the first element destructor will be called.

Question 3 : Are there any new intrinsic (built-in) data types?

Answer 3 : Yes. The ANSI committee added the bool intrinsic type and its true and false value keywords.

Question 4 : Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?

Answer 4 : 1.   In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object. For example: complex c1,c2; c1=c2; //this is assignment complex c3=c2; //copy constructor 2.   A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler: There is no need to test to see if it is being initialized from itself. There is no need to clean up (eg, delete) an existing value (there is none). A reference to itself is not returned.

Question 5 : Can I free() pointers allocated with new? Can I delete pointers        allocated with malloc()?

Answer 5 : No! It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program.  But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc(). Beware! I occasionally get e-mail from people telling me that it works OK for them on machine X and compiler Y.  That does not make it right! Sometimes people say, "But I'm just working with an array of char." Nonetheless do not mix malloc() and delete on the same pointer, or new and free() on the same pointer! If you allocated via p = new char[n], you must use delete[] p; you must not use free(p).  Or if you allocated via p = malloc(n), you must use free(p); you must not use delete[] p or delete p! Mixing these up could cause a catastrophic failure at runtime if the code was ported to a new machine, a new compiler, or even a new version of the same compiler. You have been warned.

Question 6 : Could you tell something about the Unix System Kernel?

Answer 6 : The kernel is the heart of the UNIX openrating system, it’s reponsible for controlling the computer’s resouces and scheduling user jobs so that each one gets its fair share of resources.

Question 7 : Count Number of characters using Pointers?

Answer 7 : #include<iostream.h> void main() { int Char_Count(char *get_string); char *strcount "THIS IS A STRING"; int int_word_count Char_Count(strcount); cout<<"word count is "<<int_word_count; } int Char_Count(char *get_string) { int count 0; if (get_string ! NULL) { for (;*(get_string+count) ! ' ';++count); } return count; }  

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

Answer 8 : 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 9 : Define namespace.

Answer 9 : It is a feature in C++ to minimize name collisions in the global name space. This namespace keyword assigns a distinct name to a library that allows other libraries to use the same identifier names without creating any name collisions. Furthermore, the compiler uses the namespace signature for differentiating the definitions.

Question 10 : Define precondition and post-condition to a member function.

Answer 10 : Precondition: A precondition is a condition that must be true on entry to a member function. A class is used correctly if preconditions are never false. An operation is not responsible for doing anything sensible if its precondition fails to hold. For example, the interface invariants of stack class say nothing about pushing yet another element on a stack that is already full. We say that isful() is a precondition of the push operation. Post-condition: A post-condition is a condition that must be true on exit from a member function if the precondition was valid on entry to that function. A class is implemented correctly if post-conditions are never false. For example, after pushing an element on the stack, we know that isempty() must necessarily hold. This is a post-condition of the push operation.

CPlusPlus Contributors


Share your email for latest updates


Our partners