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?

  1. 0
  2. An empty string
  3. It raises a SyntaxError
  4. 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?

  1. Global, Local, Enclosing, Built-in
  2. Local, Enclosing, Global, Built-in
  3. Built-in, Global, Enclosing, Local
  4. 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?

  1. It makes a name refer to a variable of the nearest enclosing function, and it is a SyntaxError if no such variable exists
  2. It lets a nested function rebind a variable defined at module level
  3. It creates a new variable in the enclosing function if one does not already exist
  4. 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

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

Other subjects

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