What correction is required for following Java code snippet to compile?
What correction is required for following Java code snippet to compile?
int [] X = new int [10];
for (int P = 0; P<= X.length(); P++)
X [P] = 5;
Show answerHide answer
Correct answer: (C) X.length() should be X.length
Explanation
In Java, length is a field of an array, not a method, so X.length() will not compile and must be X.length. The <= would only cause a runtime exception, not a compile error.