ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
struct S {
int a = 1;
int b = 2;
S() : a(10) { }
S(int) { }
};
int main() { S x; S y(0); cout << x.a << x.b << y.a; }- A. 1021
- B. 1011
- C. 121
- D. 10210
Correct answer: A. 1021
A default member initializer is used only by constructors that do not initialise that member in their own initializer list: S() overrides a with 10 but keeps b = 2; S(int) keeps both defaults.
More OOP with C++ and Java questions
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() { D…
- What is printed? java class Util { static int say(String s) { System.out.print(s); return 1; } } interface I { int X = Util.say("I"); }…
- What is printed? java class P { static int v = 5; static { System.out.print("P"); } } class C extends P { static { System.out.print("C")…
- What is printed? java class C { static { System.out.print("init "); } } // in main: C[] a = new C[2]; Class<?> k = C.class…
- What is printed? java try { try { System.out.print("1"); throw new Exception("a"); } catch (Exception e) { System.out.print("2"); throw…
- What happens when this is compiled (inside a method declared throws IOException)? java List<String> files = List.of("a.txt")…