Question

Given an empty stack s, an input array a = {1,2,3,4}. What is printed when the...

Given an empty stack s, an input array a = {1,2,3,4}. What is printed when the following function is executed?

public void pushAndPop(Stack s, int[] a){

s.push(a[0]);

s.push(a[1]);

System.out.println(s.pop());

s.push(a[2]);

System.out.println(s.pop());

s.push(a[3]);

System.out.println(s.pop());

System.out.println(s.pop());

}

A.2, 1, 4, 3

B.2, 3, 1, 4

C.1, 2, 3, 4

D.2, 3, 4, 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:-

OPTION (D) 2, 3, 4, 1

Explanation :- first statement define that ,push first element of array in the stack means push 1 in the stack so top of the stack will be 1

second statemnet define that ,push second element of array in the stack , means push 2 in the stack so top of the stack will be 2

third statement pop the element from stack ,means pop the top element of stack .currently top element of stack is 2 so poped 2 in stack

fourth statement define that ,push third element of array in the stack , means push 3 in the stack so top of the stack will be 3

fifth statement pop the element from stack , means pop the top element of stack . currently top element of stack is 3 so poped 3 in stack

sixth statement define that ,push fourth element of array in the stack , means push 4 in the stack so top of the stack will be 4

seventh statement pop the element from stack , means pop the top element of stack . currently top element of stack is 4 so poped 4 in stack

eightth statement pop the element from stack , means pop the top element of stack . currently top element of stack is 1 so poped 1 in stack

  

  

  

Add a comment
Know the answer?
Add Answer to:
Given an empty stack s, an input array a = {1,2,3,4}. What is printed when the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • A stack S of integers initially contains the following data: 6 (top) 2 7 3 The...

    A stack S of integers initially contains the following data: 6 (top) 2 7 3 The following code is then executed: int x = S.pop(); int y = S.pop(); int z = S.pop(); S.push(x + y); int w = S.pop(); S.push(w + z); After this code has been executed, what are the contents of the stack? A. 8 10 B. 10 8 C. 15 3 D. 10 8 6 3

  • Help with c++ program. The following code takes an infix expression and converts it to postfix....

    Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15...

    Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15 points The purpose of this lab is to implement and test a static and dy namic stack Part 1: Static Stack In this part, you are going to design a stack of characters. Assume a simple static array implementation. Complete the code below as specified by the comments below const int MAX- 5 // define an alias for the element type class Stack private:...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Data structures: Assume we have a stack named s. What will be the values stored in...

    Data structures: Assume we have a stack named s. What will be the values stored in s after the following operations(from bottom to top)? s.push(1); s.push(5); s.push(6); s.pop(); s.push(3); s.push(4); s.push(8); s.pop(); s.pop();

  • Please help JAVA Create the following in Stack.java: An array of ints set to size 10...

    Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...

  • Please answer question 8.35 Python 3.0 for language Comment for each part of line of code...

    Please answer question 8.35 Python 3.0 for language Comment for each part of line of code explaining what is being done 8.35 A stack is a sequence container type that, like a queue, supports very restrictis methods: All insertions and removals are from one end of the stack, typicly aces the top of the stack. Implement container class Stack that implements cked to a a subclass of object, support the len) overloaded operator, and support the me be referred a...

  • What is the output from running the following code snippet. void mystery(Queue<int>& q) { Stack<int> s;...

    What is the output from running the following code snippet. void mystery(Queue<int>& q) { Stack<int> s; while (!s.isEmptyQueue()) { s.push(q.front()); q.deleteQueue(); } while (!s.isEmptyStack()) { q.addQueue(2 * s.top()); s.pop(); } } Queue q; q.addQueue(8); q.addQueue(4); q.addQueue(18); q.addQueue(7); q.addQueue(5); mystery(q); cout << "["; while (!.isEmptyQueue()) { cout << " " << q.front(); } cout << " ]" << endl;

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT