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, walkin 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 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) None of the mentionedView Answer

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

4. What is the output of this program?

#include <iostream>

usingnamespace 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;

return0;

}

a) 5, 5
b) 7, 3
c) 3, 7
d) None of the mentionedView Answer

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

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+=

7. What is the output of this program?

#include <iostream>

usingnamespace std;

class myclass

{

public:

int i;

myclass *operator->()

{returnthis;}

};

int main()

{

myclass ob;

ob->i =10;

cout<< ob.i<<" "<< ob->i;

return0;

}

a) 10 10
b) 11 11
c) error
d) runtime errorView 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) 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 mentionedView Answer

Answer:d
Explanation:None.

9. Operator overloading is
a) making c++ operator works with objects
b) giving new meaning to existing operator
c) making new operator
d) both a & bView Answer

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

10. What is the output of this program?

#include <iostream>

usingnamespace std;

ostream & operator<<(ostream & i, int n)

{

return i;

}

int main()

{

cout<<5<< endl;

cin.get();

return0;

}

a) 5
b) 6
c) error
d) runtime errorView Answer

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