ProgrammingHardGATE CS · TCS Digital · Advanced
What is printed?
What is printed?
class A {
A() { init(); }
void init() { System.out.print("A"); }
}
class B extends A {
int v = 5;
B() { super(); System.out.print(v); }
void init() { System.out.print("B" + v); }
}
// in main:
new B();- A. B55
- B. B05
- C. A5
- D. B00
Correct answer: B. B05
A's constructor calls the overridden init() before B's field initialisers have run, so v is still 0 and 'B0' is printed; afterwards v = 5 and B's constructor body prints 5.
More OOP with C++ and Java questions
- 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 {…
- What is printed? java class K { int v; K(int v) { this.v = v; } public int hashCode() { return v; } public boolean equals(Object o) {…
- What is printed? java Number[] n = new Integer[2]; n[0] = 1; try { n[1] = 2.5; } catch (ArrayStoreException e) { System.out.print("ASE ")…