C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Operator Overloading – 2”.
Q 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
Show Answer
Answer:-B. Operator that performs its action on two operandExplanation
As the word binary itself means 2 therefore a binary operator operates on two operands.Q 2. Which is the correct example of a binary operator?
A. ++
B. —
C. Dereferencing operator(*)
D. +
Show 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.Q 3. Which is the correct example of a unary operator?
A. &
B. ==
C. —
D. /
Show Answer
Answer:-C. —Explanation
&, == and / requires two operands whereas — requires only one operand, in general, it decreases the value of operand by 1.Q 4. Which is called ternary operator?
A. ?:
B. &&
C. |||
D. ===
Show Answer
Answer:-A. ?:Explanation
?: is called ternary operator because it separates three expressions. exp1 ? exp2 : exp3.Q 5. 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(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
Show Answer
Answer:-C. ErrorExplanation
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.Answer: c
Explanation:
Q 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
Show Answer
Answer:-A. Complex Number: 4 + i6Explanation
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.Answer: a
Explanation:
Q 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. Segmentation fault
D. Error
Show Answer
Answer:-D. ErrorExplanation
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.Q 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;
}
Show Answer
Answer:-A. bool operator<(Box b) { return this->capacity < b.capacity ? true : false; }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.Q 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
Show Answer
Answer:-D. Associativity and precedence of operators does not changeExplanation
Both arithmetic and non-arithmetic operators can be overloaded. The precedence and associativity of operators remains the same after and before operator overloading.Q 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
Show Answer
Answer:-B. Arity of operators can be changed using operator overloadingExplanation
Arity means a number of operands an operator requires to perform its action and operator overloading does not changes the arity of any operator.Q 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
Show Answer
Answer:-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.Answer: a
Explanation:
Q 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
Show Answer
Answer:-A. ErrorExplanation
As the operator overloaded function defined is private therfore on comparison the function cannot be called from outside therefore the program gives error.Q 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. ()
Show 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.Q 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==(Box b);
B. bool operator==(Box b){}
C. bool operator==();
D. Box operator==();
Show Answer
Answer:-A. bool operator==(Box b);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);Q 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
Leave a Reply