ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
String s = "b";
int r = 0;
switch (s) {
case "a": r += 1;
case "b": r += 2;
case "c": r += 4; break;
default: r += 8;
}
System.out.print(r);- A. 2
- B. 6
- C. 14
- D. 7
Correct answer: B. 6
Execution enters at case "b" and falls through to case "c" because there is no break, adding 2 + 4 = 6; the break then stops before default.
More OOP with C++ and Java questions
- What is printed? java class R implements AutoCloseable { public void close() { throw new RuntimeException("C"); } } // in main: try { try…
- Which line fails to compile? java void run() { try { int x = 1; } catch (RuntimeException e) { } // line 1 try { int y = 2; } catch…
- What is printed? java class P { static { System.out.print("1"); } { System.out.print("2"); } P() { System.out.print("3"); } } class C…
- Which line fails to compile? java int base = 10; int step = 1; step = 2; Function<Integer, Integer> f = x -> x + base; // line 1…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(int... a) {…
- What is printed? java char c = 'a'; c += 1; System.out.print(('a' + 1) + "" + (char) ('a' + 1) + c);