Functions & Scope · Question 9 · Medium
A function receives keyword arguments through **info. What does sorted(info) give here?
def show(**info):
return sorted(info)
print(show(city="Pune", age=21))
- ['age', 'city']
- ['city', 'age']
- [21, 'Pune']
- [('age', 21), ('city', 'Pune')]
Answer and explanation
Answer: A. ['age', 'city']
Explanation: **info collects the keyword arguments into the dictionary {'city': 'Pune', 'age': 21}. Iterating over a dictionary gives its keys, and sorted returns them as a list in alphabetical order.
Previous: The tag function uses a dictionary as a default parameter value. What do the… · Next: The hit function tries to increase a module-level counter without declaring it…
Practise functions & scope
Try a fresh set of functions & scope questions with an explanation after every answer, free and without signing up: Functions & Scope practice. Or sit a full placement mock test.
More functions & scope questions
- A function assigns to a name that also exists at module level. What does this…
- A function is defined as def book(src, dst, seats=1). Which of these calls is…
- The tag function uses a dictionary as a default parameter value. What do the…
- The hit function tries to increase a module-level counter without declaring it…
- This recursive function steps down by 2 on every call. What value is printed…
- A function is passed to another function as an argument. What does twice print…
- Two counters are built from the same closure factory. What does the last line…
- When Python looks up a name used inside a function, in which order does it…
- A function signature mixes normal parameters, *rest and a keyword-only…
- The enclosing variable x is reassigned after the inner function is defined but…
More python practice
Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge