0

Constructor and Destructor (NCO)

Description: constructor
Number of Questions: 15
Created by:
Tags: constructor OOPs
Attempted 0/15 Correct 0 Score 0

Which of the following is not a characteristic of destructor function used in C++?

  1. Destructor must have the same name as the class preceded by a tilde (~).

  2. Destructor has a return type.

  3. Destructor cannot take arguments; so it cannot be overloaded.

  4. There can be only one destructor in each class.

  5. Destructor cannot be inherited.


Correct Option: B
Explanation:

This is not the correct characteristic of the destructor function as it cannot return any value.

Which of the following types of constructor is a special member function that is invoked by the C++ compiler without any arguments for initialising the objets of a class?

  1. Parameterized constructor

  2. Copy constructor

  3. Default constructor

  4. Constructor overloading

  5. Destructor


Correct Option: C
Explanation:

This type of constructor is a special member function that is invoked by the C++ compiler without any arguments for initialising the objects of a class.

A constructor function can be declared in ____________ section only.

  1. private

  2. protected

  3. public

  4. private protected

  5. default


Correct Option: C
Explanation:

This is the correct option as constructor function should be declared in the public section only so that it can be accessible outside the class.

Consider the following C++ code: class Name{ int x,y; public: Name(int a, b=10) { x=a; }};

If an object of this class is created as Name obj1(100); then what would be the values assigned to x and y?

  1. 100 and 10

  2. 100 and 200

  3. 10 and 100

  4. 200 and 100

  5. No values are assigned.


Correct Option: A
Explanation:

This is correct assignment as with the object creation, only one argument is passed and another value assigned is the default argument.

What is the main purpose of using default constructor in a C++ program?

  1. It is compulsorily required in every program.

  2. It is used to access data members of a class.

  3. It is used to initialise the object of a class.

  4. It is used to avoid any confusion when overloaded constructors are used.

  5. It is of no use.


Correct Option: C
Explanation:

This is the correct option as default constructor is used to initialise the object of a class.

Which of the following special member functions is executed when an object of that class is destroyed?

  1. Copy constructor

  2. Default constructor

  3. Constructor with default arguments

  4. Destructor

  5. Parameterized constructor


Correct Option: D
Explanation:

This is a special member function that is executed when an object of that class is destroyed.

Which of the following is not a characteristic of a constructor?

  1. It should be declared in the public section.

  2. It can be invoked automatically when the objects are created.

  3. It cannot return any value not even void type.

  4. Constructors can be virtual.

  5. Constructors can make implicit calls to the operators new and delete when memory allocation is required.


Correct Option: D
Explanation:

This is not the characteristic of a constructor as constructors cannot be virtual because virtual keyword creates a single copy of the function to derived class.

Which of the following variables can be used as an argument to copy constructor?

  1. Static variable

  2. Pointer variable

  3. Reference variable

  4. new

  5. delete


Correct Option: C
Explanation:

This variable can be used as an argument to the copy constructor as argument can only be passed by reference in a copy constructor.

Which of the following functions is created by the compiler when no constructor is created or supplied by the user?

  1. Friend function

  2. Inline function

  3. Default constructor

  4. this pointer

  5. Static member function


Correct Option: C
Explanation:

A default constructor is created to create and initialise objects of the same class.

Which of the following types of constructor function should be used to initialise the various data elements of different objects with different values when they are created?

  1. Default constructor

  2. Parameterized constructor

  3. Copy constructor

  4. Overloaded constructor

  5. Constructor with default value


Correct Option: B
Explanation:

This type of constructor function should be used to initialise the various data elements of different objects with different values when they are created.

Consider the following C++ code and select its output from the given options:

class A{ int a; public: A(int x) {cout<< Base Constructor:<

  1. Derived constructor:Base constructor:

  2. Base constructor:Derived constructor:

  3. Default constructor:Base constructor:

  4. Derived constructor:Default constructor:

  5. Base constructor:Base constructor:


Correct Option: B
Explanation:

This is the correct output of the above C++ code. If the constructor function is used in both base and derived classes, then always the base constructor function would be executed first and then derived constructor.

What do you think would be the main advantage of creating constructor function in a class?

  1. It avoids creating private members in a class.

  2. Constuctor functions are automatically invoked when objects are created.

  3. It avoids creating friend function in a class.

  4. It avoids creating destructor function in a class.

  5. It avoids creating pointers to objects.


Correct Option: B
Explanation:

This is the main advantage of using a constructor function in a class.

Which of the following is one of the special properties of a destructor function?

  1. There can be only one destructor function for a class.

  2. Destructor functions are not invoked automatically when the objects are destroyed.

  3. Arguments can be provided to a destructor.

  4. A destructor function can return value.

  5. It can be declared in the private section of a class.


Correct Option: A
Explanation:

This is the only one special property of a destructor function.

Consider the following C++ code:

Number num1 = Number(100,200);

If Number refers to a class, then this method of calling constructor function is called

  1. explicit call

  2. implicit call

  3. pointer call

  4. reference call

  5. call by value


Correct Option: A
Explanation:

This method of calling constructor function is called as explicit call.

When two or more constructor functions with the same name but with different function signatures act in the same class, it is called

  1. default constructor

  2. copy constructor

  3. constructor overloading

  4. parameterized constructor

  5. exit()


Correct Option: C
Explanation:

When two or more constructor functions with the same name but with different function signatures act in the same class, it is called constructor overloading.

- Hide questions