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;
}- A. 6 8
- B. 5 8
- C. 8 10
- 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
- A base class has a non-virtual destructor. A derived class owns a dynamically allocated member that it frees in its own destructor. What…
- java interface Greet { default String hello() { return "Hi"; } } class English implements Greet {} class French implements Greet { public…
- Why is operator<< for printing a user-defined type usually written as a free function (often declared a friend), rather than as a member…
- java class R implements AutoCloseable { public void close() { System.out.print("close"); } } try (R r = new R()) {…
- Which statement about C++ vtables is correct for a class with multiple virtual functions?
- Class A's constructor declares throws IOException. Class B extends A. What must every constructor of B do about this checked exception?