?
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:
This syntax is also valid for declaring and initializing a String array in Java. However, it's less commonly used.
Option C:
This syntax is invalid. In Java, when declaring an array, the size should not be specified in the square brackets.
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.
Join the Discussion
Login to post a comment or share your explanation.
LoginA. 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