Test Series - python

Test Number 68/108

Q: 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]
Solution: 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: 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
Solution: Refer documentation.
Q: Which module in the python standard library parses options received from the command line?
A. getopt
B. os
C. getarg
D. main
Solution: getopt parses options received from the command line.
Q: What is the type of sys.argv?
A. set
B. list
C. tuple
D. string
Solution: It is a list of elements.
Q: 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
Solution: Refer documentation.
Q: 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
Solution: Refer documentation.
Q: 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
Solution: Refer documentation.
Q: Where are the arguments received from the command line stored?
A. sys.argv
B. os.argv
C. argv
D. none of the mentioned
Solution: Refer documentation.
Q: 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]]]
Solution: append() returns None.

You Have Score    /9