ProgrammingVery hardTCS NQT Prime · Infosys SP
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct I {
int v;
I& operator++() { ++v; return *this; }
I operator++(int) { I t = *this; ++v; return t; }
};
int main() {
I a{5};
I b = a++;
I c = ++a;
cout << a.v << b.v << c.v;
}- A. 757
- B. 665
- C. 577
- D. 767
Correct answer: A. 757
a++ returns the old copy (b.v = 5) and a becomes 6; ++a makes a = 7 and c copies it (7). Output a.v, b.v, c.v = 7,5,7.
More OOP with C++ and Java questions
- What is printed? (also assume <typeinfo>) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct G { const char n; G(const char s)…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp void f(bool) { cout << "b"; } void f(string) {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp template <class T> void f(T) { cout << "T"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { static int n; static void p() {…