Multiple choice technology architecture

How do you declare and initialize a String array with two elements in a single line of code?

  1. String [] strArr = [“element 1”, “element 2];

  2. String [] strArr = {“element 1”, “element 2”};

  3. String [] strArr = (“element 1”, “element2”);

  4. String [] strArr = new String [“element 1” , “element 2”];

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java uses curly braces for array initialization, not square brackets or parentheses. The correct syntax is String[] strArr = {"element 1", "element 2"};. This creates an array with the specified elements. You can also use new String[]{"element 1", "element 2"} but the short form with braces is more concise.