Operator Overloading Questions and Answers

C++ Programming Questions and Answers – Operator Overloading

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

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

Explanation: ?:, :: and . cannot be overloaded whereas == can be overloaded.
2. Which of the following operator cannot be used to overload when that function is declared as friend function?
a) -=
b) ||
c) ==
d) []

Explanation: When a friend function is declared for an operator overloaded function, [] cannot be overloaded.
3. Which of the following operator can be used to overload when that function is declared as friend function?
a) []
b) ()
c) ->
d) |=

Explanation: [], (), and -> cannot be overloaded when an operator overloaded function is declared as a friend function.
4. 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

Explanation: Unary operator overloaded functions for non-static member functions should not take any object arguments.
5. 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

Explanation: In the case of non-static member functions, the overloaded binary operator should only take one object parameter.
6. 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

Explanation: Unary operator overloaded functions should only take one object parameter in the case of friend operator overloaded functions.
7. 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

Explanation: Binary operator overloaded functions should take no more than two object arguments in the case of friend operator overloaded functions.
8. 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

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

9. 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

Explanation: Because every static member must be initialised, and we have already done so with variable ‘a,’ there will be no run-time errors. There is no compiler issue because variable ‘a’ is a static member and is referenced using the class for initialization.

10. 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

Explanation: Because string is a C++ class, when we do (s1+s2), a temporary object is formed that contains the result of s1+s2. That object then calls the function substr(), which is a callable function for that temporary string object because it is of the string class.
11. 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

Explanation: Because the programmer hasn’t specified what should happen when two objects of type A are combined, the computer doesn’t know what to do and generates a compile time error.
12. 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

Explanation: Operator overloading allows a programmer to give an operator his or her own meaning for user-defined data types (eg, classes).
13. 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){}

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

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

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.
15. Which of the following operator cannot be overloaded?
a) +
b) ?:
c) –
d) %

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

Operator overloading, also known as operator ad hoc polymorphism in computer programming, is a type of polymorphism in which various operators have different implementations depending on the inputs they receive. Operator overloading is a syntactic sugar that allows programming with notation closer to the target domain and allows user-defined types to have a similar level of syntactic support as types built in.

Leave a Reply

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