Python functions and variable scope exam review

This study set covers essential concepts of Python functions and variable scope, including definitions, examples, and common questions to help students prepare for exams.

KoalaAva4·24 flashcards·24 questions
collegecomputer_scienceprogramming
0
Known
1 / 24
0
Learning
Front

What is a function in Python?

Tap to flip
Back

A function is a block of reusable code that performs a specific task. It is defined using the 'def' keyword.

Tap to flip
Got it
Still learning

Quiz(24 questions)

Question 1 of 24

1. What is the purpose of a function in Python?

Terms in this Study Set(24)

Functions in Python(12)

What is a function in Python?

A function is a block of reusable code that performs a specific task. It is defined using the 'def' keyword.

What are parameters in a function?

Parameters are variables listed inside the parentheses in a function definition. They allow you to pass information into the function.

True or False: Functions can return multiple values.

True. Functions can return multiple values as a tuple, allowing the caller to receive several results at once.

Fill in the blank: Use the ______ keyword to define a function.

'def'

How do you call a function?

You call a function by using its name followed by parentheses, e.g., 'my_function()'.

What is the return value of a function?

The return value is the output that a function sends back to the caller using the 'return' statement.

Compare 'return' vs. 'print' in functions.

'return' sends a value back to the caller, while 'print' displays output to the console without returning anything.

Cause → Effect: What happens if you omit the return statement?

The function will return 'None' by default, meaning no value is sent back to the caller.

What is a default parameter?

A default parameter is a parameter that assumes a default value if no argument is passed during the function call.

How to define a function with default parameters?

Example: 'def greet(name='User'):' sets 'User' as the default value for 'name' if not specified.

What is the purpose of *args in a function?

'*args' allows a function to accept any number of positional arguments, packing them into a tuple.

Explain the use of **kwargs.

'**kwargs' allows a function to accept any number of keyword arguments, packing them into a dictionary.

Variable Scope(12)

What is a local variable?

A local variable is defined within a function and only accessible within that function's scope.

True or False: Global variables can be accessed anywhere in the code.

True. Global variables are defined outside any function and can be used throughout the entire module.

Define a global variable.

A global variable is declared outside of all functions and is available to all functions within the same module.

What does the nonlocal keyword do?

The nonlocal keyword allows you to assign a value to a variable in the nearest enclosing scope that is not global.

Local vs Global: What's the difference?

- Local: Accessible only within the function. - Global: Accessible throughout the entire module.

Fill in the blank: A __________ variable exists only within a specific block of code.

Local

What happens if you use a global variable in a function without declaring it?

Python treats it as a local variable unless declared with the 'global' keyword, leading to an UnboundLocalError.

Cause: Assigning a value to a variable inside a function.

Effect: The variable is treated as local unless explicitly marked as global.

True or False: Nonlocal variables can be used in nested functions.

True. Nonlocal variables help to access variables from the nearest enclosing scope, excluding globals.

What is the scope of a variable defined inside a loop?

The variable has a local scope, accessible only within the loop unless declared otherwise.

Demonstrate local variable scope with an example.

def example(): x = 5 example() print(x) # Raises NameError: name 'x' is not defined.

Global variable example.

x = 10 def display(): print(x) display() # Outputs: 10

Questions in this Study Set(24)

1. What is the purpose of a function in Python?

A.To encapsulate reusable code for specific tasks
B.To store global variables
C.To create loops
D.To manage memory allocation

2. What is the primary characteristic of a local variable?

A.Defined within a function and accessible only there.
B.Accessible anywhere in the module.
C.Available to all functions after definition.
D.Declared in the global scope.

3. Which of the following correctly defines a function named 'calculate'?

A.def calculate():
B.function calculate():
C.calculate() =>
D.calculate: def()

4. True or False: A global variable can be modified within a function without declaring it as global.

A.True
B.False
C.Only in nested functions
D.Only if defined as local first

5. What happens if you do not provide a parameter when calling a function with default parameters?

A.An error occurs
B.The default value is used
C.The function cannot execute
D.The parameter is set to None

6. What is the effect of using the nonlocal keyword in a nested function?

A.It allows modification of the nearest enclosing scope's variable.
B.It creates a new global variable.
C.It restricts access to local variables.
D.It only applies to global variables.

7. Which of the following is NOT a way to return multiple values from a function?

A.Returning a tuple
B.Returning a list
C.Returning a dictionary
D.Returning a string

8. Which of the following variables is scoped locally?

A.A variable declared inside a function.
B.A variable declared at the top level of a module.
C.A variable declared outside any function.
D.A variable declared in the global scope.

9. What does the *args parameter allow in a function definition?

A.To accept unlimited keyword arguments
B.To accept unlimited positional arguments
C.To enforce strict parameter counts
D.To define a default parameter

10. What happens if you refer to a variable before it is defined in a function?

A.Python raises a NameError.
B.It returns None.
C.The variable is treated as global.
D.It defaults to zero.

11. In what scenario is it better to use **kwargs instead of individual parameters?

A.When you know the exact number of arguments
B.When you want to pass a fixed number of keyword arguments
C.When you want to accept an arbitrary number of keyword arguments
D.When you want to avoid using any parameters

12. Identify the variable scope of a variable defined inside a for loop.

A.Local to the loop
B.Global to the module
C.Nonlocal to any enclosing function
D.Available throughout the program

13. Which statement correctly calls a function named 'display_message'?

A.display_message;
B.call display_message()
C.display_message()
D.invoke display_message

14. Which of the following is NOT true about global variables?

A.They can be accessed from any function.
B.They are defined outside of any function.
C.They can be redeclared inside a function.
D.They are temporary and get deleted after function execution.

15. True or False: A function can have the same name as a built-in function.

A.True, but it can cause conflicts
B.False, it is not allowed
C.True, and it will override the built-in function
D.False, built-in functions have priority

16. Fill in the blank: A __________ variable is accessible in all functions throughout a module.

A.Global
B.Local
C.Nonlocal
D.Private

17. What is the effect of using the return statement in a function?

A.It ends the function execution and sends a value back to the caller
B.It displays a message to the console
C.It creates a new variable
D.It does nothing and continues execution

18. What occurs when a local variable is assigned a value within a function?

A.It becomes a global variable.
B.It is treated as local for that function.
C.It overrides a global variable.
D.It is ignored by the interpreter.

19. What is a key difference between 'print' and 'return' in a function?

A.Print displays output while return sends a value back to the caller
B.Return displays output while print sends a value back to the caller
C.Both perform the same function
D.Print can only be used in global scope

20. True or False: A variable declared outside any function has local scope.

A.True
B.False
C.Only in nested functions
D.Depends on the function

21. How does Python handle a function that does not include a return statement?

A.It returns an empty string
B.It returns None by default
C.It raises an error
D.It continues without stopping

22. Which keyword is used to define a variable in the nearest enclosing scope that is not global?

A.nonlocal
B.local
C.global
D.enclosing

23. What is the role of parameters in a function?

A.To specify the name of the function
B.To store values for the function to use
C.To define the function's return type
D.To call other functions

24. Consider the following code snippet: def func(): x = 10; return x. What is the scope of x?

A.Local to func()
B.Global to the module
C.Nonlocal to any enclosing function
D.Accessible everywhere

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.