ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
enum Lvl { LOW, MID, HIGH }
// in main:
Lvl a = Lvl.valueOf("MID");
System.out.print(a.ordinal() + " " + (a.compareTo(Lvl.HIGH) < 0) + " " + Lvl.values().length);- A. 2 true 3
- B. 1 false 3
- C. 1 true 3
- D. 1 true 2
Correct answer: C. 1 true 3
MID has ordinal 1; enums compare by ordinal, and MID comes before HIGH, so compareTo is negative (true); values() returns all 3 constants.
More OOP with C++ and Java questions
- 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…
- 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 =…