Python Multiple Choice Questions – Argument Parsing

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

Q 1. What is the type of each element in sys.argv?
A. set
B. list
C. tuple
D. string

Show Answer Answer:-D. string
Explanation It is a list of strings.


Q 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

Show Answer Answer:-B. number of arguments + 1
Explanation The first argument is the name of the program itself. Therefore the length of sys.argv is one more than the number arguments.


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

def foo(k):
k[0] = 1
q = [0]
foo(q)
print(q)
A. [0]
B. [1]
C. [1, 0]
D. [0, 1]

Show Answer Answer:-B. [1]
Explanation Lists are passed by reference.


Q 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

Show Answer Answer:-C. two stars followed by a valid identifier
Explanation Refer documentation.


Q 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

Show Answer Answer:-C. zero or more
Explanation Zero keyword arguments may be passed if all the arguments have default values.


Q 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. 13
B. 3 1
C. error
D. none of the mentioned

Show Answer Answer:-B. 3 1
Explanation It is possible to pass function names as arguments to other functions.


Q 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

Show Answer Answer:-B. 1
Explanation It is possible to read the value of a global variable directly.


Q 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

Show Answer Answer:-C. error
Explanation It is not possible to change the value of a global variable without explicitly specifying it.


Q 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. Error
D. None

Show Answer Answer:-B. False
Explanation A new object is created in the function.


Q 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. [1] [1, 2] [1, 2, 3]

Show Answer Answer:-B. [0] [0, 1] [0, 1, 2]
Explanation When a list is a default value, the same list will be reused.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page