• +91 9971497814
  • info@interviewmaterial.com

CPlusPlus Interview Questions Answers

Question 1 : Count Number of characters using Pointers?

Answer 1 : #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 2 : What do you mean by inline function?

Answer 2 : 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.

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

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

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

Answer 4 : 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 5 : What is a scope resolution operator?

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

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 : Explain the scope resolution operator.

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

Answer 9 : 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 10 : What is class invariant?

Answer 10 : 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.

CPlusPlus Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners