Menu
Topics Index
...
`

Arrays Are Useful


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays Are Useful
What will be the output of the following program?
import java.util.*;

public class ArraysAreUseful {

    public static void main(String[] args) {
        int a[] = new int[10];
        for (int i = 0; i < a.length; i += 2) {
            a[i] = i * 2;
        }
        print(a);
        for (int i = 0; i < a.length; i += 3) {
            a[i] = i * 3;
        }
        print(a);
        int[] b = Arrays.copyOf(a, 10);
        Arrays.sort(b);
        print(b);
        Arrays.fill(a, 5, 7, 6);
        print(a);
        Arrays.fill(b, 5, 6, 6);
        print(b);
    }

    private static void print(int[] a) {
        for (int elem : a) {
            System.out.print((elem == 0 ? "." : elem) + ".");
        }
        System.out.println();
    }
}

A. ....4...8...12...16...
....4.9.8...18...16.27.
........4.8.9.16.18.27.
....4.9.8.6.6...16.27.
........4.6.9.16.18.27.
B. 2...6...10...14...18...
3...6.12.10...21...18.30.
......3.6.10.12.18.21.30.
3...6.12.10.6.6...18.30.
......3.6.6.12.18.21.30.
C. ....4...8...12...16...
....4.9.8...18...16.27.
........4.8.9.16.18.27.
....4.9.8.6.6.6.6.6.
........4.6.6.6.6.6.
D. Compilation Error
E. Runtime Error
Topic: java.util.Arrays - Class Arrays In Collection Framework

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A. First time array a holds the values 0, 4 8 16 at indexes 0 2 4 6 and 8. Next it holds 0 4 9 18 16 and 27 at indexes 0 2 3 6 and 9. Next b is made as a copy of a and sorted. fill method places the third argument value from index 5 to 7 in a, i.e., fills 6 from index 5 to 7 and 6 in b from index 5 to 6.

Posted by Nikhil Sulibhavi    2014-05-14 13:22:11


Ans is d...the elements which are empty are replaced by a . which cannot be compared by a integer while applying sorting function in the array b[]..So,it results in a compilation error

Posted by Badam Swathi    2014-05-14 14:10:29


Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-15 04:32:21


© meritcampus 2019

All Rights Reserved.

Open In App