What correction is required for the following Java code snippet to compile?…
2017
What correction is required for the following Java code snippet to compile?
int [] X = new int [10];
for (int P = 0; P < X.length(); P++)
X [P] = 5;
- A.
X[P] = 5 should be X(P) = 5
- B.
P <= X.length() should be P < X.length()
- C.
X.length() should be X.length
- D.
P++ should be P+1
Attempted by 144 students.
Show answer & explanation
Correct answer: C
In Java, array length is a variable, not a method.
So X.length() is incorrect.
The correct form is X.length.