Menu
Question Index
...

What will be the output of the following program?

import java.util.*;

public class Swapping
{
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        System.out.println(list);
        for(int element1 = 0; element1 < list.size(); element1++)
        {
            for(int element2 = 0; element2 < list.size(); element2++)
            {
                Collections.swap(list, element1, element2);
            }
        }
        System.out.println("After swapping");
        System.out.println(list);
    }
}


[1, 2, 3, 4, 5]
After swapping
[5, 2, 3, 4, 1]
[1, 2, 3, 4, 5]
After swapping
[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]
After swapping
[3, 4, 5, 1, 2]
Compilation Error
Runtime Error

Doubts

Problems

Topic: Java Collection Algorithms

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App