ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement
Which line fails to compile?
Which line fails to compile?
interface I { static String s() { return "I"; } default String d() { return "D"; } }
class C implements I { }
// in main:
String a = I.s(); // line 1
String b = new C().d(); // line 2
String c = C.s(); // line 3
String e = ((I) new C()).d(); // line 4- A. Line 1
- B. Line 2
- C. Line 4
- D. Line 3
Correct answer: D. Line 3
Static methods of an interface are not inherited by implementing classes, so they must be called as I.s(); C.s() does not compile. Default methods are inherited and callable on C or through I.
More OOP with C++ and Java questions
- 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…
- Which line fails to compile? java class P { void run() throws java.io.IOException { } } class A extends P { void run() { } } // line 1…