My Report

C++ Operator Test – 3


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What is a binary operator?

2. Which is the correct example of a unary operator?

3. Which is called ternary operator?

4. Which operator should be overloaded in the following code to make the program error free?

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};
int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 == b2){
		cout<<"Equal";
	}
	else{
		cout<<"Not Equal";
	}
	return 0;
}

5. Give the function prototype of the operator function which we need to define in this program so that the program has no errors.

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};

int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 == b2){
		cout<<"Equal";
	}
	else{
		cout<<"Not Equal";
	}
	return 0;
}

6. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class complex
{

	int i;
	int j;
        public:
	complex(int a, int b)
        {
		i = a;
		j = b;
	}

	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}

	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}
};

int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	complex c3 = c1 + c2;
	c3.show();
	return 0;
}

7. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class complex
{
	int i;
	int j;
        public:
	complex(){}
	complex(int a, int b)
        {
	        i = a;
		j = b;
	}

	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}

	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}
};

int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	complex c3 = c1 + c2;
	c3.show();
	return 0;
}

8. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
	bool operator<(Box b){
		return this->capacity < b.capacity ? true : false;
	}
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}

};

int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"Box 2 has large capacity.";
	}
	else{
		cout<<"Box 1 has large capacity.";
	}
	return 0;
}

9. Which is the correct statement about operator overloading?

10. Given the following C++ code. How would you define the < operator for Box class so that when boxes b1 and b2 are compared in if block the program gives correct result?

#include <iostream>
#include <string>
using namespace std;
class Box
{
	int capacity;
     public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
};

int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"Box 2 has large capacity.";
	}
	else{
		cout<<"Box 1 has large capacity.";
	}
	return 0;
}

11. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class complex
{
	int i;
	int j;
      public:
	complex(){}
	complex(int a, int b)
        {
		i = a;
		j = b;
	}
	complex operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		return temp;
	}

	void operator+(complex c)
        {
		complex temp;
		temp.i = this->i + c.i;
		temp.j = this->j + c.j;
		temp.show_poss();
	}

	void show(){
		cout<<"Complex Number: "<<i<<" + i"<<j<<endl;
	}

	void show_poss(){
		cout<<"Your result after addition will be: "<<i<<" + i"<<j<<endl;
	}
};

int main(int argc, char const *argv[])
{
	complex c1(1,2);
	complex c2(3,4);
	c1 + c2;
	return 0;
}

12. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class Box
{
	int capacity;
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}

};

int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	return 0;
}

13. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class Box{
	int capacity;
public:
	Box(){}
	Box(double capacity){
		this->capacity = capacity;
	}
	bool operator<(Box b){
		return b.capacity < this->capacity? true : false;
	}
};

int main(int argc, char const *argv[])
{
	Box b1(10);
	Box b2 = Box(14);
	if(b1 < b2){
		cout<<"B1's capacity is small";
	}
	else{
		cout<<"B2's capacity is small";
	}
	return 0;
}

14. Pick the incorrect statements out of the following.

15. Which is the correct example of a binary operator?


 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.