PCEP Control Flow and Functions 3 — Questions and Answers
Question 1: What is a default parameter value in Python?
- A value required for every function call
- A value used when no argument is provided for that parameter (Correct answer)
- The return value of the function
- A global variable used inside a function
Correct answer: A value used when no argument is provided for that parameter
Default parameter values let you call a function without providing that argument, using the defined default instead.
Question 2: What is the output of the following? def f(x, y=2): return x * y print(f(3))
- 3
- 6 (Correct answer)
- 2
- Error
Correct answer: 6
With only one argument provided, `y` defaults to 2, so `f(3)` computes `3 * 2 = 6`.
Question 3: Which statement about `break` in a nested loop is correct?
- It exits all nested loops at once
- It exits only the innermost loop containing it (Correct answer)
- It skips the current iteration of the outer loop
- It raises a RuntimeError
Correct answer: It exits only the innermost loop containing it
`break` only terminates the innermost loop in which it is placed; outer loops continue normally.
Question 4: What is the output of the following code? for i in range(5): pass print(i)
- 4 (Correct answer)
- 5
- 0
- None
Correct answer: 4
After the loop, `i` retains the last value assigned to it, which is 4 (range stops before 5).
Question 5: Which of the following is a valid way to call a function using keyword arguments?
- greet(name='Alice', age=30) (Correct answer)
- greet('Alice', name=30)
- greet(name:'Alice')
- greet{name='Alice'}
Correct answer: greet(name='Alice', age=30)
Keyword arguments use `param=value` syntax within the function call parentheses.
Question 6: What happens if you use `else` with a `for` loop in Python?
- It runs if the loop encounters an error
- It runs if the loop completes without hitting a `break` (Correct answer)
- It always runs after the loop regardless
- It is a syntax error
Correct answer: It runs if the loop completes without hitting a `break`
The `else` clause of a `for` loop executes only if the loop finished normally without encountering a `break`.
Question 7: What is the output of this code? def add(a, b): return a + b result = add(2, 3) print(result * 2)
- 5
- 10 (Correct answer)
- 23
- Error
Correct answer: 10
`add(2, 3)` returns 5, and `5 * 2` equals 10.
What is a default parameter value in Python?