Enhance your grasp of Python functions with this quiz focusing on syntax, usage, parameters, returns, and scope. Perfect for developers aiming to solidify foundational backend development skills in Python.
Which keyword is used in Python to define a function, as shown in `def my_function():`?
Explanation: The 'def' keyword is used in Python to define functions. The other options, 'func', 'define', and 'function', are not valid in Python for this purpose. While they may sound appropriate or be used in other languages, only 'def' is correct syntax for creating a new function.
In the definition `def greet(name):`, what is 'name' called?
Explanation: 'name' is called a parameter because it receives a value when the function is called. 'Variable' refers to any storage location but not specifically the input to a function. 'Keyword' and 'Operator' are unrelated terms that do not describe inputs in function definitions.
How do you correctly call a function named 'add_numbers' in Python?
Explanation: In Python, functions are called by writing their name followed by parentheses, such as 'add_numbers()'. 'call add_numbers' and 'use add_numbers()' are not valid Python syntax. 'add_numbers[]' uses square brackets, which are not used for function calls.
What does the 'return' keyword do inside a Python function?
Explanation: The 'return' keyword immediately exits a function and returns a value to the caller. It does not start or declare a function. 'Loops the function' is incorrect as 'return' is unrelated to loops.
What happens if a default parameter value is provided in a Python function definition?
Explanation: Setting a default value for a parameter makes it optional for the caller. Requiring the parameter, causing errors, or making the function not run are incorrect explanations for default arguments.
Given `def subtract(a, b):`, what does the call `subtract(5, 2)` assign to 'a' and 'b'?
Explanation: Positional arguments are assigned to parameters in order, so 'a' is 5 and 'b' is 2. Reversing the order or leaving parameters empty does not match how arguments are passed. There is no situation where positionally passed arguments leave parameters undefined.
What is the purpose of a triple-quoted string placed immediately after a function header?
Explanation: A triple-quoted string following a function header becomes the function's docstring, which serves as documentation. Comments, variables, and parameters are different concepts not represented by docstrings.
If you declare a variable inside a function, what is its scope?
Explanation: Variables declared inside a function are local and only exist within that function. They are not global, nor accessible from other modules or public throughout the program.
Which of the following correctly calls `def sum(a, b):` using keyword arguments?
Explanation: Using keyword arguments involves specifying parameter names and values in parentheses. The other options use incorrect syntax, such as missing parentheses, brackets, or improper separation of arguments.
What does the statement `return x, y` do in a Python function?
Explanation: Returning multiple values with commas in Python creates a tuple containing those values. Returning only x, raising an error, or returning their sum are incorrect interpretations.
What should you use if you want to define a function with no body yet?
Explanation: The 'pass' statement is used as a placeholder for function bodies that have no content yet. 'none', 'empty', and 'skip' are not valid Python statements for this purpose.
Which syntax lets a function accept any number of positional arguments?
Explanation: *args allows a function to accept a variable number of positional arguments in Python. The '$args', 'args*', and 'varargs' are incorrect and not valid syntax in Python.
How can you define an anonymous function that adds two numbers in Python?
Explanation: The 'lambda' keyword defines anonymous functions in Python. 'def' requires a function name, 'anon' and 'function' are not valid Python syntax for declaring anonymous functions.
Which of these is a valid Python function name?
Explanation: Function names must start with a letter or underscore and contain only letters, numbers, and underscores. Names starting with a number, containing dashes, or spaces are invalid in Python.
What is it called when a function calls itself within its own code?
Explanation: When a function calls itself, it is termed recursion, which is used to solve problems by breaking them into smaller similar problems. Iteration refers to repeating actions, inheritance is about classes, and overloading refers to using the same name for different functions.
If a function does not have a return statement, what does it return by default in Python?
Explanation: Functions without an explicit return statement return None by default. Returning zero, an empty string, or false only occurs if specifically coded.