Python MCQs on Argument Parsing: SET 2

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

Check Answer

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

 

12. 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

Check Answer

Answer: a
Refer documentation.

 

13. Which module in the python standard library parses options received from the command line?

a) getopt

b) os

c) getarg

d) main

Check Answer

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

 

14. What is the type of sys.argv?

a) set

b) list

c) tuple

d) string

Check Answer

Answer: b
It is a list of elements.

 

15. 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

Check Answer

Answer: c
Refer documentation.

 

16. 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

Check Answer

Answer: a
Refer documentation.

 

17. 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

Check Answer

Answer: d
Refer documentation.

 

18. 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

Check Answer

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

 

19. Where are the arguments received from the command line stored?

a) sys.argv

b) os.argv

c) argv

d) none of the mentioned

Check Answer

Answer: a
Refer documentation.

 

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

Check Answer

Answer: c
append() returns None.

Page  1  2

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment