ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
int[] a = {1, 2}, b = {1, 2};
int[][] p = {{1}}, q = {{1}};
System.out.print(a.equals(b) + " " + Arrays.equals(a, b) + " "
+ Arrays.equals(p, q) + " " + Arrays.deepEquals(p, q));- A. true true true true
- B. false true true true
- C. false false false true
- D. false true false true
Correct answer: D. false true false true
Arrays do not override equals, so a.equals(b) is identity (false). Arrays.equals compares elements with equals: fine for ints, but the inner arrays of p and q are compared by identity (false). deepEquals recurses into nested arrays.
More OOP with C++ and Java questions
- What is printed? java String s = null; s += "x"; String t = "a" + null + 1 + 2; System.out.print(s + " " + t);
- What is printed? java StringBuilder sb = new StringBuilder("abcdef"); sb.insert(2, "XY").delete(5, 7).reverse().setCharAt(0, 'Z')…
- What is printed? java String s = "level"; String t = s.replace('l', 'p'); System.out.print(t + " " + (s.replace('z', 'q') == s) + " " + s);
- What is printed? java String s = "hello"; System.out.print("[" + s.substring(5) + "]"); System.out.print(s.substring(6));
- What is printed? java Integer a = 1000; Integer b = a; a++; System.out.print(a + " " + b + " " + (a == b));
- What is printed? java class P { int id; P(int id) { this.id = id; } public boolean equals(P o) { return o != null && id == o.id; } } // in…