LearnFrenzy
Sign in Take a free test

Arrays in Java · Predict the Output · Question 4

==, equals and Arrays.equals on arrays Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        int[][] x = {{1}, {2}};
        int[][] y = {{1}, {2}};
        System.out.println((a == b) + " " + a.equals(b) + " " + Arrays.equals(a, b) + " "
                + Arrays.equals(x, y) + " " + Arrays.deepEquals(x, y));
    }
}
  • A false false true false true Correct
  • B true true true true true
  • C false true true true true
  • D false false true true true
Correct Answer: Option A

For nested arrays Arrays.equals compares the row references; only deepEquals compares the contents of the rows.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links