Python Functions and Recursion Quiz
28 Questions
1 Views

Python Functions and Recursion Quiz

Created by
@AdventurousJasper8246

Podcast Beta

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the purpose of the function randint(a, b) in the random module?

  • To create a list of random items
  • To calculate the length of a list
  • To generate a random integer between a and b inclusive (correct)
  • To sort a list of integers
  • A ZeroDivisionError occurs when a division by zero is attempted in a program.

    True

    What do you call the variables defined within a function's body?

    local variables

    When writing your own module, you need to create a new ._________ file containing functions and values.

    <p>py</p> Signup and view all the answers

    Match the following exceptions with their descriptions:

    <p>ZeroDivisionError = Occurs when a division by zero is attempted ImportError = Raised when there's an error importing a module IndexError = Triggered by trying to access an out-of-range index in a list SyntaxError = Occurs when the code cannot be parsed properly</p> Signup and view all the answers

    What technique involves a function calling itself?

    <p>Recursion</p> Signup and view all the answers

    A for-loop is essential for implementing recursion.

    <p>False</p> Signup and view all the answers

    In Python, a collection of code files with functions and values is called a ______.

    <p>module</p> Signup and view all the answers

    Match the following keywords with their function in Python modules:

    <p>import = To include a module in your code from = To import specific objects from a module as = To assign a different name to an imported module</p> <ul> <li>(asterisk) = To import all objects from a module</li> </ul> Signup and view all the answers

    Which statement is true regarding global and local variables?

    <p>Global variables can be accessed inside a function.</p> Signup and view all the answers

    It is encouraged to use * to import all objects from a module.

    <p>False</p> Signup and view all the answers

    def rec_sum(a_list):
    if a_list == [] :
      return 0
    else:
      return a_list[0] + rec_sum(a_list[1:])

    A function that sums the items in a list is utilizing ______.

    <p>recursion</p> Signup and view all the answers

    What must you do before calling a function in Python?

    <p>Define the function</p> Signup and view all the answers

    A variable defined inside a function can be accessed from outside that function.

    <p>False</p> Signup and view all the answers

    What do modules in Python primarily refer to?

    <p>A file containing Python statements and definitions</p> Signup and view all the answers

    What is the purpose of using the keyword 'global' in a function?

    <p>To access a global variable within the function.</p> Signup and view all the answers

    A function that does not take any arguments is called a function with _____ arguments.

    <p>zero</p> Signup and view all the answers

    Modules are imported using the command load module_name.

    <p>False</p> Signup and view all the answers

    What is the output of the following code: print(2 + 2)?

    <p>4</p> Signup and view all the answers

    Which of the following statements about local and global variables is true?

    <p>Local variables can only be accessed within the function where they are defined.</p> Signup and view all the answers

    What is a key benefit of defining functions in Python?

    <p>Code reuse and easier maintenance.</p> Signup and view all the answers

    ___ refer to a collection of functions and definitions in Python.

    <p>Modules</p> Signup and view all the answers

    Match the following Python components with their descriptions:

    <p>Function = A reusable piece of code Module = A file containing Python code Exception = An error that occurs during execution Statement = An instruction that Python can execute</p> Signup and view all the answers

    Match the following terms with their definitions:

    <p>Local Variable = A variable defined inside a function. Global Variable = A variable defined outside of all functions. Function Argument = A value passed to a function. Built-in Function = A function provided by Python by default.</p> Signup and view all the answers

    Which of the following statements about functions is false?

    <p>Functions must have a variable number of arguments.</p> Signup and view all the answers

    The syntax to define a function in Python starts with the keyword _____ followed by the function name.

    <p>def</p> Signup and view all the answers

    An exception in Python refers to a standard function.

    <p>False</p> Signup and view all the answers

    What is the purpose of using modules in Python?

    <p>To organize and encapsulate code for reuse and maintainability.</p> Signup and view all the answers

    Study Notes

    Function Motivation

    • Defining your own functions promotes code reusability
    • Easier to maintain large codebases

    Defining a Function

    • Use "def" keyword followed by function name and parenthesized arguments
    • Indented statement block follows the definition

    Zero, One or More Arguments

    • Functions can have zero arguments
    • Functions can have more than one argument
    • Arguments can be of different data types

    Local vs Global Variables

    • Variables declared inside a function are local
    • Local variables cannot be accessed outside the function
    • Global variables are defined outside of functions
    • Variables assigned values inside a function are local by default
    • "global" keyword allows access to a global variable inside a function

    Quiz 1

    • Output: c) 42 17 17 4 1 15 3 4

    Recursion

    • A function calling itsef to solve a task
    • Example: calculate the sum of items in a list without a for-loop
    • Use a "base case" to stop recursive calls
    • "Recursive case" handles the rest of the task

    Modules

    • Modules are pieces of code saved in .py files containing functions and values
    • Used for common tasks
    • Import modules using "import module_name"
    • Access module functions and values using "module_name.var"
    • Import specific functions using "from module_name import var"

    Quiz 3

    • Output: c) An error occurs

    Modules: Three Types

    • Preinstalled modules: accessible in the Python Standard Library
    • Self-written modules: create a .py file with the module name, import using the file name without extension
    • Third-party modules: installed by third-party sources, install using PIP

    Examples

    • Generate a list of n random integers between 0 and 10
    • Create a function "random_list" that takes n as input and returns a list of n random integers

    Exceptions

    • Occur when code encounters issues
    • Can be caused by incorrect code or input
    • Without exception handling, program terminates
    • Different exceptions are raised for different reasons
    • Common exceptions include ZeroDivisionError, ImportError, IndexError, NameError, SyntaxError

    Quiz 2

    • Output: 3. 4 is maximum

    Quiz 4

    • Output: Answers: Error message because variables c and d are not visible to the function mean() 35 35 4.0 53 53 4.0

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    Test your understanding of defining functions, argument types, and variable scopes in Python. This quiz also covers key concepts related to recursion, such as base and recursive cases. Get ready to assess your knowledge of these fundamental programming concepts!

    More Like This

    Python Functions Quiz
    60 questions

    Python Functions Quiz

    GenerousChrysoprase avatar
    GenerousChrysoprase
    Python Functions and String Manipulation
    8 questions
    Python Functions and Arguments
    40 questions
    Use Quizgecko on...
    Browser
    Browser