Bird
0
0

Which of the following is the correct syntax to declare and initialize an array of strings with values "apple", "banana", "cherry"?

easy📝 Syntax Q3 of 15
Java - Arrays
Which of the following is the correct syntax to declare and initialize an array of strings with values "apple", "banana", "cherry"?
AString fruits[] = new String["apple", "banana", "cherry"];
BString[] fruits = new String[] {"apple", "banana", "cherry"};
CString[] fruits = new String[3] {"apple", "banana", "cherry"};
DString fruits = {"apple", "banana", "cherry"};
Step-by-Step Solution
Solution:
  1. Step 1: Understand array declaration and initialization

    In Java, to declare and initialize an array with values, you can use new String[] { ... } syntax.
  2. Step 2: Identify correct syntax

    String[] fruits = new String[] {"apple", "banana", "cherry"}; correctly declares and initializes the array with the given values.
  3. Final Answer:

    String[] fruits = new String[] {"apple", "banana", "cherry"}; -> Option B
  4. Quick Check:

    Array initialization with new keyword = String[] fruits = new String[] {"apple", "banana", "cherry"}; [OK]
Quick Trick: Use new Type[] {values} to initialize arrays explicitly [OK]
Common Mistakes:
  • Using new String[] with values inside brackets incorrectly
  • Omitting new keyword
  • Assigning array to single String variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes