Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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]

View Answer

Answer: a
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].




2 - Question

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

View Answer

Answer: a
Explanation: Refer documentation.




3 - Question

3. Which module in the python standard library parses options received from the command line?
a) getopt
b) os
c) getarg
d) main

View Answer

Answer: a
Explanation: getopt parses options received from the command line.




4 - Question

4. What is the type of sys.argv?
a) set
b) list
c) tuple
d) string

View Answer

Answer: b
Explanation: It is a list of elements.




5 - Question

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

View Answer

Answer: c
Explanation: Refer documentation.




6 - Question

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

View Answer

Answer: a
Explanation: Refer documentation.




7 - Question

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

View Answer

Answer: d
Explanation: Refer documentation.




8 - Question

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

View Answer

Answer: a
Explanation: The same object is modified in the function.




9 - Question

9. Where are the arguments received from the command line stored?
a) sys.argv
b) os.argv
c) argv
d) none of the mentioned

View Answer

Answer: a
Explanation: Refer documentation.




10 - Question

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]]]

View Answer

Answer: c
Explanation: append() returns None.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners