Functions & Scope questions and answers
Arguments, defaults, *args, closures, lambda and recursion. Practise functions & scope questions from the python section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.
Sample functions & scope questions with worked answers
Question 1
In Python, what value does a function give back when its body ends without executing any return statement?
- 0
- An empty string
- It raises a SyntaxError
- None
Answer: D. None
Explanation: Every Python function returns something. If no return statement runs, the function returns the special object None.
Question 2
When Python looks up a name used inside a function, in which order does it search the scopes?
- Global, Local, Enclosing, Built-in
- Local, Enclosing, Global, Built-in
- Built-in, Global, Enclosing, Local
- Local, Global, Enclosing, Built-in
Answer: B. Local, Enclosing, Global, Built-in
Explanation: This is the LEGB rule: the function's own local scope first, then any enclosing function scopes, then the module's global scope, and finally the built-in names.
Question 3
Which statement about the nonlocal keyword in Python is correct?
- It makes a name refer to a variable of the nearest enclosing function, and it is a SyntaxError if no such variable exists
- It lets a nested function rebind a variable defined at module level
- It creates a new variable in the enclosing function if one does not already exist
- It is required even when a nested function only reads an enclosing variable
Answer: A. It makes a name refer to a variable of the nearest enclosing function, and it is a SyntaxError if no such variable exists
Explanation: nonlocal binds to an existing variable in an enclosing function scope, never to a module-level one (that is the job of global). It cannot create the variable, and it is only needed for assignment, not for reading.
All 20 functions & scope questions with answers
- A canteen bill function has two default parameters. What does this code print…
- The greet function below prints a message but has no return statement. What…
- In Python, what value does a function give back when its body ends without…
- A lambda is given a default value for its second parameter. What does this…
- The function below collects any number of marks using *marks. What is printed?
- 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…
- A function receives keyword arguments through **info. What does sorted(info)…
- 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…
- A default parameter value refers to a module-level variable that is changed…
- The recursive function below counts how many moves a number needs to reach 1…
- Which statement about the nonlocal keyword in Python is correct?
- Inside update, one list is changed with append and the other name is…
Functions & Scope: common questions
What are functions & scope questions?
Arguments, defaults, *args, closures, lambda and recursion. They appear in the python section of many placement tests.
Where can I practise functions & scope questions with answers for free?
Right here on LogicWiz Exams. Every functions & scope question has a worked explanation after you answer. Practice is free and needs no sign-up.
How should I practise functions & scope?
Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.
Start practising
On this page you can start a fresh set of functions & scope questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Python practice.
More python topics
- Variables, Types & Operators: Dynamic typing, numbers, operators, precedence and truthiness.
- Python Strings: Indexing, slicing, methods, formatting and immutability.
- Lists & Tuples: Slicing, methods, copies, sorting and unpacking.
- Dictionaries & Sets: Lookups, iteration, hashable keys and set operations.
- Conditions & Loops: if/elif, for, while, range, break, continue and loop else.
- Comprehensions & Generators: List, dict and set comprehensions, yield and lazy evaluation.
- Classes & Objects: Attributes, inheritance, super(), dunder methods and properties.
- Exceptions & Files: try/except/else/finally, raising errors and the with statement.
- Built-ins & Standard Library: sorted, zip, any/all, collections, itertools and imports.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep