Find the output of the following Python code. def MessageGame(Msg, Nums): Msg…

2026

Find the output of the following Python code.

def MessageGame(Msg, Nums):
Msg = Msg + "!"
Nums.append(100)
print("Func Msg:", Msg) # Output 1A
print("Func List:", Nums) # Output 1B

Text = "Hello"
MyList = [10, 20]
MessageGame(Text, MyList)
print("Final Msg:", Text) # Output 1C
print("Final List:", MyList) # Output 1D

Show answer & explanation
  • In function MessageGame(Msg, Nums), Msg is an immutable (string) while Nums is a mutable (list).

  • Msg = Msg + "!""Hello" becomes "Hello!", but this is only a local change.

  • Nums.append(100)[10, 20] → [10, 20, 100] (this modifies the original list globally).

  • Outputs inside the function:

    • Func Msg: Hello!

    • Func List: [10, 20, 100]

  • After function call:

    • Text remains unchanged ⇒ Hello

    • MyList is modified ⇒ [10, 20, 100]

  • Final Outputs:

    • Final Msg: Hello

    • Final List: [10, 20, 100]

Explore the full course: Hpsc Pgt Computer Science