Control Flow and Functions Flashcards
7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Control Flow and Functions flashcards as text
What does the `global` keyword do inside a function?
Answer: Allows the function to read and modify a global variable
The `global` keyword declares that a name inside a function refers to the module-level variable rather than creating a local one.
What is the output of the following? x = 5 def change(): x = 10 change() print(x)
Answer: 5
Without the `global` keyword, `x = 10` inside `change()` creates a local variable, leaving the global `x` unchanged at 5.
Which loop is guaranteed to execute its body at least once?
Answer: None — Python has no do-while, so neither guarantees one execution
Python does not have a do-while loop, so neither `for` nor `while` guarantees at least one execution; the condition is checked before the first iteration.
What is the output of the following? def counter(n): while n > 0: print(n) n -= 1 counter(3)
Answer: 3 2 1
The loop prints n starting at 3 and decrements until n reaches 0 (not printed), yielding 3, 2, 1.
What is a function's local scope?
Answer: Variables defined inside the function, not visible outside it
Local scope refers to variables defined within a function that are only accessible inside that function during its execution.
What does `range(5, 0, -1)` produce?
Answer: [5, 4, 3, 2, 1]
`range(5, 0, -1)` starts at 5, counts down by 1, and stops before reaching 0, yielding 5, 4, 3, 2, 1.
What is the output of the following code? for i in range(2): for j in range(2): print(i, j)
Answer: 0 0, 0 1, 1 0, 1 1
The nested loops iterate all combinations: (0,0), (0,1), (1,0), (1,1), each printed on a separate line.