C++ Programming Questions and Answers – Operator Overloading – 1

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Operator Overloading – 1”.

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
    public:
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
int A::a = 5;
 
int main(int argc, char const *argv[])
{
	A a;
	return 0;
}

a) Error as a private member a is referenced outside the class
b) Segmentation fault
c) No output
d) Program compiles successfully but gives run-time error
View Answer

Answer: c
Explanation: As every static member must be initialized and we have initialized variable ‘a’ so no run time error. Also as variable ‘a’ is a static member and is referenced using the class for initialization therefore no compiler error.
advertisement
advertisement

2. What happens when objects s1 and s2 are added?

string s1 = "Hello";
string s2 = "World";
string s3 = (s1+s2).substr(5);

a) Error because s1+s2 will result into string and no string has substr() function
b) Segmentation fault as two string cannot be added in C++
c) The statements runs perfectly
d) Run-time error
View Answer

Answer: c
Explanation: string is class in C++, therefore when we do (s1+s2) a temporary object is created which stores the result of s1+s2 and then that object calls the function substr() and as that is an object of string class hence substr is a callable function for that temporary string object.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
    public:
	A()
        {
		cout<<"Object of A is created\n";
	}
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
class B
{
    public:
};
 
int main(int argc, char const *argv[])
{
	A a1, a2;
	A a3 = a1 + a2;
	return 0;
}

a) Runs perfectly
b) Run-time Error
c) Segmentation fault
d) Compile-time Error
View Answer

Answer: d
Explanation: As the programmer has not defined what action should be taken when two objects of class A are added, so the program doesn’t know and gives compile time error.
advertisement

4. What is operator overloading in C++?
a) Overriding the operator meaning by the user defined meaning for user defined data type
b) Redefining the way operator works for user defined types
c) Ability to provide the operators with some special meaning for user defined data type
d) All of the mentioned
View Answer

Answer: d
Explanation: Operator overloading helps programmer to give his/her own meaning to an operator for user defined data types(eg, classes).

5. What is the syntax of overloading operator + for class A?
a) A operator+(argument_list){}
b) A operator[+](argument_list){}
c) int +(argument_list){}
d) int [+](argument_list){}
View Answer

Answer: a
Explanation: The general syntax for operator overloading is:

return_type operator_keywordOperator(argument_list){}
eg.
A opeartor+(argument_list){}

6. How many approaches are used for operator overloading?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are 3 different approaches used for operator overloading:
i. Overloading unary operator.
ii. Overloading binary operator.
iii. Overloading binary operator using a friend function.

7. Which of the following operator cannot be overloaded?
a) +
b) ?:
c) –
d) %
View Answer

Answer: b
Explanation: ?:, :: and . cannot be overloaded +, -, % can be overloaded.

8. Which of the following operator can be overloaded?
a) ?:
b) ::
c) .
d) ==
View Answer

Answer: d
Explanation: ?:, :: and . cannot be overloaded whereas == can be overloaded.

9. Which of the following operator cannot be used to overload when that function is declared as friend function?
a) -=
b) ||
c) ==
d) []
View Answer

Answer: d
Explanation: When an operator overlaoded function is declared as friend function then [] cannot be overloaded.

10. Which of the following operator can be used to overload when that function is declared as friend function?
a) []
b) ()
c) ->
d) |=
View Answer

Answer: d
Explanation: When an operator overlaoded function is declared as friend function then [], () and -> cannot be overloaded.

11. In case of non-static member functions how many maximum object arguments a unary operator overloaded function can take?
a) 1
b) 2
c) 3
d) 0
View Answer

Answer: d
Explanation: In the case of non-static member functions unary operator overloaded function should not take any object argument.

12. In case of non-static member functions how many maximum object arguments a binary operator overloaded function can take?
a) 1
b) 2
c) 3
d) 0
View Answer

Answer: a
Explanation: In the case of non-static member functions binary operator overloaded function should take maximum one object argument only.

13. In the case of friend operator overloaded functions how many maximum object arguments a unary operator overloaded function can take?
a) 1
b) 2
c) 3
d) 0
View Answer

Answer: a
Explanation: In the case of friend operator overloaded functions unary operator overloaded function should take maximum one object argument only.

14. In the case of friend operator overloaded functions how many maximum object arguments a binary operator overloaded function can take?
a) 1
b) 2
c) 3
d) 0
View Answer

Answer: b
Explanation: In the case of friend operator overloaded functions binary operator overloaded function should take maximum two object argument.

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
 
   public:
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
	void operator.()
        {
		cout<<"Objects are added\n";
	}
};
 
class B
{
     public:
};
 
int main(int argc, char const *argv[])
{
	A a1, a2;
	return 0;
}

a) Run-time Error
b) Runs perfectly
c) Segmentation fault
d) Compile-time error
View Answer

Answer: d
Explanation: .(dot) operator cannot be overloaded therefore the program gives error.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
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.