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
Which of the following is the correct syntax for an `if` statement with multiple conditions using logical operators?
Answer: if x > 0 and x < 10:
Python uses the keyword `and` (lowercase) to combine boolean conditions, not `&&` or `AND`.
What is the result of the following code? def mystery(n): if n <= 1: return n return mystery(n - 1) + mystery(n - 2) print(mystery(4))
Answer: 3
This is a recursive Fibonacci function; mystery(4) = mystery(3) + mystery(2) = 2 + 1 = 3.
What is printed by the following code? for i in range(10): if i % 2 == 0: continue if i > 6: break print(i)
Answer: 1 3 5
Odd numbers are printed; `continue` skips evens and `break` stops when i exceeds 6, so 7 never prints.
Which of the following correctly describes positional arguments?
Answer: Arguments matched by their position in the function call
Positional arguments are matched to parameters based on the order they appear in the function call.
What is the output of the following? def greet(name, msg='Hello'): print(msg, name) greet('Bob') greet('Alice', 'Hi')
Answer: Hello Bob\nHi Alice
First call uses the default msg='Hello' with name='Bob'; second call overrides msg with 'Hi' and name='Alice'.
What happens when a `return` statement is reached inside a function?
Answer: The function exits immediately and optionally sends back a value
A `return` statement immediately terminates the function's execution and optionally returns a value to the caller.
What is the output of the following code? i = 1 while True: print(i) i += 1 if i > 3: break
Answer: 1 2 3
The loop prints 1, 2, 3; after printing 3, i becomes 4 which satisfies `i > 3` and `break` exits the loop.