0% found this document useful (0 votes)
44 views7 pages

Constructors MCQS

Operator overloading in C++ allows giving additional meaning to existing operators for user-defined types. Certain operators, like `::`, `.*`, `?:`, and `.` cannot be overloaded, while the `operator` keyword is used for this purpose. Friend functions are often utilized in operator overloading to access private members of a class.

Uploaded by

Furqan Awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

Constructors MCQS

Operator overloading in C++ allows giving additional meaning to existing operators for user-defined types. Certain operators, like `::`, `.*`, `?:`, and `.` cannot be overloaded, while the `operator` keyword is used for this purpose. Friend functions are often utilized in operator overloading to access private members of a class.

Uploaded by

Furqan Awan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Operator overloading

1. **What is operator overloading in C++?**

a) Changing the precedence of operators

b) Defining new operators

c) Giving additional meaning to existing operators for user-defined types

d) Removing operators from C++

**Answer:** c

2. **Which operators cannot be overloaded in C++?**

a) `+`, `-`

b) `::`, `.*`, `?:`, `.`

c) `<<`, `>>`

d) `++`, `--`

**Answer:** b

3. **Operator overloading is an example of:**

a) Polymorphism

b) Inheritance

c) Encapsulation

d) Abstraction

**Answer:** a

4. **Which keyword is used for operator overloading?**

a) `operator`

b) `overload`
c) `function`

d) `define`

**Answer:** a

---

#### **Syntax & Implementation**

5. **How is the `+` operator overloaded for a class `Complex`?**

a) `Complex operator+(Complex c)`

b) `void operator+()`

c) `int +operator(Complex c)`

d) `Complex +operator()`

**Answer:** a

6. **Which of the following is the correct way to overload the `<<` operator
for output?**

a) `ostream& operator<<(ostream& out, ClassName obj)`

b) `void operator<<(ostream out, ClassName obj)`

c) `ClassName operator<<(ostream& out)`

d) `ostream operator<<(ClassName obj)`

**Answer:** a

7. **Can the `=` operator be overloaded?**

a) No, it is a built-in operator

b) Yes, but only for primitive types

c) Yes, for user-defined classes


d) Only in Java

**Answer:** c

8. **What is the return type of the postfix `++` operator overload function?
**

a) `void`

b) `ClassName&`

c) `ClassName` (by value)

d) `int`

**Answer:** c

---

#### **Unary & Binary Operators**

9. **How is the unary `-` operator overloaded?**

a) `ClassName operator-()`

b) `ClassName operator-(ClassName obj)`

c) `void operator-()`

d) `int operator-()`

**Answer:** a

10. **Which function overloads the `==` operator for comparing two
objects?**

a) `bool operator==(ClassName obj)`

b) `void operator==(ClassName obj)`

c) `ClassName operator==()`
d) `int operator==(ClassName obj)`

**Answer:** a

11. **How is the `[]` (subscript) operator overloaded?**

a) `int operator[](int index)`

b) `ClassName operator[](int index)`

c) `void operator[](int index)`

d) `float operator[]()`

**Answer:** a

12. **What is the correct way to overload the `+=` operator?**

a) `ClassName& operator+=(ClassName obj)`

b) `void operator+=(ClassName obj)`

c) `int operator+=(ClassName obj)`

d) `ClassName operator+=(ClassName obj)`

**Answer:** a

---

#### **Friend Functions & Special Cases**

13. **Why are friend functions used in operator overloading?**

a) To access private members of a class

b) To improve performance

c) To allow operators to work with primitive types

d) To replace member functions

**Answer:** a
14. **Which operator must be overloaded as a friend function?**

a) `+`

b) `=`

c) `<<` (for `cout`)

d) `[]`

**Answer:** c

15. **Can the `()` (function call) operator be overloaded?**

a) No

b) Yes, to create functors (function objects)

c) Only in Java

d) Only for arrays

**Answer:** b

16. **What is the correct syntax for overloading the `()` operator?**

a) `void operator()(args)`

b) `ClassName operator()(args)`

c) `int operator()()`

d) `float operator()(ClassName obj)`

**Answer:** a

---

#### **Advanced Concepts**

17. **What happens if the `=` operator is not overloaded for a class?**
a) Compiler generates a default assignment operator

b) Program crashes

c) Objects cannot be copied

d) Operator overloading is disabled

**Answer:** a

18. **Which operator cannot be overloaded using a friend function?**

a) `+`

b) `=`

c) `<<`

d) `==`

**Answer:** b

19. **What is the output of the following code?**

```cpp

class Test {

public:

int x;

Test operator+(Test t) {

Test temp;

temp.x = x + t.x;

return temp;

};

int main() {

Test t1, t2;

t1.x = 5;
t2.x = 10;

Test t3 = t1 + t2;

cout << t3.x;

```

a) `5`

b) `10`

c) `15`

d) Error

**Answer:** c

20. **Which operator is commonly overloaded to enable smart pointers?**

a) `*` (dereference)

b) `+`

c) `=`

d) `>>`

**Answer:** a

You might also like