Question 63

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;
  1. AX [P] = 5 should be X (P) = 5
  2. BP<= X.length() should be P< X.length()
  3. CX.length() should be X.length
  4. DP++ should be P+1
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.

From DSSSB TGT Computer Science Practice Set 5

This is question 63 of DSSSB TGT Computer Science Practice Set 5. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.