C++ Programming Questions and Answers – Operator Overloading – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Operator Overloading – 2”.

1. What is a binary operator?
a) Operator that performs its action on a single operand
b) Operator that performs its action on two operand
c) Operator that performs its action on three operand
d) Operator that performs its action on any number of operands
View Answer

Answer: b
Explanation: As the word binary itself means 2 therefore a binary operator operates on two operands.

2. Which is the correct example of a binary operator?
a) ++
b) —
c) Dereferencing operator(*)
d) +
View Answer

Answer: d
Explanation: +(adding two operands) requires two operands whereas ++(increases value by 1), –(decreases value by 1) and *(dereferencing operator used for accessing value of pointers) requires only one operand.

3. Which is the correct example of a unary operator?
a) &
b) ==
c) —
d) /
View Answer

Answer: c
Explanation: &, == and / requires two operands whereas — requires only one operand, in general, it decreases the value of operand by 1.
advertisement
advertisement

4. Which is called ternary operator?
a) ?:
b) &&
c) |||
d) ===
View Answer

Answer: a
Explanation: ?: is called ternary operator because it separates three expressions. exp1 ? exp2 : exp3.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
#include <iostream>
#include <string>
using namespace std;
class complex
{
 
	int i;
	int j;
        public:
	complex(int a, int b)
        {
		i = a;
		j = b;
	}
 
	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}
 
	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	complex c3 = c1 + c2;
	c3.show();
	return 0;
}

a) 4 + i6
b) 2 + i2
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: In the operator overloaded function we are trying to call default constructor of the class complex but as we have overridden the constructor by our constructor therefore the default constructor cannot be called hence the program gives error.
advertisement

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

#include <iostream>
#include <string>
using namespace std;
class complex
{
	int i;
	int j;
        public:
	complex(){}
	complex(int a, int b)
        {
	        i = a;
		j = b;
	}
 
	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}
 
	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	complex c3 = c1 + c2;
	c3.show();
	return 0;
}

a) Complex Number: 4 + i6
b) Complex Number: 2 + i2
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: As we have defined in the class complec that when we add the two objects of the class complex then add those two complex numbers and show() displays that result.
advertisement

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

#include <iostream>
#include <string>
using namespace std;
class complex
{
	int i;
	int j;
      public:
	complex(){}
	complex(int a, int b)
        {
		i = a;
		j = b;
	}
	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}
 
	void operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		temp.show_poss();
	}
 
	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}
 
	void show_poss(){
		cout<<"Your result after addition will be: "<<i<<" + i"<<j<<endl;
	}
};
 
int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	c1 + c2;
	return 0;
}

a) Complex Number: 4 + i6
b) Complex Number: 2 + i2
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: Each operator function can be defined only once in a class. So as in this program we are trying to define two functions for operator ‘+’ which is not allowed in C++ therefore program gives error.

8. Given the following C++ code. How would you define the < operator for Box class so that when boxes b1 and b2 are compared in if block the program gives correct result?

#include <iostream>
#include <string>
using namespace std;
class Box
{
	int capacity;
     public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};
 
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"Box 2 has large capacity.";
	}
	else{
		cout<<"Box 1 has large capacity.";
	}
	return 0;
}

a)

bool operator<(Box b)
{
	return this->capacity < b.capacity ? true : false;
}

b)

bool operator<(Box b)
{
	return this->capacity > b.capacity ? true : false;
}

c)

bool operator<(Box b)
{
	return  b1 > b2 ? true : false;
}

d)

bool operator<(Box b)
{
	return this < b ? true : false;
}
View Answer
Answer: a
Explanation: As we need to give the result after comparing the capacity of two boxes. We use < operator and as this is the first operand and second operand is passed so we need to do this->capacity < b.capacity (passed object) to make the program run.
 
 

9. Which is the correct statement about operator overloading?
a) Only arithmetic operators can be overloaded
b) Only non-arithmetic operators can be overloaded
c) Precedence of operators are changed after overlaoding
d) Associativity and precedence of operators does not change
View Answer

Answer: d
Explanation: Both arithmetic and non-arithmetic operators can be overloaded. The precedence and associativity of operators remains the same after and before operator overloading.

10. Pick the incorrect statements out of the following.
a) Operator overloading does not disturbs the precedence of operators
b) Arity of operators can be changed using operator overloading
c) No new operators can be created
d) All of the mentioned
View Answer

Answer: b
Explanation: Arity means a number of operands an operator requires to perform its action and operator overloading does not changes the arity of any operator.

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

#include <iostream>
#include <string>
using namespace std;
class Box
{
	int capacity;
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
 
};
 
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	return 0;
}

a) Error
b) Segmentation fault
c) 4
d) No output
View Answer

Answer: a
Explanation: As constructors are defined private and we know objects cannot access private objects therefore program gives error. Also no class should have private constructor.

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

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
	bool operator<(Box b){
		return this->capacity < b.capacity ? true : false;
	}
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
 
};
 
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"Box 2 has large capacity.";
	}
	else{
		cout<<"Box 1 has large capacity.";
	}
	return 0;
}

a) Error
b) Segmentation fault
c) Box 2 has large capacity
d) No output
View Answer

Answer: a
Explanation: As the operator overloaded function defined is private therfore on comparison the function cannot be called from outside therefore the program gives error.

13. Which operator should be overloaded in the following code to make the program error free?

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 == b2){
		cout<<"Equal";
	}
	else{
		cout<<"Not Equal";
	}
	return 0;
}

a) +
b) ==
c) =
d) ()
View Answer

Answer: b
Explanation: As in the if block we are trying to compare two Box objects and no method is defined to tell compiler how the comparison should be done bwteen these two objects. Hence we need to overload the == operator.

14. Give the function prototype of the operator function which we need to define in this program so that the program has no errors.

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};
 
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 == b2){
		cout<<"Equal";
	}
	else{
		cout<<"Not Equal";
	}
	return 0;
}

a) bool operator==();
b) bool operator==(Box b){}
c) bool operator==(Box b);
d) Box operator==();
View Answer

Answer: c
Explanation: In this question we are asked to give the function prototypr not the function definition so the answer should not contain {} braces. The correct overloaded function is bool operator==(Box b);

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

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
	bool operator<(Box b){
		return b.capacity < this->capacity? true : false;
	}
};
 
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"B1's capacity is small";
	}
	else{
		cout<<"B2's capacity is small";
	}
	return 0;
}

a) B1's capacity is small
b) B2's capacity is small
c) Error
d) Segmentation fault
View Answer

Answer: b
Explanation: Though the b1's capacity is small the program prints B2's capacity is small because in the < operator overloaded function we are checking B2's capacity < B1's capacity which is false therefore the else is executed.

Sanfoundry Global Education & Learning Series - C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.