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 --- # What is a program? Rank order the different phases the Python interpreter goes to when interpreting your code. 1. Detect lexical errors as encoding mismatch 2. Check the syntactic structure of the code to detect ill-formed statements 3. Compile the Python source code into byte code to faster its execution 4. Load and execude the code in the Python virtual machine # What is a program? How to improve the following code to make it easier to understand and extend? ```python from typing import List l : List[int] = [5, 4, 8, 9, 0, -5, 1, 3] i : int = 1 j : int = 0 while i < len(l) - 1: if l[i] < l[len(l) - 1]: l[j] += l[i] l[i] = l[j] - l[i] l[j] = l[j] - l[i] j += 1 i += 1 l[j] += l[len(l) - 1] l[len(l) - 1] = l[j] - l[len(l) - 1] l[j] = l[j] - l[len(l) - 1] ``` - [ ] Use more parentheses and brackets - [x] Add comments - [x] Use more explicit names for the variables - [x] Avoid having at two places (within and after the iteration) three lines of code doing the same thing - [ ] Regroup the statements on a same line - [ ] Add semi-colon at the end of each line # What is a program? What is a program? - [ ] A series of random instructions - [x] A series of instructions that realize an algorithm - [ ] A set of hardware specifications - [x] A translation of an algorithm into a particular language - [ ] A collection of user inputs # What is a program? What does the syntax of a program depend on? - [ ] The hardware specifications - [ ] The operating system - [x] The programming language - [x] The specificities of the language being used - [ ] The algorithm’s complexity # What is a program? Which factors are important when choosing a programming language for a project? - [x] Adaptedness to the project - [x] Performance - [ ] The programming language ranking on a specific website - [x] Availability of libraries - [ ] Random selection
--- secondary_color: lightgray --- # Good programming practices What are the key components of writing a readable program? - [x] Proper indentation - [x] Meaningful variable names - [ ] Extensive use of global variables - [x] Appropriate comments - [ ] Ignoring the programming language syntax rules # Good programming practices What should be avoided when writing variable names? - [ ] Using meaningful names - [x] Names like `x1`, `x2`, `papa`, `maman` - [ ] Following naming conventions - [ ] Using snake_case in Python - [x] Non-descriptive names like `a` or `prog1` # Good programming practices Why is it important to avoid using hardcoded values in your program? - [x] It makes the code harder to update - [x] Using variables or constants makes the code more flexible - [ ] It affects the program's execution speed - [ ] Hardcoded values increase readability - [x] Variables allow easier modification of the code # Good programming practices Which of the following practices should be followed for maintainability? - [x] Adding comments to clarify the code’s logic - [ ] Using only global variables - [x] Structuring the code for reusability - [x] Writing clear documentation for future developers - [ ] Avoiding the use of functions # Good programming practices What are some recommendations for writing reliable code? - [x] Testing each functionality thoroughly - [x] Structuring code into atomic, independent functionalities - [ ] Avoiding the use of built-in functions - [ ] Using only global variables for simplicity - [x] Reusing built-in language functionalities whenever possible # Good programming practices Why is it important to comment on your code? - [x] To help collaborators understand the code - [ ] To make the code run faster - [x] To clarify complex or non-trivial logic - [x] To document the functionality and implementation strategies - [ ] To follow coding norms for consistency # Good programming practices What are the advantages of using local variables over global variables? - [x] Local variables reduce the risk of errors when reused - [x] Local variables make the code easier to debug - [ ] Global variables are easier to modify - [x] Local variables help keep the scope of the program clear - [ ] Local variables make the code less readable
--- secondary_color: lightgray --- # Assessing quality of a Python code What is type hinting in Python used for? - [x] Improving code readability - [ ] Optimizing program performance - [x] Indicating variable types explicitly - [ ] Replacing type annotations in Java - [x] Helping IDEs prevent errors # Assessing quality of a Python code Which of the following is a dynamically typed language? - [x] Python - [ ] Java - [ ] C++ - [ ] C# - [ ] Go # Assessing quality of a Python code What is the primary purpose of Pylint in Python development? - [x] To check if code follows PEP 8 guidelines - [ ] To automatically format code - [ ] To execute code more efficiently - [x] To give a grade to your Python code - [ ] To detect runtime errors # Assessing quality of a Python code What happens when type errors are found during a Mypy check? - [x] The errors are listed, identifying mismatches in argument types - [ ] The program is stopped from running - [x] It prevents hidden type errors in nested conditions - [ ] The program’s performance is optimized - [x] It helps with debugging by showing type inconsistencies # Assessing quality of a Python code What are the common usage practices for type hinting in Python? - [x] Type hinting for function arguments - [x] Type hinting for function return types - [ ] Typing every variable in the program - [x] Using libraries like `typing_extensions` for additional types - [ ] Completely replacing the need for dynamic typing
--- secondary_color: lightgray --- # Programming errors What are the main types of programming errors? - [x] Syntactic errors - [x] Runtime errors - [ ] Logical errors - [ ] Compilation errors - [ ] Typing errors # Programming errors What causes a syntactic error in a Python program? - [x] Incorrect code structure that violates Python's syntax rules - [x] Using invalid variable names - [x] Misplaced tokens or symbols in the code - [ ] Invalid data types used during execution - [ ] Accessing unavailable memory # Programming errors What can cause runtime errors in a Python program? - [x] Division by zero - [x] Unauthorized operations on variables - [x] Accessing an unavailable file - [ ] Incorrect indentation - [ ] Misspelled variable names # Programming errors Why might random numbers make debugging difficult in a program? - [x] The outcome of the program may vary with each execution - [ ] Random numbers always generate runtime errors - [x] Some executions might work fine while others fail unexpectedly - [ ] Random numbers are inherently unpredictable and cause instability - [x] Debugging a program with random elements can be inconsistent # Programming errors What can be done to ensure consistency in debugging when random numbers are involved? - [x] Set a seed for the random number generator - [x] Use a deterministic series of numbers initialized with a seed - [ ] Avoid using random numbers altogether - [ ] Ensure all values are predefined - [x] Identify the seed that causes errors and debug accordingly # Programming errors Which of the following are examples of syntactic errors? - [x] A missing colon in a `for` loop - [x] Invalid symbols like `for i j in range()` - [ ] Division by zero in the code - [ ] Accessing a file that doesn't exist - [x] Mismatched parentheses in function calls # Programming errors What is a best practice when using random numbers in a program? - [x] Set a specific seed to ensure reproducibility during debugging - [ ] Always avoid random number usage in important computations - [x] Use a known seed to ensure that all executions yield the same output - [ ] Use multiple random number generators without setting their seeds - [x] Report performance of random algorithms by averaging results over multiple runs