Conditional Array Element Assignment
Which of the following lines correctly replaces the missing line to safely assign “Welcome” to messages[0] only if it is currently null?
public class InitializeObjectArray {
public static void main(String[] args) {
String[] messages = new String[3];
// Missing line: Safely assign "Welcome" to the first element if it is null.
if(messages[0] == null){
messages[0] = "Welcome";
}
System.out.println(messages[0]);
}
}
A
if(messages[0] != null){ messages[0] = “Welcome”; }
B
messages[0].equals(“Welcome”);
C
if(messages[0] == null){ messages[0] = “Welcome”; }
D
messages[0] = new String(“Welcome”);
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | b.bao.o | 2 | 2 | 0m 00s | 200 |
| #2 | bommasam000 | 3 | 3 | 2m 07s | 173 |
| #3 | singhris000 | 1 | 1 | 0m 15s | 85 |
| #4 | sathilak000 | 1 | 1 | 0m 17s | 83 |
| #5 | suhanakochhar006 | 1 | 1 | 0m 39s | 61 |
| #6 | geethasailaja | 1 | 3 | 0m 52s | 28 |
| #7 | sailajavadlamani33 | 1 | 2 | 2m 10s | -40 |
| #8 | y.seong2027 | 1 | 1 | 19m 36s | -1,076 |
APFIVE