Menu
Topics Index
...
`

Test List Fill And Frequency


If you need explanation Read this topic

If you need Answer Take test on this topic

Test List Fill And Frequency
What will be the output of the following program?
import java.util.*;

public class BigBoss {

    public static void main(String a[]) {
        List<String> list = new ArrayList<String>();
        list.add("one");
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        System.out.println("Before list: " + list);
        Collections.fill(list, "one");
        list.add("five");
        System.out.println("After list: " + list);
        System.out.println("Frequency: " + Collections.frequency(list, "one"));
    }
}

A. Before list: [one, one, two, three, four]
After list: [one, one]
Frequency: 2
B. Before list: [one, one, two, three, four]
After list: [one, one, one, one, one, five]
Frequency: 5
C. Before list: [one, one, two, three, four]
After list: [two, three, four, five]
Frequency: 0
D. Before list: [one, one, two, three, four]
After list: [one, one, five]
Frequency: 2
E. Compilation Error or 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 (b) part.here is concept of filling a list using collections.fill method.initially list of string type filled with "one","two",three"'"four","five".

Posted by Anuj Batham    2015-03-12 14:17:44


later with the help of collection.fill method list initialized with "one" and in next line one more element is added with string as "five"

Posted by Anuj Batham    2015-03-12 14:19:15


Answer is B....

Initially we are adding the elements to the list with values "one","one","two","three","four".
Printing list will print all the elements inside the ArrayList
Collections.fill method replaces all the elements in the specified list with the specified element.
So all the elements will be replaced by "one".
Next we are adding element "five" to the list.
Now printing list will print [one one one one one five]
Collections.frequency method will return the number of times the specified element is present in the specified list.
Since "one" element is present five times in the list,frequency is 5

Posted by Shashanka Mogaliraju    2015-03-12 15:06:13


ANS IS B

Posted by Mânïshå Mùlchåndânï    2015-03-12 17:23:43


This dose is now closed and the winners are  Shashanka Mogaliraju,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Cool Pawan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-13 01:34:41


© meritcampus 2019

All Rights Reserved.

Open In App