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

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

Q 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

Show Answer Answer:-C. No output
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.

Q 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

Show Answer Answer:-D. Run-time error
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.

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

#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

Show Answer Answer:-D. Compile-time Error
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.

Q 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

Show Answer Answer:-D. All of the mentioned
Explanation Operator overloading helps programmer to give his/her own meaning to an operator for user defined data types(eg, classes).

Q 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){}

Show Answer Answer:-A. A operator+(argument_list){}
Explanation The general syntax for operator overloading is: return_type operator_keywordOperator(argument_list){} eg. A opeartor+(argument_list){}

Q 6. How many approaches are used for operator overloading?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-C. 3
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.

Q 7. Which of the following operator cannot be overloaded?
A. +
B. ?:
C. –
D. %

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

Q 8. Which of the following operator can be overloaded?
A. ?:
B. ::
C. .
D. ==

Show Answer Answer:-D. ==
Explanation ?:, :: and . cannot be overloaded whereas == can be overloaded.

Q 9. Which of the following operator cannot be used to overload when that function is declared as friend function?
A. -=
B. ||
C. ==
D. []

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

Q 10. Which of the following operator can be used to overload when that function is declared as friend function?
A. []
B. ()
C.->
D. |=

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

Q 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

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

Q 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

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

Q 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

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

Q 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

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

Q 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

Show Answer Answer:-D. Compile-time error
Explanation .(dot) operator cannot be overloaded therefore the program gives error.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page