ProgrammingHardGATE CS · TCS Digital · Advanced
What is printed? (C++17, assume <iostream> and using namespace std)
What is printed? (C++17, assume <iostream> and using namespace std)
struct C {
int v = 0;
C& operator++() { ++v; return *this; }
C operator++(int) { C t = *this; ++v; return t; }
};
int main() {
C c;
(c++)++;
++(++c);
cout << c.v;
}- A. 3
- B. 5
- C. 2
- D. 4
Correct answer: A. 3
Postfix ++ returns a copy, so (c++)++ increments c once and then a discarded temporary. Prefix ++ returns a reference to c, so ++(++c) adds 2: total 3.
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() { super()…
- What is printed? java Long l = 127L; Integer i = 127; System.out.print(l.equals(i) + " " + l.equals(127L) + " " + (l == 127) + " " +…
- What is printed? java static void f(long x) { System.out.print("L"); } static void f(Integer x) { System.out.print("I"); } static void…
- What is printed? java System.out.print(true ? 1 : 2.0); System.out.print(true ? 'a' : 0); int i = 0; System.out.print(true ? 'a' : i);
- What is printed? java static int f() { try { throw new RuntimeException(); } finally { return 1; } } static int g() { int x = 1; try {…