ProgrammingVery hardTCS NQT Prime · Infosys SP
What is printed?
What is printed?
class A {
A() { init(); }
void init() { System.out.print("A"); }
}
class B extends A {
int v = 5;
B() { System.out.print(v); }
void init() { System.out.print(v); }
}
// main: new B();- A. 05
- B. 55
- C. A5
- D. 00
Correct answer: A. 05
A's constructor calls the overridden init() before B's field initializers run, so v is still 0; then v = 5 is set and B's constructor prints 5.
More OOP with C++ and Java questions
- What is printed? java class S { static { System.out.print("s"); } { System.out.print("i"); } S() { System.out.print("c"); } } // main: new…
- What does this print? java static StringBuilder f() { StringBuilder sb = new StringBuilder("a"); try { sb.append("b"); return sb; }…
- What is printed? java String a = "hi"; String b = new String("hi"); String c = b.intern(); System.out.print((a == b) + " " + (a == c) + "…
- What is printed? java static void f(Integer i) { System.out.print("I"); } static void f(long l) { System.out.print("L"); } static void…
- What is printed? java Integer a = 127, b = 127, c = 128, d = 128; System.out.print((a == b) + " " + (c == d) + " " + c.equals(d));
- What happens when this runs? java Integer n = null; boolean flag = false; Integer r = flag ? 1 : n; System.out.print(r);