ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class Matrix {
public:
    Matrix operator*(const Matrix &o) const { cout << "mul "; return Matrix(); }
    Matrix operator+(const Matrix &o) const { cout << "add "; return Matrix(); }
};
int main() {
    Matrix a, b, c;
    Matrix r = a + b * c;
}
  1. A. add mul
  2. B. mul add
  3. C. Compile-time error: operator precedence cannot be overloaded away
  4. D. add then mul, order undefined

Correct answer: B. mul add

Overloading operators does NOT change their built-in precedence or associativity: * still binds tighter than +, so a + b * c parses as a + (b * c) exactly as with primitive types. b * c evaluates first, printing mul, and then operator+ runs, printing add. Output: mul add.

More OOP with C++ and Java questions

Exams that ask this topic