• +91 9971497814
  • info@interviewmaterial.com

CPlusPlus Interview Questions Answers

Question 1 : What problems might the following macro bring to the application?

Answer 1 : #define sq(x) x*x

Question 2 : What’s the output of the following program? Why?

Answer 2 : #include <stdio.h> main() { typedef union { int a; char b[10]; float c; } Union; Union x,y = {100}; x.a = 50; strcpy(x.b,\"hello\"); x.c = 21.50; printf(\"Union x : %d %s %f \n\",x.a,x.b,x.c ); printf(\"Union y :%d %s%f \n\",y.a,y.b,y.c); } Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively) What is output equal to in output = (X & Y) | (X & Z) | (Y & Z)

Question 3 : Write a program that ask for user input from 5 to 9 then calculate the average

Answer 3 : #include "iostream.h" int main() { int MAX = 4; int total = 0; int average; int numb; for (int i=0; i<MAX; i++) { cout << "Please enter your input between 5 and 9: "; cin >> numb; while ( numb<5 || numb>9) { cout << "Invalid input, please re-enter: "; cin >> numb; } total = total + numb; } average = total/MAX; cout << "The average number is: " << average << "\n"; return 0; }

Question 4 : Count Number of characters using Pointers?

Answer 4 : #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 5 : What are the advantages of inheritance?

Answer 5 : • 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 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 are the conditions that have to be met for a condition to be an invariant of the class?

Answer 7 : * The condition should hold at the end of every constructor. * The condition should hold at the end of every mutator (non-const) operation.

Question 8 : Explain differences between eg. new() and malloc()

Answer 8 : 1. 1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont use brackets will calling new or delete]. 2.) no need of allocate the memory while using “new” but in “malloc()” we have to use “sizeof()”. 3.) “new” will initlize the new memory to 0 but “malloc()” gives random value in the new alloted memory location [better to use calloc()] 2. new() allocates continous space for the object instace malloc() allocates distributed space. new() is castless, meaning that allocates memory for this specific type, malloc(), calloc() allocate space for void * that is cated to the specific class type pointer.

Question 9 : What is the difference between an ARRAY and a LIST?

Answer 9 : 1. Array is collection of homogeneous elements. List is collection of heterogeneous elements. For Array memory allocated is static and continuous. For List memory allocated is dynamic and Random. Array: User need not have to keep in track of next memory allocation. List: User has to keep in Track of next location where memory is allocated. 2. Array uses direct access of stored members, list uses sequencial access for members. //With Array you have direct access to memory position 5 Object x = a[5]; // x takes directly a reference to 5th element of array //With the list you have to cross all previous nodes in order to get the 5th node: list mylist; list::iterator it; for( it = list.begin() ; it != list.end() ; it++ ) { if( i==5) { x = *it; break; } i++; }

Question 10 : What are the advantages and disadvantages of B-star trees over Binary trees?

Answer 10 : 1. B-star trees have better data structure and are faster in search than Binary trees, but it’s harder to write codes for B-start trees. 2. The major difference between B-tree and binary tres is that B-tree is a external data structure and binary tree is a main memory data structure. The computational complexity of binary tree is counted by the number of comparison operations at each node, while the computational complexity of B-tree is determined by the disk I/O, that is, the number of node that will be loaded from disk to main memory. The comparision of the different values in one node is not counted.

CPlusPlus Contributors


Share your email for latest updates


Our partners