Question 1 of 25
Hints left: 3
Q1
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; }
Q2
Which of the following constructors are provided by the C++ compiler if not defined in a class?
Q3
What is a copy constructor?
Q4
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; }
Q5
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; }
Q6
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class A{ mutable int a; public: A(){ cout<<"A's Constructor called\n"; } ~A(){ cout<<"A's Destructor called\n"; } }; class B{ static A a; public: B(){ cout<<"B's Constructor called\n"; } ~B(){ cout<<"B's Destructor called\n"; } }; int main(int argc, char const *argv[]) { B b1; }
Q7
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class A{ mutable int a; public: A(){ cout<<"A's Constructor called\n"; } ~A(){ cout<<"A's Destructor called\n"; } }; class B: public A{ public: B(){ cout<<"B's Constructor called\n"; } ~B(){ cout<<"B's Destructor called\n"; } }; int main(int argc, char const *argv[]) { B b1; }
Q8
How many types of constructors are there in C++?
Q9
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<<a2.a; return 0; }
Q10
In the following C++ code how many times the string "A's constructor called" will be printed? #include <iostream> #include <string> using namespace std; class A{ int a; public: A(){ cout<<"A's constructor called"; } }; class B{ static A a; public: B(){ cout<<"B's constructor called"; } static A get(){ return a; } }; A B::a; int main(int argc, char const *argv[]) { B b; A a1 = b.get(); A a2 = b.get(); A a3 = b.get(); }
Q11
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; }
Q12
What is the role of destructors in Classes?
Q13
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class A{ mutable int a; public: A(){ cout<<"A's default constructor called\n"; } A(const A& a){ cout<<"A's copy Constructor called\n"; } }; class B{ A obj; public: B(){ cout<<"B's Constructor called\n"; } }; int main(int argc, char const *argv[]) { B b1; B b2; }
Q14
What is syntax of defining a destructor of class A?
Q15
When a copy constructor is called?
Q16
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class A{ mutable int a; public: A(){ cout<<"A's Constructor called\n"; } ~A(){ cout<<"A's Destructor called\n"; } }; class B{ static A a; public: B(){ cout<<"B's Constructor called\n"; } ~B(){ cout<<"B's Destructor called\n"; } }; A B::a; int main(int argc, char const *argv[]) { return 0; }
Q17
What is the difference between constructors and destructors?
Q18
What will be the output of the following C++ code? #include <iostream> #include <string> using namespace std; class A{ int a; public: A(int i){ a = i; } void assign(int i){ a = i; } int return_value(){ return a; } }; int main(int argc, char const *argv[]) { A obj; obj.assign(5); cout<<obj.return_value(); }
Q19
When destructors are called?
Q20
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; }
Q21
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; }
Q22
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; }
Q23
How many parameters does a default constructor require?
Q24
Which of the following represents the correct explicit call to a constructor of class A? class A{ int a; public: A(int i) { a = i; } }
Q25
Why constructors are efficient instead of a function init() defined by the user to initialize the data members of an object?