Multiple choice technology programming languages

Which are legal declarations?

  1. short x [];

  2. short [] y;

  3. short [5] x2;

  4. short z2 [5];

  5. short [] z [] [];

  6. short [] y2 = [5];

Reveal answer Fill a bubble to check yourself
A,B,E Correct answer
Explanation

In Java, array declarations can place brackets before or after the variable name. A (short x[]) and B (short [] y) are both valid legal declarations. E (short [] z [] []) is valid for multi-dimensional arrays (jagged array declaration). C (short [5] x2) and F (short [] y2 = [5]) are invalid - size cannot be specified in declaration, and array initialization needs 'new short[5]' not '[5]'. D (short z2[5]) is also invalid for same reason as C.