Computer Science
Find the output of the following code:
answer = []; output = ''
answer.append('T')
answer.append('A')
answer.append('M')
ch = answer.pop()
output = output + ch
ch = answer.pop()
output = output + ch
ch = answer.pop()
output = output + ch
print("Result=", output)
Answer
Output
Result= MAT
Explanation
The code initializes an empty list answer and an empty string output. It appends the characters 'T', 'A', and 'M' to the answer list. After appending, answer list becomes ['T', 'A', 'M'].
After that, three pop() operations are performed on answer, removing and returning the last element each time ('M', 'A', and 'T' respectively). These characters are concatenated to the output string. So, output string becomes MAT which is printed as the final output.
Related Questions
State TRUE or FALSE for the following cases:
(a) Stack is a linear data structure.
(b) Stack does not follow LIFO rule.
(c) PUSH operation may result into underflow condition.
(d) In POSTFIX notation for expression, operators are placed after operands.
Find the output of the following code:
result = 0 numberList = [10, 20, 30] numberList.append(40) result = result + numberList.pop() result = result + numberList.pop() print("Result=", result)Write a program to reverse a string using stack.
For the following arithmetic expression:
((2 + 3) * (4 / 2)) + 2
Show step-by-step process for matching parentheses using stack data structure.