C++ Programming Questions and Answers – Operator Functions

This section on C++ questions and puzzles focuses 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
View Answer

Answer: b
Explanation: 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) []
View Answer

Answer: a
Explanation: :: 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
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

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

  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: 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:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ oper.cpp
$ a.out
7, 3

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

advertisement
  1.     #include <iostream>
  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
advertisement

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
Explanation: 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 <iostream>
  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
Explanation: 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 <iostream>
  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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: Operator overloading is the way adding operation to the existing operators.

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

  1.     #include <iostream>
  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
View Answer

Answer: c
Explanation: In this program, there will arise an ambiguous overload for 5.

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.