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
Clarification: 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) +
Clarification: +(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) /
Clarification: &, == and / requires two operands whereas — requires only one operand, in general, it decreases the value of operand by 1.

4. Which is called ternary operator?
a) ?:
b) &&
c) |||
d) ===
Clarification: ?: is called ternary operator because it separates three expressions. exp1 ? exp2 : exp3.

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

```#include
#include
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
Clarification: 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.

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

```#include
#include
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
Clarification: 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.

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

```#include
#include
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(){
}
};

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
Clarification: 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
#include
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 operatorcapacity
b)
bool operatorcapacity > b.capacity ? true : false;
}
c)
bool operator b2 ? true : false;
}
d)
bool operator
View AnswerAnswer: aClarification: 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 overloadedb) Only non-arithmetic operators can be overloadedc) Precedence of operators are changed after overlaodingd) Associativity and precedence of operators does not changeAnswer: dClarification: 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 operatorsb) Arity of operators can be changed using operator overloadingc) No new operators can be createdd) All of the mentionedAnswer: bClarification: 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
#include
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) Errorb) Segmentation faultc) 4d) No outputAnswer: aClarification: 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
#include
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) Errorb) Segmentation faultc) Box 2 has large capacityd) No outputAnswer: aClarification: 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
#include
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) ()Answer: bClarification: 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
#include
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==();Answer: cClarification: 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
#include
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 smallb) B2's capacity is smallc) Errord) Segmentation faultAnswer: bClarification: 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.
Global Education & Learning Series - C++ Programming Language.
```