ExamVeda
Login
Home
This question belongs to Java Program Array
Array
?

Which of the following is a valid declaration and initialization of a String array in Java?

Answer & Solution
Correct Answer: Option D
Option A initializes a String array named "names" with three elements: "Alice", "Bob", and "Charlie".
This is a valid and commonly used syntax for declaring and initializing a String array in Java.

Option B: String names[] = new String[]{"Alice", "Bob", "Charlie"};
This syntax is also valid for declaring and initializing a String array in Java. However, it's less commonly used.

Option C: String[3] names = {"Alice", "Bob", "Charlie"};
This syntax is invalid. In Java, when declaring an array, the size should not be specified in the square brackets.

Examveda
Question posted by Examveda
Community

Join the Discussion

1 Comment
Shalini Chauhan
Shalini Chauhan 2 years ago
Which of the following is a valid declaration and initialization of a String array in Java?
A. String[] names = {"Alice", "Bob", "Charlie"};

B. String names[] = {"Alice", "Bob", "Charlie"};

C. String[3] names = {"Alice", "Bob", "Charlie"};

D. All of the above

option A and option B both are true and valid why given only answer A