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)

Python Stack

1 Like

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.

Answered By

1 Like


Related Questions