ProgrammingHardAccenture · Infosys
What is printed?
What is printed?
class P { P() { init(); } void init() { } }
class C extends P {
int v = 5;
void init() { System.out.print(v); }
}
// in main:
new C();- A. 5
- B. 0
- C. null
- D. Compile error
Correct answer: B. 0
The superclass constructor runs before C's field initialisers, and the overridden init is called while v still holds its default 0.
More OOP with C++ and Java questions
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct M { M() { cout << "M"; } }; struct B { B() { cout << "B"; }…
- What is printed? java Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d));
- What does this method return? java static int f() { int x = 1; try { return x; } finally { x = 2; } }
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { B() { f(); } virtual void f() { cout << "B"; } }; struct…
- A Java method has this structure. What happens when it is compiled? java try { reader.read(); // throws IOException } catch (Exception e)…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() = 0; }; void B::f() { cout << "B"; }…