ProgrammingModerateInfosys · HCL
In Java, an interface declares int MAX = 10;. What happens if an implementing class executes MAX = 20; ?
- A. MAX becomes 20 for that object only
- B. MAX becomes 20 for all implementing classes
- C. It compiles and throws an exception at run time
- D. Compile error, because interface fields are implicitly public static final
Correct answer: D. Compile error, because interface fields are implicitly public static final
Fields in an interface are constants: public, static and final by default, so assignment is rejected at compile time.
More OOP with C++ and Java questions
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct A { A() { cout << "c"; } A(const A&) { cout << "k"; } ~A() {…
- What is printed? java class P { static String w() { return "P"; } } class Q extends P { static String w() { return "Q"; } } // in main: P…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct…
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void…
- What is printed? java class A { int x = 1; } class B extends A { int x = 2; } // in main: A a = new B(); System.out.print(a.x + " " + ((B)…
- What is printed? java class P { P() { init(); } void init() { } } class C extends P { int v = 5; void init() { System.out.print(v); } } //…