C++ Programming written test Questions & Answers focuses on “Constructors and Destructors – 3”.
Q 1. Which of the following constructors are provided by the C++ compiler if not defined in a class?
A. Default constructor
B. Copy Assignment Operator
C. Copy constructor
D. All of the mentioned
Show Answer
Answer:-D. All of the mentionedExplanation
If a programmer does not define the above constructors in a class the C++ compiler by default provides these constructors to avoid error on basic operations. The compiler will do a shallow copy for “Copy constructor” and “Copy Assignment Operator” if there are no user-defined “copy constructors” or “copy assignment operator” constructors.Q 2. When a copy constructor is called?
A. When an object of the class is returned by value
B. When an object of the class is passed by value to a function
C. When an object is constructed based on another object of the same class
D. All of the mentioned
Show Answer
Answer:-D. All of the mentionedExplanation
Copy constructor is called in all the above-mentioned criteria because in all the above cases we are somehow trying to copy one object into another.Q 3. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ A(){ cout<<"Constructor called"; } }; int main(int argc, char const *argv[]) { A a; return 0; }
A. Constructor called
B. Nothing printed
C. Segmentation fault
D. Error
Show Answer
Answer:-D. ErrorExplanation
No constructor should be made private because objects need to call them and as by default all the members of a class are private therefore constructor defined in the above program is private which is wrong therefore the compiler gives the error.Q 4. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: int a; A(int a){ this->a = a; } }; int main(int argc, char const *argv[]) { A a1, a2(10); cout<<a2.a; return 0; }
A. 10
B. Compile time error
C. Run-time error
D. No output
Show Answer
Answer:-B. Compile time errorExplanation
The declaration of object a1 needs a constructor without any arguments which is not available in the class as we have overwritten the default constructor, therefore, the program gives the error.Q 5. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: A(){ cout<<"Constructor called"; } }; int main(int argc, char const *argv[]) { A *a; return 0; }
A. Constructor called
B. Nothing printed
C. Segmentation fault
D. Error
Show Answer
Answer:-B. Nothing printedExplanation
As we have declared a pointer variable for class A but we have not initialized the memory to that pointer and until the memory is not initialized the constructor for the pointer variable will not be called hence nothing is printed on the screen.Q 6. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: int a; }; int main(int argc, char const *argv[]) { A a1 = {10}; A a2 = a1; cout<<a1.a<<a2.a; return 0; }
A. 1010
B. 87368746
C. Segmentation fault
D. Error
Show Answer
Answer:-A. 1010Explanation
Although the declaration of object a1 looks erroneous but actually it is acceptable by the C++ compiler to take values for class objects as mentioned above. Next value of a1 is copied to a2 hence 1010 is printed. Output: $ ./a.out 1010Q 7. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: int a; A(int a=0){ this->a = a; } }; int main(int argc, char const *argv[]) { A a1, a2(10); cout<<a1.a<<a2.a; return 0; }
A. 010
B. 100
C. 001
D. Error
Show Answer
Answer:-A. 010Explanation
As constructor is accepting default parameter therefore the declaration of a1 and a2 both are valid hence the program runs successfully.Q 8. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: int a; A(){ cout<<"Constructor called"; } }; int main(int argc, char const *argv[]) { A *a1 = (A*)malloc(sizeof(A)); return 0; }
A. Nothing printed
B. Constructor called
C. Segmentation fault
D. Error
Show Answer
Answer:-A. Nothing printedExplanation
Unlike new malloc never calls the constructor of a class hence when we are assigning memory to an object of class A using malloc constructor is not called hence nothing is printed.Q 9. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: int a; A(){ cout<<"Constructor called"; } } a; int main(int argc, char const *argv[]) { return 0; }
A. Segmentation fault
B. Nothing printed
C. Constructor called
D. Error
Show Answer
Answer:-C. Constructor calledExplanation
In this program, we have defined a global variable an outside main function for which constructor will be called hence the output is printed.Q 10. How destructor overloading is done?
A. By changing the number of parameters
B. By changing the parameters type
C. By changing both the number of parameters and their type
D. No chance for destructor overloading
Show Answer
Answer:-D. No chance for destructor overloadingExplanation
A class is allowed to have only one destructor. Therefore there is no point of destructor overloading.Q 11. Which of the following is correct?
A. Destructor is used to initialize objects
B. There can be more than one destructor in a class
C. Destructor definition starts with !
D. Destructors can be virtual
Show Answer
Answer:-D. Destructors can be virtualExplanation
Destructors can be virtual. They are used to destroy objects. Only one destructor is allowed per class. Destructor definition starts with a tilde(~).Q 12. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ ~A(){} }; class B { public: A a; }; int main(int argc, char const *argv[]) { B b; return 0; }
A. A’s Constructor called
B’s constructor called
B. B’s Constructor called
A’s constructor called
C. Segmentation fault
D. Error
Show Answer
Answer:-D. ErrorExplanation
In this program, the destructor of class A is private therefore the destructor for object a cannot be called hence the program gives an error.Q 13. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ A(){ cout<<"A's Constructor called\n"; } friend class B; }; class B { public: A a; B(){ cout<<"B's constructor called\ns"; } }; int main(int argc, char const *argv[]) { B b; return 0; }
A. A’s Constructor called
B’s constructor called
B. B’s Constructor called
A’s constructor called
C. Error
D. Segmentation fault
Show Answer
Answer:-A. A’s Constructor called B’s constructor calledExplanation
Now still the constructor of a class is private but class B is friend class of A hence it can access the private members of class A and as in the above program we defining an object of class A in class B only, therefore, the program runs fine.Q 14. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ public: int a; A(){ cout<<"Constructor called\n"; } } a; int main(int argc, char const *argv[]) { cout<<"Main function\n"; return 0; }
A. Main function
Constructor called
B. Constructor called
Main function
C. Error
D. Segmentation fault
Show Answer
Answer:-B. Constructor called Main functionExplanation
In this program, we have defined a global variable an outside main function for which constructor will be called. Now as a is a global variable, therefore, the call for constructor will be before the main function hence constructor called will be printed before the Main function.Q 15. What will be the output of the following C++ code?
#include <iostream> using namespace std; class A{ A(){ cout<<"A's Constructor called\n"; } }; class B { public: A a; B(){ cout<<"B's constructor called\ns"; } }; int main(int argc, char const *argv[]) { B b; return 0; }
A. A’s Constructor called
B’s constructor called
B. B’s Constructor called
A’s constructor called
C. Segmentation fault
D. Error
Leave a Reply