Topics Index

Swap Elements

If you need explanation Read this topic

If you need Answer Take test on this topic

Swap Elements
What will be the output of the following program?
import java.util.*;

public class SwapArrayList
    public static void main(String args[])
        ArrayList<String> list = new ArrayList<String>();
        Collections.swap(2, 4);
        System.out.println("Results after swap operation: " + list);

A. Results after swap operation: [players, viewers, cricket, play, watch]
B. Results after swap operation: [players, play, cricket, viewers, watch]
C. Results after swap operation: [players, play, watch, viewers, cricket]
D. Compilation Error
E. Runtime Error
Topic: Java Collection Algorithms

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 we have the AL Concept
AL is Advanced version of the Array where arrays are fixed in the size and arrays can store homogeneous elements where as AL are Growble in size and can store heterogeneous elements and AL can allow the null insertion and allows Duplicate elements and insertion order is preserved and AL implements RandomAccess interface
here we create the AL with the Strings so it will store only strings objects in it..and we have another class Collections in this class we have the some methods which can apply to the Collection classes..similar here we have the swap() which is used to swap the objects..but in this we are not specifying anything here it will take 3 arg list,startInd,endIndex but here we are not passing the list so error will raise..

Posted by Uday Kumar    2015-02-13 13:12:15


Posted by Shubham Bansal    2015-02-13 19:22:52

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-16 05:13:45

© meritcampus 2019

All Rights Reserved.

Open In App