Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
335 views
in Programming by (95.2k points)
closed by

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

1 Answer

0 votes
by (95.4k points)
selected by
 
Best answer
Correct Answer - Option 3 : X.length() should be X.length 

In java, for an integer variable X to find the length of the array we should use X.length instead of x.length().

Corect code:

int [] X = new int [10];       // x is an integer array

for (int P = 0; P < X.length; P++)      // so, X.length should be used and P < X.length

X [P] = 5; 

Important Point:

String X;

X.length() is used for String data type 

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...