• +91 9971497814
  • info@interviewmaterial.com

CPlusPlus Interview Questions Answers

Question 1 : What is C++?

Answer 1 : C++ is a programming language. It literally means "increased C", reflecting its nature as an evolution of the C language.

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

Answer 2 : 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 3 : Is it necessary to already know another programming language before learning C++?

Answer 3 : Not necessarily. C++ is a simple and clear language in its expressions. It is true that a piece of code written with C++ may be seen by a stranger of programming a bit more cryptic than some other languages due to the intensive use of special characters ({}[]*&!|...), but once one knows the meaning of such characters it can be even more schematic and clear than other languages that rely more on English words. Also, the simplification of the input/output interface of C++ in comparison to C and the incorporation of the standard template library in the language, makes the communication and manipulation of data in a program written in C++ as simple as in other languages, without losing the power it offers.

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

Answer 4 : 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 5 : Describe run-time type identification.

Answer 5 : The ability to determine at run time the type of an object by using the typeid operator or the dynamic_cast operator.

Question 6 : What is class invariant?

Answer 6 : A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In particular all class invariants are both preconditions and post-conditions for all operations or member functions of the class.

Question 7 : Explain the ISA and HASA class relationships. How would you implement each in a class design?

Answer 7 : A specialized class "is" a specialization of another class and, therefore, has the ISA relationship with the other class. An Employee ISA Person. This relationship is best implemented with inheritance. Employee is derived from Person. A class may have an instance of another class. For example, an employee "has" a salary, therefore the Employee class has the HASA relationship with the Salary class. This relationship is best implemented by embedding an object of the Salary class in the Employee class.

Question 8 : How do you link a C++ program to C functions?

Answer 8 : By using the extern "C" linkage specification around the C function declarations.

Question 9 : Explain the scope resolution operator.

Answer 9 : It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

Question 10 : What is polymorphism? Explain with an example?

Answer 10 : "Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object. Example: function overloading, function overriding, virtual functions. Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.

CPlusPlus Contributors


Share your email for latest updates


Our partners