Menu
Question Index
...

What will be the output of the following program?

import java.util.*;

public class PushPop
{
    private LinkedList list = new LinkedList();

    public void push(Object v)
    {
        list.addFirst(v);
    }
    public Object pop()
    {
        return list.removeFirst();
    }
    public Object top()
    {
        return list.getFirst();
    }
    public static void main(String[] args)
    {
        PushPop stack = new PushPop();
        for (int i = 40; i >= 30; i--)
            stack.push(new Integer(i));
        System.out.print(stack.top() + " ");
        System.out.print(stack.pop() + " ");
        stack.push(10);
        System.out.print(stack.top() + " ");
        System.out.print(stack.pop() + " ");
        System.out.print(stack.pop());
    }
}


30 30 10 10 31
30 30 31 31 32
30 30 10 31 32
Compilation Error because list is declared as private
Compilation Error for some other reason

Doubts

Problems

Topic: Stack In Java - java.util.Stack Class

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App