Operator Overloading Interview Questions

    Question 1C++ OOPS Operator Overloading Concept

    Which operators cannot be overloaded in C++?

    Question 2C++ OOPS Operator Overloading Return Type

    What should be the return type when overloading the ++ operator (prefix version)?

    Question 3C++ OOPS Unary Operator Overloading Example

    What will be the output?

    #include <iostream> using namespace std; class Counter { int value; public: Counter(int v) : value(v) {} void operator++() { value++; } void display() { cout << value; } }; int main() { Counter c(5); ++c; c.display(); }

    Question 4C++ OOPS Binary Operator Overloading

    Which function signature correctly overloads the binary + operator inside a class Complex?

    Question 5C++ OOPS Friend Function Operator Overloading

    What will be the output?

    #include <iostream> using namespace std; class Complex { int x; public: Complex(int v) : x(v) {} friend Complex operator+(Complex c1, Complex c2); void show() { cout << x; } }; Complex operator+(Complex c1, Complex c2) { return Complex(c1.x + c2.x); } int main() { Complex a(3), b(4); Complex c = a + b; c.show(); }

    Question 6C++ OOPS Operator Overloading Limitation

    Which of the following statements is NOT true?

    Question 7C++ OOPS Overloading Postfix ++

    Which is the correct function signature for overloading postfix ++?

    Question 8C++ OOPS Relational Operator Overloading

    What will be the output?

    #include <iostream> using namespace std; class Box { int volume; public: Box(int v) : volume(v) {} bool operator>(Box b) { return volume > b.volume; } }; int main() { Box b1(10), b2(20); cout << (b1 > b2); }

    Question 9C++ OOPS Overloading [] Operator

    Which statement about overloading [] is correct?

    Question 10C++ OOPS Operator Overloading Example (Mix)

    What will be the output?

    #include <iostream> using namespace std; class Point { int x; public: Point(int v) : x(v) {} Point operator-(Point p) { return Point(x - p.x); } void show() { cout << x; } }; int main() { Point p1(10), p2(3); Point p3 = p1 - p2; p3.show(); }