Vidyalelo
Java Programming · Q2

Array

Programming · Java Programming · question 2

Q2

How do you declare a one-dimensional array in Java?

A.
int[] myArray;
Answer
B.
int myArray[];
C.
Array myArray;
D.
All of the above

Answer: Option A

Solution

Answer: Option A
Solution:
In Java, a one-dimensional array is declared by specifying the type of elements followed by square brackets and then the array name. The correct syntax is:
type[] arrayName;
Option A follows this syntax and is the correct way to declare a one-dimensional array in Java.
Option B is also a valid syntax but is less commonly used. Both Option A and Option B are equivalent.
Option C is incorrect as it attempts to use generics syntax, which is not allowed for arrays in Java.
Option D is incorrect because Option C is not a valid way to declare an array.