Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { int[] a = {1, 2, 3}; int[] b…

1348Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = a;
        b[0] = 9;
        System.out.println(a[0] + " " + a.length);
    }
}
  • A1 3
  • B1 4
  • C9 4
  • D9 3
Correct answer: D

Explanation

b and a refer to the same array, so a[0] becomes 9, and the length is still 3.

Discussion 0

Nobody has replied yet. Start the discussion!