250+ TOP MCQs on Constructors and Destructors – 3 and Answers

C++ Programming written test Questions & Answers on “Constructors and Destructors – 3”.

Pour autant, je suis sûr qu'une personne de ma famille voudrait m'aider avec une consultation médicale en ligne et je sais qu'elle m'accompagne pour ça. Viagra and levitra are also https://thepaintbrush.net/field-trips marketed by several other companies. Now, 3 weeks after using steroids my acne is much better.

If you notice a change in your gums or gums and tongue, then you have to see your dentist to make sure there is nothing more wrong. If you want to try the tadalafil 20 mg for the buy provigil online from canada first time, it is important that you take it with a doctor or a medical professional before you start the use of the tadalafil 20 mg. Do all of your medical reviews on the internet to find a reliable doctor to get your doxycycline tablet.

1. 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
Answer: d
Clarification: 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.

2. 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
Answer: d
Clarification: 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.

3. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A a;
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
Answer: c
Clarification: No constructor should be made private because objects need to call them and as by default all the members of a class are private therefore constructor defined in the above program is private which is wrong therefore the compiler gives the error.

4. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
 
public:
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A *a;
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
Answer: b
Clarification: As we have declared a pointer variable for class A but we have not initialized the memory to that pointer and until the memory is not initialized the constructor for the pointer variable will not be called hence nothing is printed on the screen.

5. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
public:
	int a;
};
int main(int argc, char const *argv[])
{
	A a1 = {10};
	A a2 = a1;
	cout<<a1.a<<a2.a;
	return 0;
}

a) 1010
b) 87368746
c) Error
d) Segmentation fault
Answer: a
Clarification: Although the declaration of object a1 looks erroneous but actually it is acceptable by the C++ compiler to take values for class objects as mentioned above. Next value of a1 is copied to a2 hence 1010 is printed.
Output:
$ ./a.out
1010

6. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
public:
	int a;
	A(int a){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<a2.a;
	return 0;
}

a) 10
b) Compile time error
c) Run-time error
d) No output
Answer: b
Clarification: The declaration of object a1 needs a constructor without any arguments which is not available in the class as we have overwritten the default constructor, therefore, the program gives the error.

7. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
public:
	int a;
	A(int a=0){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<a2.a;
	return 0;
}

a) 010
b) 100
c) 001
d) Error
Answer: a
Clarification: As constructor is accepting default parameter therefore the declaration of a1 and a2 both are valid hence the program runs successfully.

8. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A *a1 = (A*)malloc(sizeof(A));
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
Answer: b
Clarification: Unlike new malloc never calls the constructor of a class hence when we are assigning memory to an object of class A using malloc constructor is not called hence nothing is printed.

9. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called";
	}
} a;
int main(int argc, char const *argv[])
{
	return 0;
}

a) Constructor called
b) Nothing printed
c) Error
d) Segmentation fault
Answer: a
Clarification: In this program, we have defined a global variable an outside main function for which constructor will be called hence the output is printed.

10. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor calledn";
	}
} a;
int main(int argc, char const *argv[])
{
	cout<<"Main functionn";
	return 0;
}

a)

Constructor called
Main function

b)

Main function
Constructor called

c) Error
d) Segmentation fault
Answer: a
Clarification: In this program, we have defined a global variable an outside main function for which constructor will be called. Now as a is a global variable, therefore, the call for constructor will be before the main function hence constructor called will be printed before the Main function.

11. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
	A(){
		cout<<"A's Constructor calledn";
	}
};
class B
{
public:
	A a;
	B(){
		cout<<"B's constructor calledns";
	}
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

a)

A's Constructor called
B's constructor called

b)

B's Constructor called
A's constructor called

c) Error
d) Segmentation fault
Answer: c
Clarification: As constructor of class A is defined private and we are trying to define an object of class A which cannot call this constructor as it is private therefore the program gives an error.

12. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
	A(){
		cout<<"A's Constructor calledn";
	}
	friend class B;
};
class B
{
public:
	A a;
	B(){
		cout<<"B's constructor calledns";
	}
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

a)

A's Constructor called
B's constructor called

b)

B's Constructor called
A's constructor called

c) Error
d) Segmentation fault
Answer: a
Clarification: Now still the constructor of a class is private but class B is friend class of A hence it can access the private members of class A and as in the above program we defining an object of class A in class B only, therefore, the program runs fine.

13. What will be the output of the following C++ code?

#include   
using namespace std;
class A{
	~A(){}
};
class B
{
public:
	A a;
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

a)

A's Constructor called
B's constructor called

b)

B's Constructor called
A's constructor called

c) Error
d) Segmentation fault
Answer: c
Clarification: In this program, the destructor of class A is private therefore the destructor for object a cannot be called hence the program gives an error.

14. 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
Answer: d
Clarification: A class is allowed to have only one destructor. Therefore there is no point of destructor overloading.

15. 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
Answer: a
Clarification: Destructors can be virtual. They are used to destroy objects. Only one destructor is allowed per class. Destructor definition starts with a tilde(~).

Leave a Reply

Your email address will not be published. Required fields are marked *