Test Series - cpp

Test Number 42/102

Q: What is the difference between constructors and destructors?
A. They have a different function name
B. Constructors does not have return type whereas destructors do have
C. Constructors allow function parameters whereas destructors do not
D. Constructors does not function parameters
Solution: Both the constructors and destructors have the same function name and both of them do not have return type but constructors allow function parameters whereas destructors do not.
Q: How many Destructors are allowed in a Class?
A. 1
B. 2
C. 3
D. 1
Solution: A class in C++ allows only one destructor, which is called whenever the lifetime of an object ends.
Q: Which of the following constructors are provided by the C++ compiler if not defined in a class?
A. Default constructor
B. Assignment constructor
C. Copy constructor
D. All of the mentioned
Solution: 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.
Q: 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
Solution: 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: 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
Solution: A class is allowed to have only one destructor. Therefore there is no point of destructor overloading.
Q: Which of the following is correct?
A. Destructors can be virtual
B. There can be more than one destructor in a class
C. Destructor definition starts with !
D. Destructor is used to initialize objects
Solution: Destructors can be virtual. They are used to destroy objects. Only one destructor is allowed per class. Destructor definition starts with a tilde(~).
Q: Where is the derived class is derived from?
A. derived
B. base
C. both derived & base
D. class
Solution: Because derived inherits functions and variables from base.
Q: Pick out the correct statement.
A. A derived class’s constructor cannot explicitly invokes its base class’s constructor
B. A derived class’s destructor cannot invoke its base class’s destructor
C. A derived class’s destructor can invoke its base class’s destructor
D. A derived class’s destructor can invoke its base & derived class’s destructor
Solution: Destructors are automatically invoked when an object goes out of scope or when a dynamically allocated object is deleted. Inheritance does not change this behavior. This is the reason a derived destructor cannot invoke its base class destructor.
Q: Which of the following can derived class inherit?
A. members
B. functions
C. both members & functions
D. classes
Solution: Both data members and member functions are inherited by derived class in C++.
Q: Which constructor will initialize the base class data member?
A. derived class
B. base class
C. class
D. derived & base class
Solution: Because it is having the proper data set to initialize, Otherwise it will throw an error.

You Have Score    /10