91.9k views
5 votes
Which of the following would correctly declare and instantiate an array of 3 ints?

a)int[6] values = new int[];
b))int[6] values = new int[6];
c)int[] values = int[4];
d)int[] values = int[6];
e)none of these

User Scuro
by
8.0k points

1 Answer

3 votes

Final answer:

The correct option to declare and instantiate an array of 3 ints is int[] values = new int[3]; (option d)

Step-by-step explanation:

Option a) int[6] values = new int[];: This is incorrect syntax. It does not specify the size of the array during instantiation, and it is missing the size within the square brackets.

Option b) int[6] values = new int[6];: This is incorrect because it declares an array of size 6, not 3.

Option c) int[] values = int[4];: This is incorrect syntax. When creating an array, the size should be specified within the square brackets during instantiation, not when declaring the array type.

Option d) int[] values = new int[3];: This is the correct option. It declares and instantiates an array of integers with a size of 3.

Option e) none of these: This is incorrect; option d) is a valid choice.

The correct way to declare and instantiate an array of 3 ints in Java is shown in option d), which is int[] values = new int[3];.

User Pfm
by
8.4k points