ProgrammingVery hardProduct-company placement · GATE CS
What is printed?
What is printed?
class P {
P() { show(); }
void show() { System.out.print("P "); }
}
class C extends P {
int x = 5;
final int y = 7;
C() { show(); }
void show() { System.out.print(x + "" + y + " "); }
}
// in main:
new C();- A. 00 57
- B. P 57
- C. 57 57
- D. 07 57
Correct answer: D. 07 57
P's constructor runs before C's field initialisers, and the overridden show() already dispatches to C: x is still 0. y is a final field initialised with a constant, so it is a constant variable and every use of it is the compile-time value 7.
More OOP with C++ and Java questions
- What is printed? java static int f() { int x = 1; try { return x; } finally { x = 2; } } static StringBuilder g() { StringBuilder sb = new…
- What is printed? java static void m(StringBuilder a, StringBuilder b) { a.append("1"); b = a; b.append("2"); } // in main: StringBuilder x…
- What is printed? java char c = 'a'; c += 1; System.out.print(c + 1); System.out.print(" " + c + 1);
- Which statement about this code is correct? java Object[] arr = new String[2]; // L1 arr[0] = "a"; // L2 arr[1] = Integer.valueOf(1); //…
- What is printed? java int i = 5; i = i++ + ++i; System.out.print(i);
- What is printed? java class A { Object get() { return "A"; } void set(String s) { System.out.print("A.set "); } } class B extends A {…