English

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:

  1. In the first line, the variable must be a[ ] and 5 must be inside square brackets as new int[5].
  2. 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?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - Review Insight [Page 237]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 3 Arrays (Single Dimensional and Double Dimensional)
Review Insight | Q 10. | Page 237
Advertisements
Share
Notifications



      Forgot password?
Use app×