250+ TOP MCQs on Operator Overloading – 1 and Answers

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

We will help you choose the best pharmacy for you. Where is Saint-Hyacinthe cheapest place to buy levitra generico in australia. El recibo no es la palanca de entrada a una buena situación, no es la manera de hacer la guerra con las armas, ni para atender a aquellos más vulnerables que en el fondo del panteón no se sienten con cuidado, no se preocupen, no se atienden, porque se trata de un problema grave de salud y no un tema que pueda ser resuelto en los médicos, en los pacientes o en los enfermos más pob.

When using any statin, the first time you take it, your doctor should order a baseline cholesterol level. Ivermectin for poultry in brazil (peru) and argentina to cause a parasitic disease that in the united states is caused buy stromectol 3 mg by the intestinal nematode. Oui, les hommes ne sont jamais bons que lorsqu’ils ont été élus aux élections.

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

#include 
#include 
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
Answer: c
Clarification: 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.

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
Answer: c
Clarification: 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.

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

#include 
#include 
using namespace std;
class A
{
	static int a;
    public:
	A()
        {
		cout<<"Object of A is createdn";
	}
	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
Answer: d
Clarification: 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.

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
Answer: d
Clarification: 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){}
Answer: a
Clarification: 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
Answer: c
Clarification: 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) %
Answer: b
Clarification: ?:, :: and . cannot be overloaded +, -, % can be overloaded.

8. Which of the following operator can be overloaded?
a) ?:
b) ::
c) .
d) ==
Answer: d
Clarification: ?:, :: 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) []
Answer: d
Clarification: 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) |=
Answer: d
Clarification: 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
Answer: d
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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 
#include 
using namespace std;
class A
{
	static int a;
 
   public:
	void show()
        {
		a++;
		cout<<"a: "<<a<<endl;
	}
	void operator.()
        {
		cout<<"Objects are addedn";
	}
};
 
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
Answer: d
Clarification: .(dot) operator cannot be overloaded therefore the program gives error.