Self-assessment quiz
Presentation & objectives
The following quizzes are here to help you check that you understood the articles you had to study. At the end of a quiz, you will be given explanations on your answers. If some of them are wrong, you will have the possibility to click on the question you failed to try again.
These quizzes 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
What is a program?
# Rank order the different phases the Python interpreter goes through when interpreting your code.
1. Detect lexical errors as encoding mismatch
> 💬 This is the first phase where lexical errors, such as encoding issues or invalid tokens, are identified.
2. Check the syntactic structure of the code to detect ill-formed statements
> 💬 After lexical analysis, Python checks if the code syntax is correct, catching mistakes like missing colons or parentheses.
3. Compile the Python source code into byte code to fasten its execution
> 💬 Once the syntax is correct, Python compiles the source code into bytecode, which can be executed more efficiently.
4. Load and execute the code in the Python virtual machine
> 💬 Finally, the bytecode is executed by the Python virtual machine to run the 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
> ❌ Adding more parentheses or brackets won't necessarily improve the readability or maintainability of the code.
- [x] Add comments
> ✅ Adding comments is essential to make the purpose of each section clear for other developers or for future reference.
- [x] Use more explicit names for the variables
> ✅ Duplicating code makes it harder to maintain. Abstracting the repeated logic into a helper function would be a cleaner approach.
- [x] Avoid having at two places (within and after the iteration) three lines of code doing the same thing
> ✅ Correct! Duplicating code makes it harder to maintain. Abstracting the repeated logic into a helper function would be a cleaner approach.
- [ ] Regroup the statements on a same line
> ❌ Incorrect! Grouping statements on the same line would reduce readability. It's better to keep them separate for clarity.
- [ ] Add semi-colon at the end of each line
> ❌ Incorrect! Python does not require semi-colons at the end of each line, and adding them would be unnecessary and unconventional in Python code.
# What is a program?
- [ ] A series of random instructions
> ❌ A program must follow a logical structure and not consist of random instructions.
- [x] A series of instructions that realize an algorithm
> ✅ A program is a series of well-defined instructions that implement an algorithm.
- [ ] A set of hardware specifications
> ❌ This relates to hardware, not a software program.
- [x] A translation of an algorithm into a particular language
> ✅ A program is an algorithm expressed in a programming language.
- [ ] A collection of user inputs
> ❌ While programs can take user inputs, this alone does not define a program.
# What does the syntax of a program depend on?
- [ ] The hardware specifications
> ❌ Hardware doesn't define the syntax of a programming language.
- [ ] The operating system
> ❌ Syntax is independent of the operating system and is defined by the language itself.
- [x] The programming language
> ✅ The syntax is determined by the rules of the programming language being used.
- [x] The specificities of the language being used
> ✅ Each language has unique syntax rules that must be followed.
- [ ] The algorithm's complexity
> ❌ The complexity of an algorithm doesn't affect syntax, though it might influence code structure.
# Which factors are important when choosing a programming language for a project?
- [x] Adaptedness to the project
> ✅ Choosing a language suited to the project's requirements is essential.
- [x] Performance
> ✅ Depending on the project, performance could be a key factor in choosing the language.
- [ ] The programming language ranking on a specific website
> ❌ Popularity rankings do not necessarily reflect the suitability of a language for a particular project.
- [x] Availability of libraries
> ✅ Availability of libraries can significantly enhance productivity and code efficiency.
- [ ] Random selection
> ❌ Choosing a language randomly would not ensure that it meets the project's needs.
Good programming practices
# What are the key components of writing a readable program?
- [x] Proper indentation
> ✅ Indentation is crucial for structuring code in a readable way, especially in Python.
- [x] Meaningful variable names
> ✅ Clear and descriptive variable names help others understand the code more easily.
- [ ] Extensive use of global variables
> ❌ Global variables should be used sparingly to avoid complexity and maintainability issues.
- [x] Appropriate comments
> ✅ Comments help explain complex sections of code and provide clarity on implementation details.
- [ ] Ignoring the programming language syntax rules
> ❌ Syntax rules must always be followed to ensure the code runs correctly.
# What should be avoided when writing variable names?
- [ ] Using meaningful names
> ❌ Meaningful names are essential for clarity and maintainability.
- [x] Names like `x1`, `x2`, `papa`, `maman`
> ✅ Avoid using non-descriptive or confusing names that don't reflect the variable's purpose.
- [ ] Following naming conventions
> ❌ It's a good practice to follow naming conventions for consistency and readability.
- [ ] Using snake_case in Python
> ❌ Snake_case is the recommended naming convention for Python variables and functions.
- [x] Non-descriptive names like `a` or `prog1`
> ✅ Avoid using single letters or vague names that do not give insight into the variable's role.
# Why is it important to avoid using hardcoded values in your program?
- [x] It makes the code harder to update
> ✅ Hardcoded values can make future modifications difficult, as the values would need to be changed in multiple places.
- [x] Using variables or constants makes the code more flexible
> ✅ Replacing hardcoded values with variables or constants makes the code easier to modify and maintain.
- [ ] It affects the program's execution speed
> ❌ Hardcoded values generally do not have a direct impact on execution speed.
- [ ] Hardcoded values increase readability
> ❌ Hardcoded values often make the code harder to understand, especially in larger programs.
- [x] Variables allow easier modification of the code
> ✅ Variables and constants make it easy to update values across the program, reducing the risk of errors.
Assessing quality of a Python code
# What is type hinting in Python used for?
- [x] Improving code readability
> ✅ Type hinting makes it clear what type of data is expected, improving readability and reducing errors.
- [ ] Optimizing program performance
> ❌ Type hinting doesn't directly affect performance.
- [x] Indicating variable types explicitly
> ✅ Type hints make variable and function types explicit, making the code easier to understand and debug.
- [ ] Replacing type annotations in Java
> ❌ Type hinting in Python is different from Java’s static type system and does not replace it.
- [x] Helping IDEs prevent errors
> ✅ Type hints help IDEs and linters identify potential errors before running the program.
# Which of the following is a dynamically typed language?
1. [x] Python
> ✅ Python is a dynamically typed language, where variable types are determined at runtime.
1. [ ] Java
> ❌ Java is a statically typed language, where variable types are checked at compile time.
1. [ ] C++
> ❌ C++ is also statically typed.
1. [ ] C#
> ❌ C# is a statically typed language.
1. [ ] Go
> ❌ Go is statically typed as well.
Programming errors
# What are the main types of programming errors?
- [x] Syntactic errors
> ✅ Syntax errors occur when the code violates the grammar rules of the programming language.
- [x] Runtime errors
> ✅ Runtime errors occur when the program encounters a problem during execution, such as dividing by zero.
- [ ] Logical errors
> ❌ Logical errors are mistakes in the program's algorithm, not a formal category of programming errors like syntax or runtime errors.
- [ ] Compilation errors
> ❌ Python does not have a separate compilation phase; it is an interpreted language.
- [ ] Typing errors
> ❌ Type errors fall under runtime or logical errors, depending on when they occur.
# What causes a syntactic error in a Python program?
- [x] Incorrect code structure that violates Python's syntax rules
> ✅ Syntax errors occur when the structure of the code does not adhere to Python’s syntax rules.
- [x] Using invalid variable names
> ✅ Python has rules about valid variable names, and violating these rules results in a syntax error.
- [x] Misplaced tokens or symbols in the code
> ✅ Incorrect use of tokens (like missing parentheses or commas) can lead to syntax errors.
- [ ] Invalid data types used during execution
> ❌ This would cause a runtime error, not a syntax error.
- [ ] Accessing unavailable memory
> ❌ This would be a runtime error, not related to syntax.
# What can cause runtime errors in a Python program?
- [x] Division by zero
> ✅ Dividing by zero will cause a runtime error because it is an invalid operation in Python.
- [x] Unauthorized operations on variables
> ✅ Performing operations on variables that are not allowed can lead to runtime errors.
- [x] Accessing an unavailable file
> ✅ Trying to open a file that does not exist will result in a runtime error.
- [ ] Incorrect indentation
> ❌ Incorrect indentation would result in a syntax error, not a runtime error.
- [ ] Misspelled variable names
> ❌ Incorrect! This would result in a NameError during execution but is not categorized as a runtime error.
# Why might random numbers make debugging difficult in a program?
- [x] The outcome of the program may vary with each execution
> ✅ Random numbers lead to varying program outcomes, making it harder to consistently reproduce errors.
- [ ] Random numbers always generate runtime errors
> ❌ Random numbers don't inherently cause errors, though they may lead to variability in results.
- [x] Some executions might work fine while others fail unexpectedly
> ✅ This inconsistency is why random numbers make debugging more challenging.
- [ ] Random numbers are inherently unpredictable and cause instability
> ❌ Random numbers are unpredictable by nature but don't necessarily cause instability.
- [x] Debugging a program with random elements can be inconsistent
> ✅ Since the output varies with each execution, debugging can become inconsistent and unreliable.
# What can be done to ensure consistency in debugging when random numbers are involved?
- [x] Set a seed for the random number generator
> ✅ Setting a seed ensures that the random number generator produces the same sequence each time.
- [x] Use a deterministic series of numbers initialized with a seed
> ✅ This ensures consistency in program behavior for debugging purposes.
- [ ] Avoid using random numbers altogether
> ❌ Random numbers are often necessary, but setting a seed helps manage their unpredictability.
- [ ] Ensure all values are predefined
> ❌ While predefined values avoid randomness, using a seed can provide reproducibility without losing the randomness.
- [x] Identify the seed that causes errors and debug accordingly
> ✅ Knowing the seed allows you to consistently reproduce the error and effectively debug the issue.
# Which of the following are examples of syntactic errors?
- [x] A missing colon in a `for` loop
> ✅ A missing colon is a syntactic error, as it violates Python's syntax rules for loop declarations.
- [x] Invalid symbols like `for i j in range()`
> ✅ This syntax is invalid, as it is missing necessary elements like a comma between variables.
- [ ] Division by zero in the code
> ❌ Division by zero causes a runtime error, not a syntax error.
- [ ] Accessing a file that doesn't exist
> ❌ This would trigger a runtime error rather than a syntax error.
- [x] Mismatched parentheses in function calls
> ✅ Mismatched parentheses are a syntax error as they prevent proper parsing of the function call.
# What is a best practice when using random numbers in a program?
- [x] Set a specific seed to ensure reproducibility during debugging
> ✅ Setting a seed ensures that the random number generation can be repeated consistently.
- [ ] Always avoid random number usage in important computations
> ❌ Random numbers can be crucial in certain computations, but proper management (e.g., setting seeds) is important.
- [x] Use a known seed to ensure that all executions yield the same output
> ✅ A known seed guarantees reproducibility for testing and debugging.
- [ ] Use multiple random number generators without setting their seeds
> ❌ This would result in non-reproducible behavior, making debugging difficult.
- [x] Report performance of random algorithms by averaging results over multiple runs
> ✅ Randomized algorithms should be evaluated based on multiple runs to get an average performance.