Menu
Topics Index
...
`

Collections1653


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1653
What will be the output of the following program?
import java.util.*;
public class Package {
    public static void main(String[] args) {
        String[] alpha = {"I", "A", "U", "O", "E", "X", "X"};
        List list = new ArrayList();
        for (String element : alpha)
            list.add(element);
        System.out.println(Collections.frequency(list, alpha[5]));
        Collections.rotate(list, 4);
        System.out.println(list);
        Collections.swap(list, 4, 2);
        System.out.println(list);
        System.out.println(Collections.nCopies(3, "X"));
        Collections.sort(list);
        System.out.println(list);
        System.out.println(Collections.unmodifiableList(list));
        Collections.fill(list, null);
        System.out.println(list);
    }
}

A. 2
[O, E, X, X, I, A, U]
[O, E, I, X, X, A, U]
[X, X, X]
[A, E, I, O, U, X, X]
[A, E, I, O, U, X, X]
[null, null, null, null, null, null, null]
B. 2
[U, O, E, X, X, I, A]
[U, O, E, I, X, X, A]
[U, O, E, I, X, X, A, X, X, X]
[A, E, I, O, U, X, X, X, X, X]
[A, E, I, O, U, X, X, X, X, X]
[A, E, I, O, U, X, X, X, X, X]
C. Output can not be determined
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java nCopies Collections - Collections.nCopies() Method

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.

Answer is A for the first print of frequency method it returns 2(frequency of X in list),and for the next print rotate method at index 4 i.e from O it rotates the list and prints it and next print statement swap method swaps by index positions X and I will be swapped and nCopies method print 3 copies of X and sort method sorts the list in alphabetical order and unmodifiablelist method makes the list as unmodifiable and prints it and next fill method replaces the list elements with null "object"so all nulls will be appeared in the last statement

Posted by Sai Avinash    2014-09-04 12:43:00


ans is D

Posted by Raju VarshnEy    2014-09-04 12:46:25


E. runtime error of ArrayIndeox out of bouds as array is out its limit here.

Posted by Shabaaz Shaikz    2014-09-04 12:47:44


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

Posted by Merit Campus    2014-09-05 05:21:26


© meritcampus 2019

All Rights Reserved.

Open In App