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 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.
C++ questions and puzzles on “Operator Functions” along with answers, explanations and/or solutions:
Q 1. Pick the other name of operator function.
A. function overloading
B. member overloading
C. operator overloading
D. object overloading
Show Answer
Answer:-C. operator overloadingExplanation
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.Q 2. Which of the following operators can’t be overloaded?
A. ::
B. +
C. –
D. []
Show 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.Q 3. How to declare operator function?
A. operator
B. operator sign
C. name of the operator
D. name of the class
Show Answer
Answer:-B. operator signExplanation
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.Q 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. 3, 3
B. 3, 5
C. 5, 7
D. 7, 3
Show Answer
Answer:-D. 7, 3Explanation
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, 3Q 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 : 5260
Show Answer
Answer:-A. Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400Explanation
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 : 5400Q 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=
Show Answer
Answer:-A. operator+ operator+=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+=Q 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
Show Answer
Answer:-A. 10 10Explanation
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 10Q 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
Show Answer
Answer:-B. The overloaded operator must have at least one operand of its class typeExplanation
The overloaded operator must not have at least one operand of its class type.Q 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
Show Answer
Answer:-D. adding operation to the existing operatorsExplanation
Operator overloading is the way adding operation to the existing operators.Q 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
Leave a Reply