Test Series - cpp

Test Number 41/102

Q: What is the role of a constructor in classes?
A. To modify the data 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 world
Solution: A constructor is used in classes to initialize data members of class in order to avoid errors/segmentation faults.
Q: Why constructors are efficient instead of a function init() defined by the user to initialize the data members of an object?
A. Because user may forget to call init() using that object leading segmentation fault
B. Because user may call init() more than once which leads to overwriting values
C. Because user may forget to define init() function
D. All of the mentioned
Solution: We cannot use init() because as mentioned in options that user may forget to initialize the members which will lead to a segmentation fault. Also if the user calls the init() function more than once it may overwrite the values and may result into disastrous results. Also if any user forgets to define init() function then no object will be initialized whereas if any constructor is not defined in any class the class provides a default constructor for initialization.
Q: What is a copy constructor?
A. A constructor that allows a user to move data from one object to another
B. A constructor to initialize an object with the values of another object
C. A constructor to check the whether to objects are equal or not
D. A constructor to kill other copies of a given object.
Solution: Copy constructor allows the user to initialize an object with the values of another object instead of supplying the same set of values again to initialize the object.
Q: What happens if a user forgets to define a constructor inside a class?
A. Error occurs
B. Segmentation fault
C. Objects are not created properly
D. Compiler provides a default constructor to avoid faults/errors
Solution: The C++ compiler always provides a default constructor if one forgets to define a constructor inside a class.
Q: How many parameters does a default constructor require?
A. 1
B. 2
C. 0
D. 3
Solution: A default constructor does not require any parameters for object creation that’s why sometimes we declare an object without any parameters.
Q: How constructors are different from other member functions of the class?
A. Constructor has the same name as the class itself
B. Constructors do not return anything
C. Constructors are automatically called when an object is created
D. All of the mentioned
Solution: All the above mention are the reasons where constructor differs from other normal member functions of a class.
Q: How many types of constructors are there in C++?
A. 1
B. 2
C. 3
D. 4
Solution: There are three types of constructors in C++ namely default, parameterized and copy constructor.
Q: What is the role of destructors in Classes?
A. To modify the data whenever required
B. To destroy an object when the lifetime of an object ends
C. To initialize the data members of an object when it is created
D. To call private functions from the outer world
Solution: Destructors are used in Classes to destroy an object after its lifetime is over i.e. to free resources occupied by that object.
Q: What is syntax of defining a destructor of class A?
A. A(){}
B. ~A(){}
C. A::A(){}
D. ~A(){};
Solution: A destructor starts with a ~(tilde) symbol, has the same name as the class.
Q: When destructors are called?
A. When a program ends
B. When a function ends
C. When a delete operator is used
D. All of the mentioned
Solution: Destructors are called at the following time:
i) at the end of the program to destroy objects declared in the main() or global scope.
ii) at the end of a function to destroy objects declared at that function scope.
iii) when user by himself tries to delete an object using the delete operator.
iv) at the end of a block to destroy objects declared at that block scope.

You Have Score    /10