Quiz#
This quiz covers the key concepts from Lecture 1: Course Introduction, including environment setup, the Python execution pipeline, Python variables, data types, mutability, and basic Python concepts.
Note
Instructions:
Answer all questions to the best of your ability.
Multiple choice questions have exactly one correct answer.
True/False questions require you to determine if the statement is correct.
Essay questions require short written responses (2-4 sentences).
Click the dropdown after each question to reveal the answer.
Multiple Choice#
Question 1
What is the primary role of the lexer in the Python execution pipeline?
Executes bytecode instructions one at a time.
Breaks source code into tokens (keywords, identifiers, operators, literals).
Converts the Abstract Syntax Tree into bytecode.
Validates syntax and builds a parse tree.
Answer
B – Breaks source code into tokens (keywords, identifiers, operators, literals).
The lexer (tokenizer) is the first stage of the execution pipeline. It reads the source code character by character and groups them into tokens like keywords, identifiers, operators, and literals.
Question 2
Which of the following best describes CPython?
A version of Python that compiles directly to machine code.
The reference implementation of Python, written in C.
A Python implementation optimized for microcontrollers.
A just-in-time compiler for Python code.
Answer
B – The reference implementation of Python, written in C.
CPython is the default and most widely used implementation of Python. The interpreter itself is written in C, which is why it’s called “CPython.” When you run python3, you’re running CPython.
Question 3
What is the output of the following code?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
[1, 2, 3][1, 2, 3, 4][4]An error occurs.
Answer
B – [1, 2, 3, 4]
When y = x is executed, both variables reference the same list object. Modifying the list through y also affects x because they point to the same memory location. This is called aliasing.
Question 4
Which of the following data types is mutable in Python?
intstrtuplelist
Answer
D – list
Lists are mutable, meaning their contents can be changed in place without creating a new object. Integers, strings, and tuples are immutable – any modification creates a new object.
Question 5
What does the id() function return in Python?
The data type of an object.
A unique integer representing the object’s memory address.
The size of an object in bytes.
The hash value of an object.
Answer
B – A unique integer representing the object’s memory address.
The id() function returns a unique identifier for an object, which in CPython corresponds to the object’s memory address. This ID remains constant for the object’s lifetime.
Question 6
What is the correct way to check if a variable x is None?
if x == None:if x is None:if x = None:if x.isNone():
Answer
B – if x is None:
Since None is a singleton (only one instance exists), using is for identity comparison is the correct and idiomatic approach. While == would work, is is more explicit and slightly faster.
Question 7
Which statement about Python’s dynamic typing is correct?
Variable types must be declared before use.
Types are checked at compile time.
A variable can be rebound to objects of different types during execution.
Dynamic typing prevents type-related runtime errors.
Answer
C – A variable can be rebound to objects of different types during execution.
In Python, variables are names that reference objects, and the type is associated with the object, not the variable. A variable can be reassigned to objects of any type during execution.
Question 8
What is the output of the following code?
a = 10
b = 10
print(a is b)
TrueFalse10An error occurs.
Answer
A – True
CPython caches small integers (typically -5 to 256) for efficiency. Since both a and b are assigned to 10, they reference the same cached integer object, so a is b returns True.
Question 9
Where does CPython cache bytecode for imported modules?
In the
__bytecode__directory.In the
__pycache__directory.In the system’s temporary folder.
Bytecode is never cached.
Answer
B – In the __pycache__ directory.
When a module is imported, CPython compiles it to bytecode and caches the .pyc files in a __pycache__ directory. This speeds up subsequent imports by avoiding recompilation.
Question 10
What is the purpose of a linter like Ruff?
To compile Python code to machine code.
To analyze code for potential errors, style violations, and code smells.
To execute Python scripts in debug mode.
To convert Python 2 code to Python 3.
Answer
B – To analyze code for potential errors, style violations, and code smells.
A linter like Ruff analyzes source code to detect potential bugs, enforce coding standards (like PEP 8), and identify code smells before runtime. It helps catch issues early in development.
Question 11
Which Python implementation would be best for running code on a microcontroller with limited RAM?
CPython
PyPy
MicroPython
Jython
Answer
C – MicroPython
MicroPython is specifically designed for microcontrollers and embedded systems with limited resources. It requires only about 256KB of RAM, making it ideal for devices like ESP32 or Raspberry Pi Pico.
Question 12
What is the output of the following code?
x = 10
print(id(x))
x = x + 1
print(id(x) == id(10))
The same ID is printed twice, then
True.Different IDs are printed, then
True.Different IDs are printed, then
False.An error occurs because integers don’t have IDs.
Answer
B – Different IDs are printed, then True.
Since integers are immutable, x + 1 creates a new integer object. The first id(x) shows the ID of 10, then after x = x + 1, x points to 11. However, id(10) returns the same ID as the original because 10 is still cached.
Question 13
According to PEP 8, which naming convention should be used for variables and functions?
camelCasePascalCasesnake_caseUPPER_CASE
Answer
C – snake_case
PEP 8, Python’s official style guide, recommends snake_case for variable and function names, PascalCase for class names, and UPPER_CASE for constants.
Question 14
What does the following code print?
def greet(name):
print(f"Hello, {name}")
result = greet("Alice")
print(result)
Hello, Alicefollowed byHello, AliceHello, Alicefollowed byNoneNonefollowed byHello, AliceAn error occurs.
Answer
B – Hello, Alice followed by None
The function greet() prints “Hello, Alice” but doesn’t explicitly return anything. In Python, functions without a return statement return None by default, so result is None.
Question 15
Which of the following is a valid Python variable name?
2fastmy-variableclass_private_var
Answer
D – _private_var
Valid Python variable names must start with a letter or underscore, and can contain letters, digits, and underscores. 2fast starts with a digit, my-variable contains a hyphen, and class is a reserved keyword.
True or False#
Question 16
True or False: In Python, variables are direct storage locations that hold values, similar to C++.
Answer
False
In Python, variables are names (references) bound to objects in memory, not direct storage locations. Unlike C++, the variable doesn’t hold the value directly – it points to an object that holds the value.
Question 17
True or False: The == operator compares object identity, while is compares values.
Answer
False
It’s the opposite: == compares values (equality), while is compares identity (whether two references point to the same object in memory).
Question 18
True or False: Python is sometimes called a “compiled interpreted language” because it compiles to bytecode, then interprets that bytecode.
Answer
True
Python first compiles source code to bytecode (an intermediate representation), then the interpreter executes that bytecode. The bytecode is not native machine code – it runs on the Python Virtual Machine.
Question 19
True or False: Strings in Python are mutable, meaning you can change individual characters after creation.
Answer
False
Strings in Python are immutable. You cannot change individual characters in a string. Any operation that appears to modify a string actually creates a new string object.
Question 20
True or False: The __pycache__ directory contains cached bytecode files to speed up future imports of modules.
Answer
True
When Python imports a module, it compiles the source to bytecode and caches it in __pycache__ as .pyc files. This allows faster subsequent imports by skipping the compilation step if the source hasn’t changed.
Question 21
True or False: In Python, True is a subclass of int and equals 1.
Answer
True
bool is a subclass of int in Python. True equals 1 and False equals 0. You can verify this with isinstance(True, int) which returns True, and True + True equals 2.
Question 22
True or False: The Global Interpreter Lock (GIL) in CPython allows true multi-threaded parallelism for CPU-bound tasks.
Answer
False
The GIL (Global Interpreter Lock) prevents true parallel execution of Python bytecode in threads. Only one thread can execute Python bytecode at a time, limiting parallelism for CPU-bound tasks. (Note: CPython 3.13+ offers experimental free-threaded builds.)
Question 23
True or False: When you assign b = a where a is a list, both a and b reference the same object in memory.
Answer
True
When you assign b = a where a is a list, both variables become references to the same list object in memory. This is called aliasing. Modifying the list through either variable affects both.
Question 24
True or False: Syntax errors in Python are caught during the runtime phase, not the compilation phase.
Answer
False
Syntax errors are caught during the compilation phase, before any code executes. The parser validates syntax while building the parse tree. Runtime errors (like NameError, TypeError) occur during execution.
Question 25
True or False: The None object in Python is a singleton, meaning there is only one instance of it in memory.
Answer
True
None is a singleton in Python – there is exactly one None object in memory. This is why we use is None instead of == None; we’re checking if a variable references that single None object.
Essay Questions#
Question 26
Explain the difference between the compilation phase and the runtime phase in Python’s execution pipeline. What types of errors are caught in each phase?
(2-4 sentences)
Answer Guidelines
Key points to include:
The compilation phase happens once when a module is first imported or run. It includes lexing, parsing, AST generation, and bytecode compilation.
Syntax errors are caught during compilation, before any code executes.
The runtime phase happens every time code executes. The interpreter reads and executes bytecode instructions.
Runtime errors (NameError, TypeError, ValueError, etc.) occur during execution when invalid operations are attempted.
Question 27
Describe the concept of aliasing in Python. Explain what happens when you assign one variable to another with mutable objects, and why this can lead to unexpected behavior.
(2-4 sentences)
Answer Guidelines
Key points to include:
Aliasing occurs when multiple variable names reference the same object in memory.
When you assign
b = awhereais a mutable object (like a list), both names point to the same object.Modifying the object through one name affects all aliases because they share the same underlying object.
To avoid this, create an independent copy using
b = a.copy()orb = list(a).
Question 28
Explain the difference between mutable and immutable objects in Python. Provide examples of each type and describe what happens when you modify them.
(2-4 sentences)
Answer Guidelines
Key points to include:
Immutable objects (int, float, str, tuple) cannot be changed after creation. Any modification creates a new object with a different ID.
Mutable objects (list, dict, set) can be modified in place. The object’s ID remains the same after modification.
Example: Adding to a list with
append()modifies the same object, butx = x + 1for an integer creates a new integer object.Understanding mutability is crucial for avoiding bugs with function arguments and variable assignments.
Question 29
Compare CPython with PyPy. Describe the key differences between these Python implementations and when you might choose one over the other.
(2-4 sentences)
Answer Guidelines
Key points to include:
CPython is the reference implementation, written in C. It has the best compatibility with C extensions and the largest ecosystem.
PyPy uses Just-In-Time (JIT) compilation, achieving significant speedups for long-running code by compiling hot paths to machine code.
Use CPython for general development and when you need C extension compatibility (NumPy, pandas, TensorFlow).
Use PyPy for performance-critical applications that don’t rely heavily on C extensions.
Question 30
Explain why Python uses the is operator for None comparisons instead of == . What is the difference between identity and equality comparison?
(2-4 sentences)
Answer Guidelines
Key points to include:
Noneis a singleton – only oneNoneobject exists in Python’s memory.The
isoperator checks identity (same object in memory), while==checks equality (same value).Since there’s only one
Noneobject, checking identity withisis the correct and idiomatic approach.Using
isis also slightly faster because it compares memory addresses directly rather than calling equality methods.