Question: 6 -
Which among the following is called first, automatically, whenever an object is created?
-
New
-
Class
-
Trigger
-
Constructor
Answer:
Constructor
Solution:
Constructors are the member functions which are called automatically whenever an object is created. It is a mandatory functions to be called for an object to be created as this helps in initializing the object to a legal initial value for the class.
Constructors are the member functions which are called automatically whenever an object is created. It is a mandatory functions to be called for an object to be created as this helps in initializing the object to a legal initial value for the class.
Question: 7 -
Destructors __________ for automatic objects if the program terminates with a call to function exit or function abort
-
Are created
-
Are called
-
Are not called
-
Are inherited
Answer:
Are not called
Solution:
Destructors Are not called for automatic objects if the program terminates with a call to function exit or function abort
Destructors Are not called for automatic objects if the program terminates with a call to function exit or function abort
Question: 8 -
What will be the output of the following program?#include <iostream>
using namespace std;
class LFC {
LFC() { cout << "Constructor called"; }
};
int main()
{
LFC t1;
return 0;
}
-
Runtime Error
-
Constructor called
-
Compiler Error
-
destructor for id 1
Answer:
Compiler Error
Solution:
By default all members of a class are private. Since no access specifier is there for find(), it becomes private and it is called outside the class when t1 is constructed in main.
By default all members of a class are private. Since no access specifier is there for find(), it becomes private and it is called outside the class when t1 is constructed in main.
Question: 9 -
Which contructor function is designed to copy object of same class type?
-
Copy constructor
-
Create constructor
-
Dynamic constructor
-
Object constructor
Answer:
Copy constructor
Solution:
Copy constructor function is designed to copy object of same class type.
Copy constructor function is designed to copy object of same class type.
Question: 10 -
Which of the following is true about constructors. i) They cannot be virtual ii) They cannot be private. iii) They are automatically called by new operator.
-
i & iii
-
ii & iii
-
All i,ii,iii
-
i & ii
Answer:
i & iii
Solution:
i) True: Virtual constructors don't make sense, it is meaningless to the C++ compiler to create an object polymorphically. ii) False: Constructors can be private
i) True: Virtual constructors don't make sense, it is meaningless to the C++ compiler to create an object polymorphically. ii) False: Constructors can be private