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?

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?

A.
2

B.
3

C.
6

D.
7



Leave a Reply 9

Your email address will not be published. Required fields are marked *


Paul

Paul

Wouldn’t the answer be 4?

Push them onto the stack:
8
6
4
2

Pop:
6
4
2

Push 3:

Paul

Paul

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.

PaulC

PaulC

this is correct.

this Q is more like an IQ test 🙂

R

R

C is the correct answer. 😀

noname

noname

the answer is c you madafaka :v

BlaBla

BlaBla

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);
}
}
}
}

DB7

DB7

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.