Quiz: MicroPython Fundamentals II — Functions & the FrameBuf Module
Test your understanding of decisions, reusable functions, bit-level operations, and the frame buffer every face in this book is drawn into.
1. What is a docstring?
- A comment starting with
#placed above a function definition - A special MicroPython keyword that validates a function's parameters
- A string returned by every function that has no explicit
returnstatement - A triple-quoted string written as the first line inside a function's body, describing what it does
Show Answer
The correct answer is D. The docstring convention places a short triple-quoted description directly after the def line, documenting the function's purpose, parameters, and return value. MicroPython does not execute it as a command — it is a string literal — but its fixed position lets tools and other programmers find it instantly. Option A describes an ordinary comment, which is a different mechanism.
Concept Tested: Docstring Convention
See: Docstring Convention
2. In an if / elif / else chain, how many branches run?
- Exactly one — the first branch whose condition is
True, or theelseif none are - Every branch whose condition evaluates to
True - All branches run, but only the last one's output is displayed
- None run unless an
elsebranch is present
Show Answer
The correct answer is A. MicroPython checks each condition from the top down, runs the first branch that evaluates to True, and skips every branch after it. The else branch is a catch-all with no condition of its own, running only when every earlier test failed. That is why battery_level = 45 prints the "normal" message and never reaches the else.
Concept Tested: Conditional Statement
3. Given def set_brightness(level=80):, what does calling set_brightness() with no argument do?
- Raises an error, because a required argument is missing
- Sets
leveltoNoneand skips the function body - Uses 80 as the value of
level - Prompts the user to type a value into the REPL
Show Answer
The correct answer is C. A default parameter value is a fallback written into the function definition, used automatically whenever the caller omits that argument. Calling set_brightness() prints 80, while set_brightness(50) overrides the default with 50. This pattern lets common cases stay short while still allowing a caller to be specific.
Concept Tested: Default Parameter Value
4. If value = 0b00000101, what does value << 2 produce?
- 7
- 20
- 2
- 10
Show Answer
The correct answer is B. Shifting left by one position doubles a number, so shifting left by two positions multiplies by 2 twice: 5 × 2 × 2 = 20, or 0b00010100 in binary. Option C is the result of value >> 1, which halves the number and discards any remainder, and option D would be a single left shift rather than two.
Concept Tested: Bit Shifting
See: Thinking in Bits
5. A global variable eye_size = 10 is assigned eye_size = 20 inside a function, but after the function runs, printing eye_size still shows 10. Why?
- The function returned before reaching the assignment line
- Global variables in MicroPython cannot hold integer values
- Printing happened before the function was called
- The assignment created a new local variable that shadowed the global and vanished when the function ended
Show Answer
The correct answer is D. Assigning to a name inside a function always creates a local variable, even when a global of the same name already exists. MicroPython issues no warning — it quietly uses the local copy, which disappears when the function returns. Adding global eye_size as the function's first line makes the assignment modify the global instead.
Concept Tested: Global Versus Local Scope
6. What is the key difference between print() and return inside a function?
print()is faster because it does not create a variablereturndisplays text on screen whileprint()stores it in memoryprint()only displays a value, whilereturnhands it back so the caller can store or reuse itreturncan only be used once per program, whileprint()can be used freely
Show Answer
The correct answer is C. A function that only prints leaves its caller with nothing to work with. return immediately exits the function and passes a value back, so it can be captured in a variable such as status = battery_status(45) and used later. That is why almost every useful function ends with a return statement rather than a print().
Concept Tested: Function Return Value
7. What is a frame buffer?
- An in-memory rectangle of pixels that a program draws into before sending the finished image to a display
- A queue of drawing commands waiting to be transmitted over the SPI bus
- A physical chip on the display module that stores the previous frame for comparison
- A limit on how many frames per second a MicroPython program may draw
Show Answer
The correct answer is A. A frame buffer is a scratchpad the size of the display, held entirely in the Pico's memory. A program draws every shape into it invisibly, then copies the whole thing to the screen in one step. Drawing this way avoids the flicker and half-finished shapes a viewer would otherwise see as each pixel changed.
Concept Tested: FrameBuf Module
8. What does center_x, center_y = screen_center(128, 64) assign, given that screen_center ends with return x, y?
center_xgets the tuple(64, 32)andcenter_ygetsNonecenter_xgets 64 andcenter_ygets 32center_xgets 128 andcenter_ygets 64- An error, because a function can only return one value
Show Answer
The correct answer is B. A return statement with comma-separated values bundles them into a tuple, and writing two variable names on the left of the equals sign unpacks that tuple in one line. Since 128 // 2 is 64 and 64 // 2 is 32, the center point is (64, 32). This (x, y) pattern reappears constantly once drawing code starts computing pixel positions.
Concept Tested: Multiple Return Values
9. If eye_size = 14 and battery = 82, what does print(f"Eye size: {eye_size}px, Battery: {battery}%") display?
Eye size: {eye_size}px, Battery: {battery}%Eye size: 14 px , Battery: 82 %Eye size: 14px, Battery: 82%f"Eye size: 14px, Battery: 82%"
Show Answer
The correct answer is C. The f prefix marks the literal as an f-string, so each expression inside curly braces is evaluated and its value inserted in place at the moment the string is built. Every other character appears exactly as written, with no extra spaces added. Without the f prefix, the braces would print literally as shown in option A.
Concept Tested: String Formatting
See: String Formatting
10. Why is drawing into a frame buffer preferable to sending each pixel change straight to the display?
- The viewer never sees half-finished shapes, because the completed image is copied to the screen in one step
- A frame buffer uses less of the Pico's memory than direct drawing does
- Only a frame buffer can store color, so direct drawing would be monochrome
- The display driver chip rejects any command that is not sent from a frame buffer
Show Answer
The correct answer is A. Because every shape is assembled invisibly in memory first, the screen only ever shows finished frames — eliminating the flicker and partial images that per-pixel updates would produce. A frame buffer actually costs memory rather than saving it, and the same framebuf module serves both the monochrome OLED and the color display in this book.
Concept Tested: FrameBuf Module