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;

Answer: C. X.length() should be X.lengthIn Java, array length is a variable, not a method. So X.length() is incorrect. The correct form is X.length.

  1. A.

    X[P] = 5 should be X(P) = 5

  2. B.

    P <= X.length() should be P < X.length()

  3. C.

    X.length() should be X.length

  4. D.

    P++ should be P+1

Attempted by 288 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.

Explore the full course: Rssb Basic Computer Instructor

Loading lesson…