LearnFrenzy
Sign in Take a free test

Functions · Predict the Output · Question 14

Passing a list and an int Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What is the output of the following Python program?

main.py
def modify(lst, n):
    lst.append(4)
    n += 1
    lst = [0]

nums = [1]
k = 1
modify(nums, k)
print(nums, k)
  • A [0] 2
  • B [1, 4] 2
  • C [1] 1
  • D [1, 4] 1 Correct
Correct Answer: Option D

append changes the caller's list. n += 1 and lst = [0] only rebind local names.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links