Overloading In C++ Mcq Exercise 4

  1. Can we overload the assignment operator (=) in C++?
  • Both
  • None of the above

Explanation:

The correct answer is option (a). In C++, the assignment operator may be overloaded to customize the assignment behavior for a class object.

  1. Can a non-member function be used to overload operators in C++?
  • Both
  • None of the above

Explanation:

The correct answer is option (a). Operators can be overloaded using the non-member functions, mainly when they require to access private members of a class using friend functions.

  1. Which of the following operators cannot be overloaded in C++?

Explanation:

The correct answer is option (c). The .* operator cannot be overloaded in C++.

  1. Which of the following statements about destructors in C++ is true?
  • Destructors can be overloaded.
  • Destructors cannot be overloaded.
  • Destructors must return an int.
  • Destructors are overloaded automatically.

Explanation:

The correct answer is option (b). Destructors cannot be overloaded, and every class can have only one destructor in C++.

  1. Can constructors be overloaded in C++ programming?
  • Both
  • None of the above

Explanation:

The correct answer is option (a). In C++, constructors can be overloaded by defining two or multiple constructors with different types of parameter lists.

  1. What will be the output of the following code?
  2. Example
    
    #include<iostream>
    
    using namespace std;
    
    class Integer {
    
        int value;
    
    public:
    
        Integer(int v): value(v) {}
    
        friend ostream& operator<<(ostream &out, const Integer &i);
    
    };
    
    ostream& operator<<(ostream &out, const Integer &i) {
    
        out << i.value;
    
        return out;
    
    }
    
    int main() {
    
        Integer i(10);
    
        cout << i << endl;
    
        return 0;
    
    }
    
  • Compilation error
  • Runtime error
  • Undefined behavior

Explanation:

The correct answer is option (a). The << operator is correctly overloaded as a friend function. It prints the value of the Integer object.

Input Required

This code uses input(). Please provide values below: