PrepAway - Latest Free Exam Questions & Answers

What is the value of the top element after these operations are executed?

You have a stack that contains integer values. The values are pushed onto the stack in the
following order: 2,4,6,8. The following sequence of operations is executed:
Pop
Push 3
Pop
Push 4
Push 6
Push 7
Pop
Pop
Pop
What is the value of the top element after these operations are executed?

PrepAway - Latest Free Exam Questions & Answers

A.
2

B.
3

C.
6

D.
7

9 Comments on “What is the value of the top element after these operations are executed?

  1. Paul says:

    Wouldn’t the answer be 6?

    Since we’re stacking, it’s “LIFO”, last in first off.
    Push = add to top of stack
    Pop = remove from top of stack

    3 was pushed on in the 2nd action and popped off in the 3rd.

    Pushed onto the stack: Pop: Push 3: Pop: Push 4: Push 6: Push 7: Pop: Pop: Pop:
    8 6 3 6 4 6 7 6 4 6
    6 4 6 4 6 4 6 4 6 4
    4 2 4 2 4 6 4 6 4 2
    2 2 2 4 6 4 2
    2 4 2
    2

    The remaining stack is:
    6
    4
    2

    and 6 is on the top of the stack.




    1



    0
  2. BlaBla says:

    Answer is 6 -_-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;

    namespace Rextester
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    Stack a = new Stack();
    a.Push(2);
    a.Push(4);
    a.Push(6);
    a.Push(8);
    foreach(var i in a){
    Console.WriteLine(i);
    }
    a.Pop();
    a.Push(3);
    a.Pop();
    a.Push(4);
    a.Push(6);
    a.Push(7);
    a.Pop();
    a.Pop();
    a.Pop();

    Console.WriteLine(“———————-“);
    foreach(var i in a){
    Console.WriteLine(i);
    }
    }
    }
    }




    1



    0
  3. DB7 says:

    C: 6
    of the original initialization, 8 is removed. Then, all subsequent additions are eventually removed also. Thus, 6 (second from head, just behind 8) from the original is now at head.




    1



    0

Leave a Reply