ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
int[] a = {1, 2};
int[] b = {1, 2};
System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " + java.util.Arrays.toString(a).equals(java.util.Arrays.toString(b)));- A. true true true
- B. false false true
- C. false true false
- D. false true true
Correct answer: D. false true true
Arrays inherit Object.equals, which compares references, so a.equals(b) is false. Arrays.equals compares elements (true), and Arrays.toString gives "[1, 2]" for both, so those strings are equal (true).
More OOP with C++ and Java questions
- 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() + " " +…
- 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()…