Machine-translated by Google. Questions are authored in English.
Top

Operator Overloading Questions and Answers

1.

If an arithmetic assignment operator is overloaded in C++ programming the result :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: B

The result goes in the object to the left of the operator.

Arithmetic assignment operators such as += and -= modify their left operand. When += is overloaded as a member function, a += b is called as a.operator+=(b): the object on the left is the one that calls the function, and the function changes that object's own data.

The object on the right is only read. The operator may also return a reference to the left object so that assignments can be chained, but returning a result is not required, and the result is stored in the left object whether or not anything is returned.

Please log in or register to report this question.

2.

In C++/C operators such as............ cannot be overloaded.

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

The operator that cannot be overloaded is ::, the scope resolution operator.

C++ allows most operators to be given new meanings for user-defined types, but a few are fixed by the language: the scope resolution operator ::, the member access operator ., the pointer-to-member operator .*, the conditional operator ?: and sizeof. These work on names and types at compile time rather than on values, so overloading them would make the language ambiguous.

The other options, +, ++ and ==, are among the operators most commonly overloaded, for example to add, increment or compare objects of a class.

Please log in or register to report this question.

3.

To overload an operator................. keyword must be used along with the operator to be overloaded.

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: D

The keyword is operator.

An overloaded operator is defined as a function whose name is the keyword operator followed by the symbol being overloaded. For example, Complex operator+(const Complex &other) const; declares how + works for a Complex class, and the compiler then translates a + b into a call to that function.

'Over' and 'Overload' are not C++ keywords at all. void is a keyword, but it names the empty return type and has nothing to do with overloading operators.

Please log in or register to report this question.

4.

Using a constructor to specify the type conversion is convenient but has implications that can be undesirable in :

(i) There can be no implicit conversion from a user defined type to basic type.
(ii) It is not possible to specify a conversion from a new type to an old one without modifying the declaration for the old one.
(iii) It is not possible to have a constructor with a single argument without also having a conversion.
(iv) A user defined conversion is implicitly applied only if it’s unique.

Exam Year(s)

MH SET - 2019, SET Exam - 2019,

Answer: B

The undesirable implications are (i), (ii) and (iii).

Bjarne Stroustrup notes that a constructor taking one argument is a handy way to define a conversion, but it has limits. It cannot give an implicit conversion from a user-defined type to a basic type, since basic types are not classes (i). It cannot specify a conversion from a new class to an older one without changing the old class's declaration (ii). And, before the keyword explicit, every single-argument constructor also acted as an implicit conversion, wanted or not (iii). Conversion operators were introduced to deal with the first two.

Statement (iv) is a language rule, that an implicit user-defined conversion is used only when it is unambiguous, which prevents surprises rather than causing them. So options including (iv) are wrong.

Please log in or register to report this question.

5.

Which of the following operators can not be overloaded in C+ + ?

Exam Year(s)

UGC NET - 2013,

Answer: D

The scope resolution operator :: cannot be overloaded in C++. Along with the member access operator (.), the pointer-to-member operator (.*) and the conditional operator (?:), it is one of the few operators whose behaviour is fixed by the language.

The multiplication operator *, the compound assignment += and the equality operator == can all be overloaded as member or friend functions, which is why they are not the answer.

Please log in or register to report this question.

Loading…
Tags: Operator Overloading Questions and Answers || Operator Overloading MCQ Questions and Answers || Operator Overloading GK Questions and Answers || Operator Overloading GK MCQ Questions || Operator Overloading Multiple Choice Questions and Answers || Operator Overloading GK || GK on Operator Overloading || Object Oriented Programming Questions and Answers || Object Oriented Programming MCQ Questions and Answers || Object Oriented Programming GK Questions and Answers || GK on Object Oriented Programming