250+ TOP MCQs on Operator Functions and Answers

This section on C++ questions and puzzles on “Operator Functions”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions and puzzles on “Operator Functions” along with answers, explanations and/or solutions:

1. Pick the other name of operator function.
a) function overloading
b) operator overloading
c) member overloading
d) object overloading
Answer: b
Clarification: Operator function means operation defined for that operator so if user defines a function for an operator then that is called operator overloading i.e. overloading already present operator function.

2. Which of the following operators can’t be overloaded?
a) ::
b) +
c) –
d) []
Answer: a
Clarification: :: operator cannot be overloaded because this operator operates on names rather than values and C++ has no syntax for writing codes that works on names than values so using syntax these operators cannot be overloaded.

3. How to declare operator function?
a) operator sign
b) operator
c) name of the operator
d) name of the class
Answer: a
Clarification: We have to declare the operator function by using the operator, operator sign. Example “operator +” where the operator is a keyword and + is the symbol need to be overloaded.

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

  1.     #include 
  2.     using namespace std;
  3.     class sample 
  4.     {
  5.         public:
  6.         int x, y;
  7.         sample() {};
  8.         sample(int, int);
  9.         sample operator + (sample);
  10.     };
  11.     sample::sample (int a, int b) 
  12.     {
  13.         x = a;
  14.         y = b;
  15.     }
  16.     sample sample::operator+ (sample param) 
  17.     {
  18.         sample temp;
  19.         temp.x = x + param.x;
  20.         temp.y = y + param.y;
  21.         return (temp);
  22.     }
  23.     int main () 
  24.     {
  25.         sample a (4,1);
  26.         sample b (3,2);
  27.         sample c;
  28.         c = a + b;
  29.         cout << c.x << "," << c.y;
  30.         return 0;
  31.     }

a) 5, 5
b) 7, 3
c) 3, 7
d) 3, 5
Answer: b
Clarification: In this program, we are adding the first number of a with first number of b by using operator function and also we are adding second number by this method also.
Output:

$ g++ oper.cpp
$ a.out
7, 3

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

  1.     #include 
  2.     using namespace std;
  3.     class Box
  4.     {   
  5.         double length;
  6.         double breadth;
  7.         double height;
  8.         public:
  9.         double getVolume(void)
  10.         {  
  11.             return length * breadth * height;
  12.         }
  13.         void setLength( double len )
  14.         {   
  15.             length = len;
  16.         }
  17.         void setBreadth( double bre )
  18.         {   
  19.             breadth = bre;
  20.         }
  21.         void setHeight( double hei )
  22.         {   
  23.             height = hei;
  24.         }
  25.         Box operator+(const Box& b)
  26.         {  
  27.             Box box;
  28.             box.length = this->length + b.length;
  29.             box.breadth = this->breadth + b.breadth;
  30.             box.height = this->height + b.height;
  31.             return box;
  32.         } 
  33.     };
  34.     int main( )
  35.     {  
  36.         Box Box1;
  37.         Box Box2;
  38.         Box Box3;
  39.         double volume = 0.0;
  40.         Box1.setLength(6.0);
  41.         Box1.setBreadth(7.0);
  42.         Box1.setHeight(5.0);
  43.         Box2.setLength(12.0);
  44.         Box2.setBreadth(13.0);
  45.         Box2.setHeight(10.0);
  46.         volume = Box1.getVolume();
  47.         cout << "Volume of Box1 : " << volume <<endl;
  48.         volume = Box2.getVolume();
  49.         cout << "Volume of Box2 : " << volume <<endl;
  50.         Box3 = Box1 + Box2;
  51.         volume = Box3.getVolume();
  52.         cout << "Volume of Box3 : " << volume <<endl;
  53.         return 0;
  54.     }

a)

   Volume of Box1 : 210
   Volume of Box2 : 1560
   Volume of Box3 : 5400

b)

   Volume of Box1 : 200
   Volume of Box2 : 1560
   Volume of Box3 : 5400

c)

   Volume of Box1 : 210
   Volume of Box2 : 1550
   Volume of Box3 : 5400

d)

   Volume of Box1 : 200
   Volume of Box2 : 1000
   Volume of Box3 : 5260

View Answer

Answer: a
Clarification: In this program, we finding the box3 area by adding box1 and box2.
Output:

$ g++ oper1.cpp
$ a.out
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

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

  1.     #include 
  2.     using namespace std;
  3.     class Integer 
  4.     {
  5.         int i;
  6.         public:
  7.         Integer(int ii) : i(ii) {}
  8.         const Integer
  9.         operator+(const Integer& rv) const 
  10.         {
  11.             cout << "operator+" << endl;
  12.             return Integer(i + rv.i);
  13.         }
  14.         Integer&
  15.         operator+=(const Integer& rv) 
  16.         {
  17.             cout << "operator+=" << endl;
  18.             i += rv.i;
  19.             return *this;
  20.         }
  21.     };
  22.     int main() 
  23.     {
  24.         int i = 1, j = 2, k = 3;
  25.         k += i + j;
  26.         Integer ii(1), jj(2), kk(3);
  27.         kk += ii + jj;
  28.     }

a)

   operator+
   operator+=

b)

   operator+=
   operator+

c)

   operator+
   operator+

d)

   operator+
   operator=

View Answer

Answer: a
Clarification: We are using two operator functions and executing them and the result is printed according to the order.
Output:

$ g++ oper2.cpp
$ a.out
operator+
operator+=

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

  1.     #include 
  2.     using namespace std;
  3.     class myclass
  4.     {
  5.         public:
  6.         int i;
  7.         myclass *operator->()
  8.         {return this;}
  9.     };
  10.     int main()
  11.     {
  12.         myclass ob;
  13.         ob->i = 10; 
  14.         cout << ob.i << " " << ob->i;
  15.         return 0;
  16.     }

a) 10 10
b) 11 11
c) error
d) runtime error
Answer: a
Clarification: In this program, -> operator is used to describe the member of the class and so we are getting this output.
Output:

$ g++ char4.cpp
$ a.out
10 10

8. Which of the following statements is NOT valid about operator overloading?
a) Only existing operators can be overloaded
b) The overloaded operator must have at least one operand of its class type
c) The overloaded operators follow the syntax rules of the original operator
d) None of the mentioned
Answer: b
Clarification: The overloaded operator must not have at least one operand of its class type.

9. Operator overloading is ___________
a) making c++ operator works with objects
b) giving new meaning to existing operator
c) making the new operator
d) adding operation to the existing operators
Answer: d
Clarification: Operator overloading is the way adding operation to the existing operators.

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

  1.     #include 
  2.     using namespace std;
  3.     ostream & operator<<(ostream & i, int n)
  4.     {
  5.         return i;
  6.     }
  7.     int main()
  8.     {
  9.         cout << 5 << endl;
  10.         cin.get();
  11.         return 0;
  12.     }

a) 5
b) 6
c) error
d) runtime error
Answer: c
Clarification: In this program, there will arise an ambiguous overload for 5.