Operator Functions(C++ Programming ) Questions and Answers

Question 1.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
ostream & operator
  1.    5
  2.    6
  3.    error
  4.    runtime error
Explanation:-
Answer: Option C. -> error

In this program, there will arise an ambiguous overload for 5.



Question 2.

Operator overloading is


  1.    making c++ operator works with objects
  2.    giving new meaning to existing operator
  3.    making new operator
  4.    both a & b
Explanation:-
Answer: Option D. -> both a & b

Operator overloading is the way adding operation to the existing operators.



Question 3.


What is the output of this program?


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
  1.    10 10
  2.    11 11
  3.    error
  4.    runtime error
Explanation:-
Answer: Option A. -> 10 10

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



Question 4.

Which of the following statements is NOT valid about operator overloading?


  1.    Only existing operators can be overloaded.
  2.    Overloaded operator must have at least one operand of its class type.
  3.    The overloaded operators follow the syntax rules of the original operator.
  4.    None of the mentioned
Explanation:-
Answer: Option D. -> None of the mentioned

None.



Question 5.



1.
#include
2.
using namespace std;
3.
class Integer
4.
{
5.
int i;
.
public:
7.
Integer(int ii) : i(ii) {}
8.
const Integer
9.
operator+(const Integer& rv) const
10.
{
11.
cout
  1.    10 10
  2.    11 11
  3.    error
  4.    runtime error
Explanation:-
Answer: Option A. -> 10 10

Answer: (a)
Explanation:We are using two operator functions and executing them and result is printed according to the order.
Output:
$ g++ oper2.cpp
$ a.out
operator+
operator+=



Question 6.


What is the output of this program?


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
  1.    5, 5
  2.    7, 3
  3.    3, 7
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> 7, 3

In this program, we are adding the first number of a with first number
of b by using opertor

function and also we are adding second number by
this method also.
Output:
$ g++ oper.cpp
$ a.out
7, 3



Question 7.

How to declare operator function?


  1.    operator operator sign
  2.    operator
  3.    operator sign
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> operator operator sign

We have to declare the operator function by using operator, operator
sign. Example "operator +"

where operator is a keyword and + is the
symbol need to be overloaded.



Question 8.

Which of the following operators can't be overloaded?


  1.    ::
  2.    +
  3.    -
  4.    []
Explanation:-
Answer: Option A. -> ::

None.



Question 9.

Pick the other name of operator function.


  1.    function overloading
  2.    operator overloading
  3.    member overloading
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> operator overloading

None.