Skip to main contentSkip to solution

Find the output of the code:

answer=[ ]

output=' '

answer.append('F')

answer.append('I')

answer.append('L')

ch=answer.pop()

output=output+ch

ch=answer.pop()

output=output+ch

ch=answer.pop()

output=ch

print(“Result=”,output)

Solution

✅ Correct Option: 3

The list works as a stack, so pop() removes the last item each time, giving 'L', then 'I', then 'F'.

The first two pops build the string up to ' LI'. The last line is output=ch, not output=output+ch, so it replaces the whole string with 'F'. The program therefore prints Result= followed by F.


Option 2: this would be the result only if the last line were output=output+ch. It is an assignment, so the earlier characters are discarded.

Option 1: pop() removes from the end (last in, first out), not from the front, so the order is never F, I, L.

Option 4: 'L' is the first value popped, but it is overwritten later.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question