Python Multiple Choice Questions – Argument Parsing

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Argument Parsing”.

1. What is the type of each element in sys.argv?
a) set
b) list
c) tuple
d) string
View Answer

Answer: d
Explanation: sys.argv is a list in Python that contains command-line arguments passed to the script. Each element in sys.argv is of type string, regardless of whether it looks like a number or not. The first element (sys.argv[0]) is the script name.

2. What is the length of sys.argv?
a) number of arguments
b) number of arguments + 1
c) number of arguments – 1
d) none of the mentioned
View Answer

Answer: b
Explanation: sys.argv includes the name of the script as the first element (sys.argv[0]), followed by the actual command-line arguments. So, if there are n command-line arguments, the length of sys.argv will be number of arguments + 1.

3. What will be the output of the following Python code?

advertisement
def foo(k):
    k[0] = 1
q = [0]
foo(q)
print(q)

a) [0]
b) [1]
c) [1, 0]
d) [0, 1]
View Answer

Answer: b
Explanation: Lists in Python are mutable, so when the list q is passed to the function foo, the change made (k[0] = 1) modifies the original list. Therefore, after the function call, q becomes [1].
👉 Apply Now for Free C Certification - December 2025

4. How are keyword arguments specified in the function heading?
a) one-star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
View Answer

Answer: c
Explanation: In Python, keyword arguments (also known as **kwargs) are specified in the function heading using two asterisks (**) followed by a valid identifier. For example:

def func(**kwargs):
    print(kwargs)

This allows the function to accept an arbitrary number of keyword arguments as a dictionary.

advertisement

5. How many keyword arguments can be passed to a function in a single function call?
a) zero
b) one
c) zero or more
d) one or more
View Answer

Answer: c
Explanation: In Python, a function can accept zero or more keyword arguments during a single call. These keyword arguments are specified by explicitly naming the parameters (e.g., func(a=1, b=2)), and Python functions can handle any number of such arguments, including none.

6. What will be the output of the following Python code?

def foo(fname, val):
    print(fname(val))
foo(max, [1, 2, 3])
foo(min, [1, 2, 3])

a)

3
1

b)

1
3

c) error
d) none of the mentioned
View Answer

Answer: a
Explanation: The function foo takes a function fname and a value val as arguments. It then applies fname to val and prints the result.

  • foo(max, [1, 2, 3]) applies max() to the list, which returns 3.
  • foo(min, [1, 2, 3]) applies min() to the list, which returns 1.

So, the output is:

3
1

7. What will be the output of the following Python code?

def foo():
    return total + 1
total = 0
print(foo())

a) 0
b) 1
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: The function foo() returns total + 1. Even though total is not defined inside the function, it is defined in the global scope before the function is called. Python allows reading a global variable inside a function unless you try to assign to it. So total = 0 globally, and foo() returns 0 + 1 = 1.

8. What will be the output of the following Python code?

def foo():
    total += 1
    return total
total = 0
print(foo())

a) 0
b) 1
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: The code results in an error because inside the function foo(), you are trying to modify the variable total without declaring it as global. Python treats total as a local variable, but it’s being used before assignment, leading to an UnboundLocalError.

9. What will be the output of the following Python code?

def foo(x):
    x = ['def', 'abc']
    return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))

a) True
b) False
c) None
d) Error
View Answer

Answer: b
Explanation: The function foo(x) assigns a new list [‘def’, ‘abc’] to x, so x now points to a different object in memory. Therefore, the id(x) inside foo() will differ from id(q) outside the function, and id(q) == foo(q) will return False.

10. What will be the output of the following Python code?

def foo(i, x=[]):
    x.append(i)
    return x
for i in range(3):
    print(foo(i))

a)

[0]
[1]
[2]

b)

[0]
[0, 1]
[0, 1, 2]

c)

[1]
[2]
[3]

d) None of the mentioned
View Answer

Answer: b
Explanation: The default argument x=[] is evaluated only once when the function is defined, not each time it’s called. So the same list is used and modified across all calls to foo(). As a result, each call appends to the same list, and the list grows with each iteration.

[0]
[0, 1]
[0, 1, 2]

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.