Monday, November 25, 2013

Question: Ternary Operator and Array

Given:

String[] elements = { "for", "tea", "too" };
String first = (elements.length > 0) ? elements[0] : null;
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The variable first is set to null.
D. The variable first is set to elements[0].

Execution:
Look carefully to syntax.
It is correct.
So element at Zero location i.e "for" will be assigned to first.
So answer will be D

Answer:
D. The variable first is set to elements[0].

No comments:

Post a Comment