Multiple choice java

String[] elements = { "for", "tea", "too" }; 
String first = (elements.length > 0) ? elements[0] : null;    

What is the result?

  1. Compilation fails

  2. An exception is thrown at runtime

  3. The variable first is set to null

  4. The variable first is set to elements[0]

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

The array elements is initialized with three items, so its length is 3, which is greater than 0. The ternary operator evaluates the condition elements.length > 0 as true, so it assigns elements[0] to the variable first. No exceptions or compilation errors occur.