Which declaration initializes an array in a single statement with exactly five elements?

Study for the Java FPT Software (Fsoft) Academy Test. Master Java concepts with diverse questions, hints, and explanations. Achieve exam success!

Multiple Choice

Which declaration initializes an array in a single statement with exactly five elements?

Explanation:
This question tests how to declare and initialize an array in a single statement using explicit values. The right approach uses an array initializer: you put the five elements inside curly braces right after the type. For example, int arr[] = {1, 2, 3, 4, 5}; creates an array of length five and initializes each position with the corresponding value, all in one statement. Other forms behave differently. Using new int[5] declares an array of length five but does not provide explicit values for the elements in the initializer, so the elements default to zero and you don’t have five explicit values specified here. A plain declaration with no initialization doesn’t set up any element values. Providing four values in the initializer means the array would have length four, not five.

This question tests how to declare and initialize an array in a single statement using explicit values. The right approach uses an array initializer: you put the five elements inside curly braces right after the type. For example, int arr[] = {1, 2, 3, 4, 5}; creates an array of length five and initializes each position with the corresponding value, all in one statement.

Other forms behave differently. Using new int[5] declares an array of length five but does not provide explicit values for the elements in the initializer, so the elements default to zero and you don’t have five explicit values specified here. A plain declaration with no initialization doesn’t set up any element values. Providing four values in the initializer means the array would have length four, not five.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy