Checking for Null in an Array
Consider the following code segment.
String[] names = new String[3];
names[0] = "Alice";
names[1] = "Bob";
if (/* condition */) {
System.out.println(names[2].length());
}
Which of the following expressions should replace /* condition */ to ensure that a NullPointerException is not thrown when the code is executed?
A
if (names[2].length() > 0)
B
if (names[2] == null)
C
if (names[2] != null)
D
if (names[2].equals(null))
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | y.seong2027 | 1 | 1 | 0m 06s | 94 |
| #2 | sailajavadlamani33 | 1 | 1 | 1m 03s | 37 |
| #3 | bommasam000 | 2 | 5 | 2m 18s | 32 |
| #4 | sssemihunal | 0 | 1 | 0m 01s | -11 |
Items per page:
10
1 – 4 of 4
