This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Operator Functions”.
1. Pick the other name of operator function.
a) function overloading
b) operator overloading
c) member overloading
d) object overloading
View Answer
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
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
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.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class sample
{
public:
int x, y;
sample() {};
sample(int, int);
sample operator + (sample);
};
sample::sample (int a, int b)
{
x = a;
y = b;
}
sample sample::operator+ (sample param)
{
sample temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main ()
{
sample a (4,1);
sample b (3,2);
sample c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
a) 5, 5
b) 7, 3
c) 3, 7
d) 3, 5
View Answer
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:
$ g++ oper.cpp $ a.out 7, 3
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Box
{
double length;
double breadth;
double height;
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
};
int main( )
{
Box Box1;
Box Box2;
Box Box3;
double volume = 0.0;
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
Box3 = Box1 + Box2;
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
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 : 5260View Answer
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?
#include <iostream>
using namespace std;
class Integer
{
int i;
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const
{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer&
operator+=(const Integer& rv)
{
cout << "operator+=" << endl;
i += rv.i;
return *this;
}
};
int main()
{
int i = 1, j = 2, k = 3;
k += i + j;
Integer ii(1), jj(2), kk(3);
kk += ii + jj;
}
a)
operator+ operator+=
b)
operator+= operator+
c)
operator+ operator+
d)
operator+ operator=View Answer
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?
#include <iostream>
using namespace std;
class myclass
{
public:
int i;
myclass *operator->()
{return this;}
};
int main()
{
myclass ob;
ob->i = 10;
cout << ob.i << " " << ob->i;
return 0;
}
a) 10 10
b) 11 11
c) error
d) runtime error
View Answer
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
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
Explanation: Operator overloading is the way adding operation to the existing operators.
10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
ostream & operator<<(ostream & i, int n)
{
return i;
}
int main()
{
cout << 5 << endl;
cin.get();
return 0;
}
a) 5
b) 6
c) error
d) runtime error
View Answer
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.
- Apply for C++ Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs