Menu
Topics Index
...
`

Collections2102


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections2102
What will be the output of the following program?
import java.util.*;
public class Collect {
    public static void main(String[] args) {
        ArrayDeque ad = new ArrayDeque<>();
        ad.add(15);
        ad.add(25);
        ad.add(35);
        ad.add(45);
        ad.add(55);
        ad.add(65);
        ad.add(75);
        ad.add(85);
        System.out.print(ad.peek() + ", ");
        System.out.print(ad.peekFirst());
        ad.addFirst(5);
        ad.addLast(95);
        System.out.println(", " + ad);
    }
}

A. 95, 15, [95, 15, 25, 35, 45, 55, 65, 75, 85, 5]
B. 95, 15, [5, 15, 25, 35, 45, 55, 65, 75, 85, 95]
C. 15, 15, [5, 15, 25, 35, 45, 55, 65, 75, 85, 95]
D. 15, 15, [95, 85, 75, 65, 55, 45, 35, 25, 15, 5]
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java ArrayDeque Class

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 C....peek method retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty...it retrieves 15...then peekfirst method retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty..thus it retrieves 15 again..after that addfirst method adds the element at the beginning of the queue..thus adds 5..addlast method adds the element at the end of the queue..thus it adds 95..thus the result wud be...c.

Posted by Sneha Rapool    2014-01-10 09:37:26


ans is C...peek returns head of the queue..peekFirst is equivalent to peek...5 is added in front and 95 at last....

Posted by Santosh Munugota    2014-01-10 12:39:38


Congratulations Sneha Rapool. You are this dose winner. We will send you a link using which you can claim your recharge.

Posted by Merit Campus    2014-01-13 13:35:25


© meritcampus 2019

All Rights Reserved.

Open In App