github

Made bySaurav Hathi

MCQ Test C/C++: Operator Overloading

Q1:

In C++, when an operator is given user-defined meaning, then it is called ____________

Tags:
Section:
Core Programming
Options:
Q2:

Which of the following operators can’t be overloaded in C++?

Tags:
Section:
Core Programming
Options:
Q3:

Correct way of declaring operator overloaded function in C++ is:-

Tags:
Section:
Core Programming
Options:
Q4:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q5:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q6:

What is the output of this C++ program?

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

}

Tags:
Section:
Core Programming
Options:
Q7:

What is the output of this program?

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

}

Tags:
Section:
Core Programming
Options:
Q8:

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

Tags:
Section:
Core Programming
Options:
Q9:

Operator overloading in C++ is ____________

Tags:
Section:
Core Programming
Options:
Q10:

What is the output of this C++ program?

#include <iostream>

using namespace std;

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

{

return i;

}

int main()

{

cout << 5 << endl;

cin.get();

return 0;

}

Tags:
Section:
Core Programming
Options: