Array Creation and Access

AP Computer Science A· difficulty 2/5

int[] a;
a = new int[]{2, 4, 6};
System.out.println(a[1]);

What is printed?

  • A

    4

    check_circle
  • B

    2

  • C

    6

  • D

    Compilation error

Explanation

After declaration, a is assigned a new int array {2,4,6}. The shorthand {2,4,6} is only valid in declarations; here we use new int[]{...}. a[1] is 4.

Want 10 more like this — adaptive to your weak spots?

Related questions