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;
}- A. add mul
- B. mul add
- C. Compile-time error: operator precedence cannot be overloaded away
- 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
- What is printed? java class Base { public Base() { System.out.print("Base() "); } } class Sub extends Base { public Sub() { super()…
- What is printed? cpp struct Shape { virtual ~Shape() = default; virtual double area() const { return 0; } }; struct Circle : Shape {…
- What is printed? java class Counter { private int count; public synchronized void inc() { count++; } public int get() { return count; } }…
- What is printed? java class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) {…
- What is printed? java public class Main { public static void main(String[] args) { String a = "hello"; String b = "hello"; String c = new…
- What is printed? java public class Main { public static void main(String[] args) { String a = new String("hi").intern(); String b = "hi"…