Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.int a = new int (5);for (int i = 0; i < = 5; i++) a [i] = i;
Advertisements
Question
Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.
int a = new int (5);
for (int i = 0; i < = 5; i++) a [i] = i;
Answer in Brief
Advertisements
Solution
The errors are follows:
- In the first line, the variable must be a[ ] and 5 must be inside square brackets as new int[5].
- In the second line, the relational operator must be < instead of <=.
The correct statements are:
int a[] = new int[5];
for (int i = 0; i < 5; i++) a[i] = i;shaalaa.com
Is there an error in this question or solution?
