These C++ Interview Questions & Answers were created specifically to familiarize you with the types of questions you could face during your C++ interview. The Interview questions typically begin with a basic concept of the subject and then progress based on further conversation and your answers.
Q1. Who developed C++?
Ans. Bjarne Stroustrup developed C++ language at AT & T Bell laboratories in Muarry Hill, New Jersey, the USA, in 1983.
Q2. Name two languages that follow a structured programming approach?
Ans. C and Pascal
Q3. Name five languages that follow Object Oriented programming approach?
Ans. C++, Java, Smalltalk, Python, Eiffel
Q4. What are the key features of Object-Oriented programming languages?
Ans. Encapsulation, data hiding, abstraction, inheritance, polymorphism, dynamic binding, etc.
Q5. Why does every program begin with the main() function?
Ans. It is because this is from where the compiler knows that program execution starts.
Q6. Why is the message function should return a value generated when the main() is compiled?
Ans. a) When you do not specify the return type void of the main( ) function OR
b) When you don’t give return 0; statement in the body of main() in case int return type.
Q7. Is converting a source file into an executable file a two-step process?
Ans. Yes, firstly, the program is compiled to an object file with .obj extension, and then linking is performed, which generates an executable (.exe) file. Linking combines more than one object files into a single executable file.
Q8. What is the user screen area?
Ans. The default user screen area is
Rows = 80 and Columns = 25
Q9. What is the difference between Turbo C++ and Borland C++ compilers?
Ans. Both Turbo C++ and Borland C++ work in the same way except that Borland C++ supports some additional features such as TD (a debugger), Turbo Assembler (TASM), a profiler (TROF).
Q10. What is the different type of file extensions associated during the creation a C++ program?
.cpp – When the source file is saved. E.g., sum.cpp
.obj – When the source program is compiled. E.g., sum.obj
.exe – When we run the program. E.g., sum.exe
.bak – When the already existing source file is modified and saved. E.g., sum.bak.
Q11. Name any five C++ compilers?
Ans. Turbo C++, Borland C++, C front, GNU C++, Zortech C++ (ZTC++)
Q12. What does a header file contain?
Ans. A header file contains the declarations of all the related functions whose definitions are stored in its library file. E.g., iostream.h contains declarations of all console Input/Output operations of C++.
Q13. What is the difference between ‘B’ and “B”?
Ans. ‘B’ is a character constant that contains only ‘B.’ However, “B” is a string constant containing two characters, ‘B’ and ‘\0’.
Q14. Suppose i is an integer variable that is assigned 32767. If we increment the value of i by 1, then what will be the output?
Ans. As we know, that range of integers is till 32767, so adding 1 will make it out of range, and it will start backward so that the output will be -32768.
Q15. Why will the output of the statement c = 5/9* (f-32); always be 0 where c and f are temperatures in degrees Celsius and Fahrenheit?
Ans. As 5/9 is always equal to 0 because both are integer values. So type casting is necessary.
Q16. What is the difference between lvalue and rvalue?
Ans. The lvalue of a variable is the address in memory. We may think of lvalue as location value. The rvalue is the value of the variable. A variable can act as both lvalue and rvalue, whereas a literal constant can only act as an rvalue. Consider the following statements
int a, b;
a = 6;
b = a;
The first assignment variable, a serve as an lvalue, and the second statement variable an act as an rvalue.
Q17. What is the difference between precedence and associativity of operators?
Ans. The hierarchy of operators refers to how operators are evaluated within an expression. In contrast, associativity refers to how the consecutive operations within the same precedence group are carried out.
Q18. How can you access the global variable in the local block of the program if both local and global variables have the same name?
Ans. Using Scope Resolution operator(:)
Q19. What will be the output of statement cout<< (10>20) ?10: 20;?
Ans. 0, the reason is that insertion operator (<<) has higher precedence than conditional operator (?:).
Q20. Classify the types of operators based on the number of operands?
Unary: The operator that uses a single operand.
Binary: The operator that uses two operands.
Ternary: The operator that uses three operands.
Q21. Which header file needs to be included in the program for using manipulators?
Ans. iomanip.h
Q22. What is a buffer?
Ans. It is a block of memory used to store data temporarily. It acts as an intermediator between the peripheral device and the program. It is used to increase the performance of the l/O operations.
Q23. Can you input a number in hexadecimal form?
Ans. Yes, using setbase(16) or hex( ) manipulators.
Q24. How will you display an integer 5237 in a 10 digit field?
Ans. cout<<setw (10) <<5237;
Q25. What is the default precision for a floating-point number to be displayed?
Ans. The default precision of a floating-point number is 6. It can be changed using the set precision () manipulator.
Q26. What are the various if statements available in C++?
1) Simple-if 2) if-else statement
3) Nested if-else 4) else-if ladder
Q27. What is the difference between if (i= =j) and if (i=j)?
Ans. In the first, if statement values of i and j variables are compared, but in the second if statement, the value of j is assigned to i, and if it is non-zero, the result is TRUE otherwise FALSE.
Q28. What will be the output of the given statements?
if(i<10); cout<<"i is less than 10";
Ans. Regardless of whether the condition evaluates to TRUE or FALSE, the message i is less than 10 is displayed in each case. This is because if ended with semicolon (;), is a single statement
if (i<10); 10"; =< if (i<10) cout<<"i is less than 10". cout<<"i is less than
Q29. Name the entry-controlled and exit-controlled loop?
Ans. Entry controlled loops: while, for
Exit controlled loop: do-while
Q30. Which loop’s body is executed atleast once?
Ans. Do-while loop as the body is executed atleast once before the condition is tested.
Q31. Write two infinite loops using for loop.
1. for (;;) {…}
2. for (expl; ;exp3) {…}
3. for(;;exp3) {…}
4. for(expl;;) {…}
Q32. What is a null statement?
Ans. It is sometimes necessary to cause a delay in a program. Therefore, a loop is made to execute a number of times without producing any results. It is known as the Null Statement. It is written as for statement followed by a semicolon.
for (i=1000;i>0;i--);
Thus it continues 1000 times without producing any output.
Q33. Can two case labels have the same value?
Ans. No
Q34. Can a case label in a switch statement have a floating-point variable or floating-point constant?
Ans. No
Q35. When is the if-else-if ladder preferred over the switch statement?
Ans. We use if else -if ladder when conditions controlling the selection process involve multiple variables, for example.
if(p<0) //.......... else if (q>1.05) //......... else if (!finish) //.........
This sequence cannot be recorded with a switch statement because all conditions involve different variables and types.
Q36. What does a header file contain?
Ans. A header file contains the declarations of library functions whose definitions are present in the library file. For example. The header file “math.h” contains declaration of various mathematical library function such as sqrt(), pow() etc.
Q37. Is it necessary to include a function declaration for each user-defined function?
Ans. If the function call precedes the function definition, then we must declare the function before using it. However, if the function definition proceeds the function call(s), there is no need to include function declaration.
Q38. What are the different ways of passing arguments to a function in C++?
Ans. 1. Passing by Value 2. Passing by Reference 3. Passing by Pointers
Q39. What is the output of the following program segment ?
int i=10; int &j=i; j=5; cout<<i;
Ans. 5 as ‘j’ is a reference variable of i.
Q40. What is the use of reference variables?
Ans. Reference variables are used to implement pass by reference parameter passing mechanism in C++.
Q41. What is the difference between passing an argument by value to that of Reference?
Ans. In the case of pass by value, any modification to the formal argument(s) in the called function is not reflected in the actual argument in the calling program, whereas in the case of pass by Reference, a modification made in the called function are reflected in the calling program.
Q42. Can we return more than one value from a function?
Ans. Using a return statement, a function can return only one value. However, a function can return more than one value, using pass by Reference or pass by pointer parameter passing techniques.
Q43. If the user uses a return statement without any value, what will be the function’s return type?
Ans. void
Q44. Can you call a function in the cout statement?
Ans. Yes, you can call a function in the cout statement provided function doesn’t have a void return type, i.e., the function should return something.
Q45. Can two functions with the same name and parameter list be distinguished based on their return type?
Ans. No, Overloaded functions cannot be distinguished based on their return type.
Q46. What is name mangling?
Ans. It is the process in which the name of the functions in the source file is converted to internally different user names. It is usually done in function overloading, where the compiler changes the name of all overloaded function definitions to implement function overloading.
Q47. Is there any ambiguity while overloading functions?
Ans. Yes, when two overloaded functions argue the same type, the bill differs technique of parameter passing. For example :
void f1 (int k) { void f1 (int &k) {
……. . ……
} }
If we call these functions like int k=10; f1(k), the compiler cannot distinguish which version of the overloaded function will be called.
Q48. What is the difference between iterative and recursive programming?
Ans. An iterative function is a function that uses a loop to repeat an action, whereas a recursive function is a function that calls itself repeatedly.
Q49. What is the output of the following program containing the macro?
Ans. 45.75
Q50. Why is it better to use symbolic names for size specification in arrays?
Ans. It becomes easier to modify a program by simply making changes in the value of the symbolic constant.
Q51. What is the benefit of contiguous memory allocation in arrays?
Ans. By knowing the address of the first element, we can access other elements of the array.
Q52. Why do array subscripts start from 0 instead of 1?
Ans. Starting the subscript from 0 simplifies the job of the compiler for handling arrays. Also, it makes the array subscribing marginally faster.
Q53. Is it possible to specify a character as an array subscript?
Ans. Yes, because every character has an ASCII value associated with it which is an integer.
Q54. If a and b are two arrays of the same type, can the statement a=b; work?
Ans. No, two arrays can be copied using the statement a=b; It can be done as
for(int i=0;i<n;i++) a[i]=b[i];
Q55. What will be the output of the following program segment?
main() { ....... int i=1; int a[4]=(1, 2, 3, 4); f1(a[i], i++); void f1 (int a, int b) cout<<a<<\\t'<<b; }
Ans. 3 2
This is because during function call, a stack is maintained in which actual arguments are pushed from right to left f(a[i] ,i++);
First, i++=2 Then a[i]=a[2]=3
Q56. Is it essential for strings to end with ‘\0’?
Ans. Yes, because this is the only way the compiler comes to know that end of the string is encountered.
Q57. What are the different ways by which strings can be assigned values?
1. Through intialization. Eg char sl [] =”Thakur”;
2. Using strcpy() function.Eg strcpy(sl, “Hello”);
3. Using input functions .Eg get() or extraction operator functions
Q58. What is the difference between inputting a string using the get() function and extraction operator (>>) function?
Ans. get() can input multiword, multiline string whereas extraction operator cannot input multiword, multiline string.
Q59. Can we initialize the members of a structure during its declaration?
Ans. No, it is just a blueprint based on which structure variables are created.
Q60. How does structure in C differ from that of structure in C++?
Ans. Structure in C only contains data, whereas structure in C++ contains data as well as associated functions?
Q61. How much memory is allocated when a structure variable is defined?
Ans. Equal to the size of the member of the structure.
Q62. Can we assign one structure variable to another of the same type?
Ans. Yes, as a member by member copy is being made.
Q63. What is the difference between a structure and a union?
Ans. The size of the structure is equal to the size of its members, whereas the size of the union is equal to the size of the member with the maximum size.
Q64. Can we write a[i] as i[a]?
Ans. Yes, since a[i] can be accessed using pointer notation as *(a+i) which is equivalent to *(i+a). Thus in the sum equal to i[a].
Q65. What is the difference between ++s->a and s++->a ?
Ans. The first one increments the contents of data member a and not s structure variable. However, in the second statement, structure variable s is incremented after accessing data member a.
Q66. What is a dangling reference?
Ans. It refers to a situation when a portion of memory referred through the pointer is released (in the free pool), but the pointer still points to it. It happens when memory is not released explicitly.
Q67. What is the difference between int *const ptr=i; and const int * const ptr=i;?
Ans. The first one defines a constant pointer to integer i, and you cannot modify the pointer. But in the second one. You cannot modify the pointer as well as integer to which integer is pointing.
Q68. What is a memory leak?
Ans. It is a block of dynamically allocated memory that we no longer have a pointer pointing to it, and thus we cannot return it to the program for later reuse. It is usually encountered when we forget to delete the dynamically allocated array.
Q69. When will you encounter a null pointer assignment error?
Ans. When you try to perform a read or write operation using a null pointer.
Q70. What is the difference between pointer to arrays and array of pointers?
Ans. Pointer to array refers to using a single pointer to access elements of an array. In contrast, an array of pointers refers to the collection of pointer variables that hold the addresses of different array elements.
Q71. What does the following declaration mean : type (*fp)() and type *fp()
Ans. In the first case, fp is a pointer function that accepts no arguments and returns type. In the second case, fp is a function that accepts no arguments and returns a pointer to type.
Q72. What is a null pointer?
Ans. C++ defines a special value called null pointer for each pointer type. It is used to compare a pointer to any object or function. The internal representation of the null pointer for different data types may be different.
Q73. What are the two significant parts of a class?
Ans. Data and its associated functions
Q74. How does a structure in C++ differ from that of classes?
Ans. Both structure and classes in C++ combine data and its associated functions into a single unit. The basic difference is that structures are specified using the struct keyword, and all its members are public by default, whereas classes are specified using the class keyword. All its members are private by default. Moreover, advanced object-oriented concepts are not implemented using structures.
Q75. What is an access specifier?
Ans. It controls which part of the program can access a member of the class.
Q76. What is the difference between public and private access specifiers?
Ans. The public member is not only accessed inside the class but also from outside the class. However, private members can only be accessed from inside the class.
Q77. What is class instantiation?
Ans. It is a process of creating objects of a class.
Q78. Is it always necessary to specify an access specifier in a class?
Ans. No, if you don’t specify any access specifier, then it is private by default.
Q79. Can you define member function outside the class body?
Ans. Yes, it is possible by declaring the member function inside and defining outside the class body using the scope resolution operator.
Q80. Are the member functions defined inside the class inline by default?
Ans. Yes, provided it contains few statements and doesn’t have control structures like loops, switch, if.
Q81. How much memory is allocated when we create an object of a class?
Ans. Memory allocated to an object is equal to the sum of the size of its data members.
Q82. Should the data members of a class always be private?
Ans. No, it can be public as well as protected.
Q83. How is data hiding implemented in C++?
Ans. Data Hiding is implemented by declaring the data members of the class as private.
Q84. How is encapsulation implemented in C++?
Ans. Encapsulation is implemented using a class that binds the data and its associated functions into a single unit.
Q85. How is abstraction implemented in C++?
Ans. Abstraction is implemented by declaring the member functions inside the class and defining outside, which separate the interfaces of the class from its implementations. The header file (such as math.h) that contains declaration of built-in functions (sin(), sqrt( ) etc.) provide interfaces to the actual implementations present in the library file.
Q86. What happens when we pass an object by reference?
Ans. When an object is passed by reference, an alias of the original passed object is created in the called function. Any modification to it in the called function is automatically reflected in the original object.
Q87. Why passing an object by reference is more efficient than passing a value?
Ans. While passing an object by reference, only the address of the object is passed and not its complete copy as in passing the object by value. So, a lot of memory is saved, especially in the case of large objects.
Q88. What is the difference between passing an object by reference to that of a pointer?
Ans. The main difference is that in the case of passing an object by reference, the address of the object is passed implicitly. In contrast, while passing an object by pointer, the address of the object is passed explicitly to the function.
Q89. Why can data members of a class not be initialized at the point of declaration?
Ans. Since memory is allocated to the data members only when an object of a class is created and not during the class specification, there is no sense to initialize data members at the point of their declaration.
Q90. How are static data members different from non-static data members?
Ans. Static data members get memory only once, no matter how many objects of a class are created, whereas non-static data members get separate memory corresponding to each object. Moreover, unlike non-static data members, a static data member can appear as a default argument to the member function of the class.
Q91. Can a data member of a class be an object of the same class?
Ans. No, because the class is just a blueprint, and a blueprint doesn’t create any object. Objects of a class are created only when the class is defined.
Q92. Can a data member of a class be a static object of the same class?
Ans. Yes, a static object as a data member of the same class is only declared within the class and is defined (allocated memory) outside the class.
Q93. How will you specify a member function which cannot modify the class’s object data member?
Ans. It is possible by declaring the member function as const. E.g. int avg() const;
Q94. What is a mutable data member?
Ans. A data member is not const even when it is a member of a const object. A mutable data member can be modified in a const member function.
Q95. Can a non-const member function be overloaded with a const member function?
Ans. Yes, For example,
int display() const;
int display() ;
Q96. Why does the constructor doesn’t have any return value, not even void?
Ans. The constructor doesn’t have any return value because it is used to initialize data members and is called automatically.
Q97. How do you distinguish a constructor from a regular member function?
Ans. Constructors are distinguished from the normal function of the class as they have the same name as that of class.
Q98. Can a constructor be overloaded?
Ans. Yes, constructors can be overloaded to provide more than one type of initialization.
Q99. What are the different ways for specifying arguments to a constructor?
Ans. There are 3 ways
• rect r1(5,6);
• rect r1= rect(5,6);
• rect r2=5;
For the third form, only one argument constructor can work.
Q100. Can the destructor be overloaded?
Ans. Since we have only one destructor in a class that cannot have any parameters, it cannot be overloaded.
Q101. How can a dynamic object be created in C++?
Ans. A dynamic object can be created using a new operator as follows,
dist *p;//pointer to the class. dist
p = new dist;
The above statement creates an object of dist class dynamically.
Q102. How is a dynamic object destroyed explicitly in C++?
Ans. A dynamic object is destroyed explicitly using the delete operator as follows, delete p;
where p is a pointer pointing to a dynamic object.
Q103. What will happen when the statement dist *p = new dist[10]; is executed? Here dist is the name of the class.
Ans. The statement,
dist *p=new dist[10];
on execution creates IO objects of the ‘dist’ class dynamically and returns the pointer to the first object. Also, the no-argument constructor is invoked for each object created.
Q104. What is the difference between delete ptr; and delete [] ptr, where ptr is a pointer pointing to dynamic object(s)?
Ans. Use the statement,
delete ptr; //ptr points to dynamic object to deallocate memory allocated to the single dynamic object created using statement,
dist *ptr = new dist;
The statement,
delete[] ptr;
is used to deallocate block of memory allocated to an array of dynamic objects created using the statement,
dist *ptr=new dist[10];
Q105. Can we initialize an object with the default constructor using the statement, student s1();?
Ans. The statement,
student s1();
is not used to initialized s1 object using default constructor as compiler interprets the above statement as a declaration of a function having name s1 which takes no parameters and return an object of the class student. The correct statement of object initialization using the default constructor is to skip the trailing empty parentheses as student s1;
Q106. When is a copy constructor invoked?
Ans. When an object is passed by value, returned by value, or explicitly copied, a copy constructor is invoked.
Q107. When is the copy constructor useful?
Ans. The copy constructor is useful when the data member of the copying object (source object ) is a pointer pointing to dynamically allocated memory.
Q108. If a class contains no constructor, can its objects be created?
Ans. Yes, objects of the class can still be created. However, these objects are not initialized. For each class, there is a pre-defined default constructor that does nothing.
Q109. Which operators cannot be overloaded?
Ans. Dot operator (.), .* , conditional operator (?:), scope resolution operator (::), sizeof operator.
Q110. What are different ways of overloading an operator?
Ans. An overloaded operator function can be a member function of the class or a non-member (friend) of the class.
Q112. How many arguments do you need to pass while overloading an operator?
Ans. While overloading a unary operator member function, there is no need to pass any argument to the overloaded function. In contrast, a binary operator overloaded member function requires one argument to pass. Thus overloading an operator as a member function of the class always requires one less argument than its operands. However, to overload an operator using non-member (friend function) of the class, the unary overloaded operator function requires one argument to pass. The binary overloaded operator function requires two arguments to pass. Thus, overloading an operator using the friend function always requires arguments equal to the number of operands used with the operator.
Q113. How can you distinguish between a prefix and postfix increment operator while overloading?
Ans. They are distinguished by dummy int-parameter. The prefix version of the overloaded increment operator takes no parameter, whereas the postfix version takes a dummy (unused) parameter of type int. So, declaration of prefix overloaded operator function is
return_type operator ++();
and declaration of overloaded postfix operator function is,
return_type operator ++(int);
Q114. Is it possible to change the precedence of operators using operator overloading?
Ans. No
Q115. If c1 and c2 of two objects of class complex, then will the statement, c2 = 5.0+c1; work by overloading operator +, which is a member function of the class complex?
Ans. The statement,
c2 = 5.0+c1;
Will not work, although we overload the ‘+’ operator as a member function. It happens because the ‘+’ operator’s left operand is not the object of class complex but a value of float type. So, there is no way to generate a call to the overloaded operator ‘+()’ member function.
To overcome this problem, overloaded the ‘+’ operator using the friend function.
Q116. Will the following statement work?
t1=10;
where t1 is an object of class time?
Ans. It will work if we have one argument constructor conversion function in-class time.
Q117. Will the following statement work?
int min=t1;
where t1 is an object of class time?.
Ans. It will work only if we overload the int type cast operator in the class time.
Q118. If objX and objY are two objects of different classes, X and Y, how does the statement, objX = objY; works?
Ans. To execute the statement, objX = objY, we need to make the conversion routine either in the source class (class Y) or in the destination class (class X). If the conversion routine is defined in the source class, we use the overloaded cast operator function. If it is defined in the destination class, we use a one-argument constructor or overloaded equal to the ‘=’ operator function.
Q119. How can you access the base class’s public members through an object of the derived class from main()?.
Ans. It is possible only if the class is derived publicly. It is because of public Inheritance. Public members of the base class become the public of the derived class.
Q120. What is the difference between public and private Inheritance?
Ans. The difference is that in the case of public Inheritance, the public member of base and derived classes can be accessed directly by the objects of the derived class. In contrast, in the case of private Inheritance, only the public members of the derived class can be accessed directly by the objects of the derived class.
Q121. What is the difference between protected and private Inheritance?
Ans. In the protected Inheritance, continued access to the base class members further down the descendent derived classes which you might have derived from an existing derived class is possible. It is not so in the case of private Inheritance.
Q122. When should base class data members be made protected rather than private or public?
Ans. If we want that certain base class members to be assessed directly in the base class and its derived class, but not from outside the base or derived classes (i.e., main( )), then such members of the base class should be declared as protected.
Q123. What is the size of the object of the derived class?
Ans. This size of the derived class object is equal to the sum of sizes of data members of the base class and derived class.
Q124. If a class is derived publicly from a base class and both classes contain a same-named public member function f1(), then can an object of the derived class access the base’s same-named member function from main()?
Ans. Yes, using the scope resolution operator as derv_obj.base_class_name::f1();
Q125. If both the base class and the derived class contains the same-named public member function f1(), then which version of f1() will be invoked when called through an the object of the derived class?
Ans. The object of derive class invokes the f1( ) version defined in the derived class as the compiler always first searches the same-named member function in the derived class and then in the base class.
Q126. Is there any relationship that exists between classes that form the Inheritance?
Ans. Yes, there is a kind of relationship that exists between a derived class and a base class.
Q127. What is the order of execution of constructors and destructors in Inheritance?
Ans. Constructors are executed in derivation in Inheritance, whereas destructors are executed in the reverse order of their creation.
Q128. What kind of relationship exists between classes involved in composition?
Ans. has a relationship.
Q129. What are the benefits of polymorphism?
Ans. The main benefit of polymorphism is that it reduces complexity by allowing the same-named member function to perform different tasks. Also, it is possible to design and implement easily extensible systems.
Q130. How is compile-time polymorphism implemented in C++?
Ans. Compile-time polymorphism is implemented using function overloading and operator overloading.
Q131. How is run time polymorphism implemented in C++?
Ans. It is implemented using virtual functions.
Q132. How do abstract classes differ from concrete classes?
Ans. A class that cannot be instantiated and contains atleast one pure virtual function is called an abstract class. However, a concrete class can be instantiated.
Q133. How can compile-time polymorphism be distinguished from run-time polymorphism?
Ans. In compile-time polymorphism, static binding is performed. The compiler decides to select the appropriate function to be called in response to the compile-time call. However, in run time polymorphism, there is dynamic binding, in which case the compiler makes the binding decision at run time.
Q134. Can a pointer to the base class point to an object of the derived class?
Ans. Yes
Q135. Can we declare a static function as virtual?
Ans. No, because declaring static function as virtual doesn’t implement virtual function mechanism as the static function is not related to objects and is invoked through the class name.
Q136. Does a virtual function declare in the base class become virtual in the derived class when it is overridden?
Ans. Yes
Q137. What is the difference between overloaded and overridden functions?
Ans. Overloading occurs when two or more functions have the same name but different parameter lists. Still, overriding occurs when a class and one of its derived classes both define a member function with the same name, parameter list, and the member function is declared to be virtual in the base class.
Q138. How many arguments are passed in the following member function call?
obj1.compute(i);
where obj1 is object of class having member function compute().
Ans. There are two arguments passed in the compute() member function call. The first one is the address of the object that generates the member function call implicitly, and the other one is the argument i passed explicitly. The pass object’s address is received in the called function by this pointer, which enables the function to understand which object generates the call.
Q139. Which of the pre-defined stream objects are automatically opened when the program execution starts?
Ans. Cin, cout, cerr, and clog.
Q140. How is the cascading of the insertion operator performed?
Ans. Consider the statement,
cout<<a<<b;
The result of the expression ‘cout<<a’ is the cout object itself which is then reapplied to operand b, then computed.
Q141. What is the difference between pre-defined stream object cerr and clog?
Ans. Both cerr and clog pre-defined objects are used to display error messages on the screen. The difference is that error messages to be displayed using cerr are unbuffered, whereas it is fully buffered in the clog.
Q142. When does the expression while (cin >> val) become false?
Ans. When Ctrl+z is pressed, the end of the file is read, or an invalid value has been encountered?
Q143. How does the insertion operator work with different types of data?
Ans. The insertion operator (<<) is a member function of class ostream where it is overloaded to work with different types of data.
Q144. Do the following statements execute properly or not? If not, why?
cin.get(a,50);
cin.get(b,30);
where ‘a’ and ‘b’ are both character arrays.
Ans. No, The reason is that when we input an array of characters a using get() function from the input stream, then the newline character ‘\n’ itself is not read but is left as the next character of the stream, which is then read by next get( ) statement so these consecutive get( ) statement can result in an error.
In order to get rid of this problem, remove ‘\n’ character from the stream using the member function ignore( ) as follows
cin.get(a,50);
cin.ignore();
cin.get(b,50);
Q145. Why is getline () preferred over get() for inputting multiline strings?
Ans. Since getline( ) functions discards the delimiter rather than leaving it in the input stream until the next input operation. It is not in the case of the get( ) function.
Q146. What is the drawback of the extraction operator while taking input for the character array?
Ans. The problem is that multiword strings cannot be inputted using the extraction operator as it discards characters after the first white space. This problem can be solved by using the get function.
Q147. Can we cascade put() function?
Ans. Yes, as follows, cout.put(‘a’) .put(‘b’);
Q148. Which header file needs to be included for performing file I/O operation?
Ans. fstream.h
Q149. What are different ways of operating a file?
Ans. 1. Using one argument constructor of appropriate stream class Eg.
ifstream in(“abc.txt”);
2. Using open( ) member function.Eg
ifstream in;
in.open(“abc.txt”);
Q150. Is it necessary to close them explicitly?
Ans. Yes, if the programmer wants to open the file in different modes later on.