Related: Functions, Modules


Q1. What does *args collect?

  • A) Keyword arguments as a dict
  • B) Extra positional arguments as a tuple ✓
  • C) Global variables
  • D) Class attributes

Q2. What is the output?

  def func(a, b=2, *args):
    return a + b + sum(args)
print(func(1, 3, 4, 5))
  
  • A) 6
  • B) 13 ✓
  • C) Error
  • D) 15

a=1, b=3 (overrides default), args=(4, 5). Sum: 1+3+4+5=13.


Q3. What does a function return if there’s no return statement?

  • A) 0
  • B) None
  • C) Empty string
  • D) Error

Q4. What is a closure?

  • A) A closed file handle
  • B) A function that captures variables from its enclosing scope ✓
  • C) A private method
  • D) A type of import

Q5. What does from math import sqrt as square_root do?

  • A) Imports the entire math module
  • B) Imports sqrt with the alias square_root ✓
  • C) Creates a new function
  • D) Deletes sqrt from math

Q6. What is if __name__ == "__main__" used for?

  • A) Importing modules
  • B) Running code only when the file is executed directly ✓
  • C) Defining classes
  • D) Error handling

Q7. What is wrong with this code?

  def add_item(item, lst=[]):
    lst.append(item)
    return lst
  
  • A) Nothing
  • B) Mutable default argument — shared across calls ✓
  • C) Missing return type
  • D) Wrong parameter order

Q8. What does lambda x, y: x + y create?

  • A) A named function
  • B) An anonymous one-expression function ✓
  • C) A class
  • D) A generator

Q9. What is LEGB rule for name lookup?

  • A) Local → Enclosing → Global → Built-in ✓
  • B) Library → External → Global → Base
  • C) Loop → Expression → Global → Block
  • D) Only local scope matters

Q10. How do you install a third-party package?

  • A) python install package
  • B) pip install package
  • C) import install package
  • D) download package.py

Next: Intermediate Quiz | Functions Exercises