Python Questions and Answers – Argument Parsing 2

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

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

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

Show Answer Answer:-A. [0]
Explanation A new list object is created in the function and the reference is lost. This can be checked by comparing the id of k before and after k = [1].


Q 2. How are variable length 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:-A. one star followed by a valid identifier
Explanation Refer documentation.


Q 3. Which module in the python standard library parses options received from the command line?
A. getopt
B. os
C. getarg
D. main

Show Answer Answer:-A. getopt
Explanation getopt parses options received from the command line.


Q 4. What is the type of sys.argv?
A. set
B. list
C. tuple
D. string

Show Answer Answer:-B. list
Explanation It is a list of elements.


Q 5. What is the value stored in sys.argv[0]?
A. null
B. you cannot access it
C. the program’s name
D. the first argument

Show Answer Answer:-C. the program’s name
Explanation Refer documentation.


Q 6. How are default arguments specified in the function heading?
A. identifier followed by an equal to sign and the default value
B. identifier followed by the default value within backticks (“)
C. identifier followed by the default value within square brackets ([])
D. identifier

Show Answer Answer:-A. identifier followed by an equal to sign and the default value
Explanation Refer documentation.


Q 7. How are required arguments specified in the function heading?
A. identifier followed by an equal to sign and the default value
B. identifier followed by the default value within backticks (“)
C. identifier followed by the default value within square brackets ([])
D. identifier

Show Answer Answer:-D.identifier
Explanation Refer documentation.


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

def foo(x):
x[0] = [‘def’]
x[1] = [‘abc’]
return id(x)
q = [‘abc’, ‘def’]
print(id(q) == foo(q))
A. True
B. False
C. None
D. Error

Show Answer Answer:-A. True
Explanation The same object is modified in the function.


Q 9. Where are the arguments received from the command line stored?
A. sys.argv
B. os.argv
C. argv
D. none of the mentioned

Show Answer Answer:-A. sys.argv
Explanation Refer documentation.


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

def foo(i, x=[]):
x.append(x.append(i))
return x
for i in range(3):
y = foo(i)
print(y)
A. [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
B. [[0], [[0], 1], [[0], [[0], 1], 2]]
C. [0, None, 1, None, 2, None]
D. [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]

Show Answer Answer:-C. [0, None, 1, None, 2, None]
Explanation append() returns None.

Comments

Leave a Reply

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

You cannot copy content of this page