• +91 9971497814
  • info@interviewmaterial.com

CPlusPlus Interview Questions Answers

Question 1 : Describe run-time type identification.

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

Question 2 : What is class invariant?

Answer 2 : 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 3 : Explain the ISA and HASA class relationships. How would you implement each in a class design?

Answer 3 : 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 4 : How do you link a C++ program to C functions?

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

Question 5 : Explain the scope resolution operator.

Answer 5 : 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 6 : What is polymorphism? Explain with an example?

Answer 6 : "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.

Question 7 : What is a scope resolution operator?

Answer 7 : A scope resolution operator (::), can be used to define the member functions of a class outside the class.

Question 8 : Tell how to check whether a linked list is circular?

Answer 8 : Create two pointers, each set to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) { print (\"circular\n\"); } } OK, why does this work? If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.

Question 9 : Write a short code using C++ to print out all odd number from 1 to 100 using a for loop

Answer 9 : for( unsigned int i = 1; i < = 100; i++ ) if( i & 0x00000001 ) cout << i << \",\";

Question 10 : What do you mean by inline function?

Answer 10 : The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.

CPlusPlus Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners