ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
class P { static String s() { return "S"; } }
// in main:
P p = null;
System.out.print(p.s());- A. S
- B. null
- C. It throws NullPointerException
- D. Compile error: static method via instance
Correct answer: A. S
A static method called through an expression uses only the expression's static type; the reference is never dereferenced, so no exception is thrown and S is printed. The compiler merely warns.
More OOP with C++ and Java questions
- Which line fails to compile? java interface I { static String s() { return "I"; } default String d() { return "D"; } } class C implements…
- What is printed? java int[] a = {1, 2}; int[] b = {1, 2}; System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " +…
- What is printed? java outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == i) continue outer; if (i + j == 3) break…
- What is printed? java enum Lvl { LOW, MID, HIGH } // in main: Lvl a = Lvl.valueOf("MID"); System.out.print(a.ordinal() + " " +…
- Which line fails to compile? java interface Cfg { int MAX = 10; } class Impl implements Cfg { int a = MAX; // line 1 void set() { MAX =…
- What is printed? java class Outer { int v = 1; class Inner { int v = 2; int get() { return v + Outer.this.v; } } static class Nest { int…