Computer Applications

Arrange the following java statements in the correct order of execution to accept values into the array a[]:

(i) a[i]=sc.nextInt( );
(ii) int a[]=new int[10];
(iii) Scanner sc=new Scanner(System.in);
(iv) for(i=0;i<10;i++)

  1. (i), (ii), (iii), (iv)
  2. (ii), (iii), (iv), (i)
  3. (iv), (iii), (ii), (i)
  4. (iii), (i), (iv), (ii)

Java Arrays

3 Likes

Answer

(ii), (iii), (iv), (i)

Reason — First, the array a must be declared and initialized (int a[] = new int[10];). Then, the Scanner object sc is created to accept input (Scanner sc = new Scanner(System.in);). Next, a for loop (for(i=0; i < 10; i++)) is used to iterate over the array indices. Inside the loop, values are accepted from the user and stored in the array (a[i] = sc.nextInt();). This sequence ensures proper initialization and input handling.

The statements in the correct order of execution are as follows:

(ii) int a[]=new int[10];
(iii) Scanner sc=new Scanner(System.in);
(iv) for(i=0;i<10;i++)
(i) a[i]=sc.nextInt( );

Answered By

1 Like


Related Questions