a. To modify the data object whenever required.
b. To destroy an object
c. To initialize the data members of an object when it is created
d. To call private functions from the outer.
The correct option is (c)
Explanation:
Explanation: So A constructor is used in classes to initialize data objects or data members of class in order to avoid errors/segmentation faults.
a. A constructor that allows a user to move data from one object to another.
b. A constructor to check the whether to objects are equal or not.
c. A constructor to initialize an object with the values of another object.
d. A constructor to kill other copies of a given object.
Show Answer
The correct option is (c)
Explanation:
The Copy constructor allows the user to initialize an object with the values of another object instead
of the supplying the same set of values again to initialize the object.
3. What will be the output of the following C++ code?
#include < iostream >
#include < string >
using namespace std;
class A{
int a;
public:
void assignV(int j){
a = j;
}
int return_value(){
return a;
}
};
int main(int argc, char const *argv[])
{
A obj;
obj.assignV(20);
cout < < obj.return_value();
}
a. 19.
b. 0.
c. Compiler Error.
d. 20
Show Answer
The correct option is (d)
Explanation:
No Explanation
4. What happens if a user forgets to define a constructor inside a class?
a. Segmentation fault
b. Compiler provides a default constructor to avoid faults/errors
c. Objects are not created properly
d. Compiler Error
Show Answer
The correct option is (b)
Explanation:
The C++ compiler always provides a default constructor if one forgets to define a constructor inside a class.
5. How many types of constructors are there in C++ ?
a. 1
b. 2
c. 3
d. 4
Show Answer
The correct option is (c)
Explanation:
There are three types of constructors in C++ namely default, parameterized and copy constructor.
6. What is the use of destructors in Classes?
a. To destroy an object when the lifetime of an object ends.
b. To initialize the data members of an object when it is created.
c. To call private functions from the outer world
d. To modify the data whenever required
Show Answer
The correct option is (a)
Explanation:
Destructors in Classes are used to destroy an object after its lifetime is over i.e. to free resources occupied by that object.
7. When destructors are called?
a. Whenever a delete operator is used
b. Whenever a program ends
c. Whenever a function ends
d. All of the mentioned
Show Answer
The correct option is (d)
Explanation:
Destructors are called at the following time:
i) When at the end of the program to destroy objects declared in the main() or global scope.
ii) When at the end of a function to destroy objects declared at that function scope.
iii) When when user by himself tries to delete an object using the delete operator.
iv) When at the end of a block to destroy objects declared at that block scope.
8. Constructors are used to ____________ ?
a. construct the data members.
b. both initialize the objects & construct the data members
c. delete the objects
d. initialize the objects
Show Answer
The correct option is (d)
Explanation:
Once the object is declared means, the constructor are also declared by default.
9. How constructors are different from other member functions of the class?
a. Constructors do not return anything.
b. Constructors are automatically called when an object is created
c. Constructor has the same name as the class itself
d. All of the mentioned
Show Answer
The correct option is (d)
Explanation:
All the above mention are the reasons where constructor differs from other normal member functions of a class.
10. What is syntax of defining a destructor of class B ?
a. B(){}
b. ~B(){};
c. ~B(){}
d. B::B(){}
Show Answer
The correct option is (c)
Explanation:
A destructor starts with a ~(tilde) symbol, has the same name as the class.