Python MCQs on Argument Parsing: SET 1

Python MCQs on Argument Parsing

In this section, we are going to introduce you to the most frequently asked Python MCQs on Argument Parsing. These Python MCQs on Argument Parsing will be going to help you in Interviews and Exams.

 

1. What is the type of each element in sys.argv?

a) set

b) list

c) tuple

d) string

Check Answer

Answer: d
It is a list of strings.

 

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

Check Answer

Answer: b
The first argument is the name of the program itself. Therefore the length of sys.argv is one more than the number arguments.

 

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

Check Answer

Answer: b
Lists are passed by reference.

 

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

Check Answer

Answer: c
Refer documentation.

 

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

Check Answer

Answer: c
Zero keyword arguments may be passed if all the arguments have default values.

 

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

Check Answer

Answer: a
It is possible to pass function names as arguments to other functions.

 

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

Check Answer

Answer: b
It is possible to read the value of a global variable directly.

 

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

Check Answer

Answer: c
It is not possible to change the value of a global variable without explicitly specifying it.

 

9. What will be the output of the following Python code?

def foo(x):

x = [‘def’, ‘abc’]

return id(x)

q = [‘abc’, ‘def’]

print(id(q) == foo(q))

a) True

b) False

c) None

d) Error

Check Answer

Answer: b
A new object is created in the function.

 

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

Check Answer

Answer: b
When a list is a default value, the same list will be reused.

 

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