Test Series - python

Test Number 67/108

Q: What is the type of each element in sys.argv?
A. set
B. list
C. tuple
D. string
Solution: It is a list of strings.
Q: 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
Solution: The first argument is the name of the program itself. Therefore the length of sys.argv is one more than the number arguments.
Q: 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]
Solution: Lists are passed by reference.
Q: 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
Solution: Refer documentation.
Q: 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
Solution: Zero keyword arguments may be passed if all the arguments have default values.
Q: 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
Solution: It is possible to pass function names as arguments to other functions.
Q: 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
Solution: It is possible to read the value of a global variable directly.
Q: 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
Solution: It is not possible to change the value of a global variable without explicitly specifying it.
Q: 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]
Solution: When a list is a default value, the same list will be reused.

You Have Score    /9