ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
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 outer;
System.out.print(i + "" + j + " ");
}
}- A. 10 20
- B. 10 20 21
- C. 10
- D. 01 10 20
Correct answer: A. 10 20
i = 0: j = 0 equals i, so continue outer. i = 1: prints 10, then j = 1 continues outer. i = 2: prints 20, then j = 1 gives i + j = 3 and breaks out of both loops.
More OOP with C++ and Java questions
- 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…
- 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…