Self-assessment quiz

Presentation & objectives

The following quizzes are here to help you check that you understood the articles you had to study. They are provided for self-assessment and will not be graded or stored.

Don’t hesitate to reach out on the Discord server for any precision/explanation!

Quizzes

--- secondary_color: lightgray --- # Functions in a programming language --- shuffle_answers: false --- Why the use of functions is so important? - [ ] It makes your code faster to run. - [x] It makes your code more reusable. - [x] It makes your code easier to read. - [x] It makes your code easier to test. # Functions in a programming language What is true? - [ ] A python function always returns a value. - [X] A Python function can return multiple values. - [ ] Python executes a function when the `def` keyword is met. - [X] A function can be passed as an argument of another function. # Functions in a programming language Which call(s) is(are) correct considering the following function prototype? ```python def random_values (nb_points: int, min_v: int = 0, max_v: int = 100) -> List[int]: """ Returns a list of n random integers between minv and maxv (inclusive). In: * nb_points: The number of values to generate. * min_v: The minimum value for the random integers. * max_v: The maximum value for the random integers. Out: * A list of n random integers between min_v and max_v (inclusive). """ ``` - [ ] `l = random_values(5, 0, 100) -> a`. - [x] `l = random_values(5, 0, 100)`. - [x] `l = random_values(5)`. - [x] `l = random_values(5, 10)`. - [x] `l = random_values(, max_v=100, min_v=10, nb_points=5)`. - [x] `l = random_values(nb_points=5, min_v=10, max_v=100)`. - [ ] `l = random_values()`. - [ ] `l = random_values('100', '0', '100')`. - [ ] `l = random_values[5, 0, 100]`. # Functions in a programming language What is the output of the following code? ```python # Define a function def func (*args): s = "" for i in args: s += str(i) + "-" print(s) # Call it func('a', 3, 0.3) ``` - [x] `a-3-0.3-`. - [ ] `0.3-`. - [ ] An error is returned due to argument mismatch.
--- secondary_color: lightgray --- # Variables visibility and scope What is the output of the following code? ```python # Define a variable x = 30 # Define a function def my_func(): x = 20 print("D1:", x) # Call it my_func() print("D2:", x) ``` - [x] `D1: 20` et `D2: 30`. - [ ] `D1: 30` et `D2: 30`. - [ ] `D1: 20` et `D2: 20`. # Variables visibility and scope What are the main differences between local and global variables? - [x] Local variables are only visible within the function where they are defined. - [x] Global variables can be accessed from anywhere in the program. - [ ] Local variables exist throughout the program's execution. - [ ] Global variables are deleted after the function finishes executing. - [ ] Local variables are automatically global after they are used in a loop. # Variables visibility and scope What happens if you try to access a local variable outside its function? - [ ] The program will print the value of the variable. - [ ] The variable will become global. - [x] A `NameError` will occur. - [x] The interpreter will report that the variable is not defined. - [ ] The function will be called automatically. # Variables visibility and scope What will be printed if a local and global variable share the same name inside a function? - [x] The local variable will "hide" the global variable. - [ ] Both the global and local variables will be printed. - [ ] A runtime error will occur. - [ ] The global variable will override the local one. - [x] Only the local variable’s value will be printed inside the function.
--- secondary_color: lightgray --- # Organizing codes in libraries What is the main benefit of organizing code into modules? - [x] Reusability of code. - [ ] Increased program execution speed. - [x] Easier management and maintenance of code. - [x] Better structure and coherence. - [ ] Reduced memory usage. # Organizing codes in libraries How can you prevent certain parts of code from being executed when a file is imported as a module? - [ ] Use the `import` keyword. - [ ] Use a `global` declaration for the variables. - [x] Use the `if __name__ == "__main__":` construct. - [x] Include test cases in the main script only. - [ ] Use a `def` statement to encapsulate all the code. # Organizing codes in libraries Which Python variable contains the list of directories where Python looks for modules? - [ ] `os.path`. - [x] `sys.path`. - [ ] `__file__`. - [ ] `PYTHONPATH`. - [ ] `dir()`. # Organizing codes in libraries Why is it important to split large codebases into multiple files (modules)? - [x] It allows for better organization and separation of concerns. - [x] It improves code maintainability and reusability. - [ ] It reduces the size of each function. - [x] It makes testing individual components easier. - [ ] It eliminates the need for the `import` statement. # Organizing codes in libraries Which of the following are good practices for organizing code into libraries? - [x] Group related functions into separate files based on themes. - [ ] Avoid splitting code into multiple files for simplicity. - [x] Use meaningful file names to avoid name conflicts with Python's standard libraries. - [ ] Always include all code in one main file. - [x] Use documentation and comments to explain the purpose of each module.
--- secondary_color: lightgray --- # Installing Python modules What is `pip` used for in Python? - [x] Installing external packages and modules. - [ ] Running Python scripts. - [x] Managing Python package dependencies. - [ ] Compiling Python programs. - [x] Installing libraries from the Python Package Index (PyPI). # Installing Python modules What is a Python package? - [x] A collection of modules. - [ ] A single Python script. - [x] A directory that contains multiple related modules. - [ ] A file installed from the web. - [x] A package that allows using dotted module names. # Installing Python modules What does the following command do? `pip3 install numpy` - [x] Installs the NumPy package for Python 3. - [ ] Updates an existing version of NumPy. - [ ] Installs NumPy for Python 2 only. - [x] Fetches the package from PyPI and installs it. - [ ] Uninstalls NumPy from your system. # Installing Python modules Which command would you use to upgrade an already installed package? - [ ] `pip install --version latest`. - [ ] `pip upgrade `. - [x] `pip install --upgrade `. - [x] `pip3 install --upgrade `. - [ ] `pip update `. # Installing Python modules Why might `pip` fail to install a package even though the command is correct? - [x] The package is being installed for the wrong Python version. - [x] The package does not exist in the PyPI repository. - [ ] The system has too many installed modules. - [x] The Python executable being used is different from the one associated with pip. - [ ] Python is not installed on the system.