cpp constructor destructor exercise

C++ exercise

1. What is the role of a constructor in classes ?

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.

Show Answer

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.

2. What is a copy constructor?

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.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise