In: Computer Science
What is the entry returned by the peek() method
after the following operations on a stack:
push(A), push(R),
pop(),
push(D), pop(),
push(L), pop(),
push(J), push(S),
pop(), pop()
Group of answer choices
A
S
L
D
J
Which of the following is true about the Stack interface we implemented in class?
Group of answer choices
You can only pop from the top of the stack but you can peek at any entry on the stack
The bottom item in a stack is the last item added
The first item added to a stack is the first one removed
To access the item at the bottom of the stack we have to pop() all items on top of it
None of the above is true
The postfix expression corresponding to a + (c - d) / (b * r) is
Group of answer choices
a c d - b r * / +
a c d - + b r * /
a + c d - / b r *
a c + d * b r - /
none of the above
The postfix expression a b * c + d - is equivalent to the following infix expression
Group of answer choices
d - c + b * a
d - (c + b * a)
a * ( b + c - d)
a * b + c - d
None of the above
Question 1:
What is the entry returned by the peek() method
after the following operations on a stack:
push(A), push(R),
pop(),
push(D), pop(),
push(L), pop(),
push(J), push(S),
pop(), pop()
Answer 1:
Stack abstract data type follows last in first out approach to process data.
The sequece of elements in the stack after each operations:
push(A)
Stack: A
push(R)
Stack: R A
pop()
Stack: A
push(D)
Stack: D A
pop()
Stack: A
push(L)
Stack: L A
pop()
Stack: A
push(J)
Stack: J A
push(S)
Stack: S J A
pop()
Stack: J A
pop()
Stack: A
Now on peek(), A will be returned.
-----------------------------------------------------------
Question 2:
Which of the following is true about the Stack interface we implemented in class?
You can only pop from the top of the stack but you can peek at any entry on the stack
The bottom item in a stack is the last item added
The first item added to a stack is the first one removed
To access the item at the bottom of the stack we have to pop() all items on top of it
None of the above is true
Answer 2:
True options is:
To access the item at the bottom of the stack we have to pop() all items on top of it
-----------------------------------------------------------
Question 3:
The postfix expression corresponding to a + (c - d) / (b * r)
Answer 3:
Sequence No. | Input Expression | Stack | Postfix Expression |
1 | a | a | |
2 | + | + | a |
3 | ( | +( | a |
4 | c | +( | ac |
5 | - | +(- | ac |
6 | d | +(- | acd |
7 | ) | + | acd- |
8 | / | +/ | acd- |
9 | ( | +/( | acd- |
10 | b | +/( | acd-b |
11 | * | +/(* | acd-b |
12 | r | +/(* | acd-br |
13 | ) | +/ | acd-br* |
14 | + | acd-br*/ | |
15 | acd-br*/+ |
So, the postfix expression will be: a c d - b r * / +
-----------------------------------------------------------
Question 4:
The postfix expression a b * c + d - is equivalent to the following infix expression
Answer 4:
Sequence No. | Input Expression | Stack | Infix Expression |
1 | a | a | |
2 | b | ab | |
3 | * | (a*b) | (a*b) |
4 | c | (a*b)c | (a*b) |
5 | + | ((a*b)+c) | ((a*b)+c) |
6 | d | ((a*b)+c)d | ((a*b)+c) |
7 | - | ((a*b)+c) - d | ((a*b)+c) - d |
So, the infix expression is a * b + c - d
-----------------------------------------------------------