ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

What is printed?

What is printed?
struct Vec2 {
    int x, y;
    Vec2 operator+(const Vec2& o) const { return {x + o.x, y + o.y}; }
};
int main() {
    Vec2 a{1, 2}, b{3, 4};
    Vec2 c = a + b + a;
    cout << c.x << " " << c.y;
}
  1. A. 6 8
  2. B. 5 8
  3. C. 8 10
  4. D. 4 6

Correct answer: B. 5 8

a + b = {4, 6}; adding a again gives {4+1, 6+2} = {5, 8}, since + associates left to right here.

More OOP with C++ and Java questions

Exams that ask this topic