ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
Which line fails to compile?
Which line fails to compile?
interface Cfg { int MAX = 10; }
class Impl implements Cfg {
int a = MAX; // line 1
void set() { MAX = 20; } // line 2
int b = Cfg.MAX + 1; // line 3
}- A. Line 1
- B. Line 3
- C. None of them
- D. Line 2
Correct answer: D. Line 2
Interface fields are implicitly public static final, so MAX cannot be assigned. Reading it unqualified in an implementing class (line 1) or qualified (line 3) is fine.
More OOP with C++ and Java questions
- 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…
- What is printed? java String s = "java"; s.toUpperCase(); s.concat("x"); StringBuilder sb = new StringBuilder("java"); sb.reverse()…
- What is printed? java interface I { default String n() { return "I"; } } class P { public String n() { return "P"; } } class C extends P…
- What is printed (C++17)? cpp struct T { T() { } T(const T&) { cout << "C"; } T(T&&) { cout << "M"; } }; int main() { const T a; T b =…
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } }; struct W { T t; }; int main() { W a; W b = std::move(a); }