• +91 9723535972
  • info@interviewmaterial.com

CPlusPlus Interview Questions and Answers

CPlusPlus Interview Questions and Answers

Question - 11 : - What do you mean by inline function?

Answer - 11 : - 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 - 12 : - Write a program that ask for user input from 5 to 9 then calculate the average

Answer - 12 : - #include "iostream.h" int main() { int MAX = 4; int total = 0; int average; int numb; for (int i=0; i> 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 - 13 : - Write a short code using C++ to print out all odd number from 1 to 100 using a for loop

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

Question - 14 : - What is public, protected, private?

Answer - 14 : - Public, protected and private are three access specifier in C++. Public data members and member functions are accessible outside the class. Protected data members and member functions are only available to derived classes. Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes. Write a function that swaps the values of two integers, using int* as the argument type. void swap(int* a, int*b) { int t; t = *a; *a = *b; *b = t; }

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

Answer - 15 : - 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 - 16 : - What is virtual constructors/destructors?

Answer - 16 : - 1. Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. There is a simple solution to this problem declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. 2. Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error. Does c++ support multilevel and multiple inheritance? Yes.

Question - 17 : - What are the advantages of inheritance?

Answer - 17 : - • 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 - 18 : - What is the difference between declaration and definition?

Answer - 18 : - The declaration tells the compiler that at some later point we plan to present the definition of this declaration. E.g.: void stars () //function declaration The definition contains the actual implementation. E.g.: void stars () // declarator { for(int j=10; j>=0; j--) //function body cout<<”*”; cout< Question - 19 : - What is the difference between an ARRAY and a LIST?

Answer - 19 : - 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 - 20 : - Does c++ support multilevel and multiple inheritance?

Answer - 20 : - Yes.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners