ProgrammingHardProduct-company placement · GATE CS
What is printed?
What is printed?
class P { String n = "P"; String get() { return n; } }
class C extends P {
String n = "C";
String get() { return n; }
String sup() { return super.n; }
}
// in main:
P p = new C();
System.out.print(p.n + p.get() + ((C) p).n + ((C) p).sup());- A. CCCP
- B. PCCP
- C. PPCP
- D. CCCC
Correct answer: B. PCCP
Fields are not polymorphic: p.n uses the static type P, giving P, while the cast gives C's field. Methods are: p.get() runs C.get(), which reads C's n. super.n in C reads the hidden field P.
More OOP with C++ and Java questions
- What is printed? java class P { P() { show(); } void show() { System.out.print("P "); } } class C extends P { int x = 5; final int y = 7…
- 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);