ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
int i = 0, j = 0; boolean p = (i++ > 0) & (i++ > 0); boolean q = (j++ > 0) && (j++ > 0); System.out.print(i + " " + j + " " + p + " " + q);
- A. 1 1 false false
- B. 2 2 false false
- C. 2 1 false false
- D. 2 1 true false
Correct answer: C. 2 1 false false
& on booleans evaluates both operands, so i is incremented twice; && stops after the first false operand, so j is incremented once. Both first comparisons are 0 > 0, false, so p and q are false.
More OOP with C++ and Java questions
- What is printed? java class P { private void f() { System.out.print("P"); } void call() { f(); } } class C extends P { void f() {…
- What is printed? java class P { static String s() { return "S"; } } // in main: P p = null; System.out.print(p.s());
- Which line fails to compile? java interface I { static String s() { return "I"; } default String d() { return "D"; } } class C implements…
- What is printed? java int[] a = {1, 2}; int[] b = {1, 2}; System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " +…
- What is printed? java 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…
- What is printed? java enum Lvl { LOW, MID, HIGH } // in main: Lvl a = Lvl.valueOf("MID"); System.out.print(a.ordinal() + " " +…