Python Theory Questions
Python Theory Questions
Which of the following data structures is returned by the functions globals() and
locals()?
a) list
b) set
c) dictionary
d) tuple
Answer: c
Explanation: Both the functions, that is, globals() and locals() return value of
the data structure dictionary
2.On assigning a value to a variable inside a function, it automatically becomes a
global variable. State whether true or false.
a) True
b) False
Answer: b
Explanation: On assigning a value to a variable inside a function, t automatically
becomes a local variable. Hence the above statement is false.
3.______________ returns a dictionary of the module namespace.
________________ returns a dictionary of the current namespace.
a) locals()
globals()
b) locals()
locals()
c) globals()
locals()
d) globals()
globals()
Answer: c
Explanation: The function globals() returns a dictionary of the module namespace,
whereas the function locals() returns a dictionary of the current namespace.
4.If a is a dictionary with some key-value pairs, what does a.popitem() do?
a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary.
Answer: a
Explanation: The method popitem() removes a random key-value pair.
11.What does this mean: *args, **kwargs? And why would we use it?
Ans: We use *args when we aren�t sure how many arguments are going to be passed to
a function, or if we want to pass a stored list or tuple of arguments to a
function. **kwargs is used when we don�t know how many keyword arguments will be
passed to a function, or it can be used to pass the values of a dictionary as
keyword arguments. The identifiers args and kwargs are a convention, you could also
use *bob and **billy but that would not be wise.