Conditional Array Element Assignment
The following code segment is intended to assign the string “Welcome” to the first element of the messages array, but only if that element is currently null. Which of the following code fragments should replace /* missing code */ to correctly implement this logic?
public class InitializeObjectArray {
public static void main(String[] args) {
String[] messages = new String[3];
/* missing code */
System.out.println(messages[0]);
}
}
A
messages[0].equals(“Welcome”);
B
if(messages[0] != null){ messages[0] = “Welcome”; }
C
messages[0] = new String(“Welcome”);
D
if(messages[0] == null){ messages[0] = “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 |
Items per page:
10
1 – 8 of 8
APFIVE