Python Control Flow: Conditional Statements Questions and Answers — Questions and Answers
Question 1: What is the output of the following Python code snippet? ```python temperature = 25 if temperature > 30: print("Hot") elif temperature > 20: print("Warm") else: print("Cold") ```
- Hot
- Warm (Correct answer)
- Cold
- No output will be generated.
Correct answer: Warm
The code first checks if `temperature` (25) is greater than 30, which is false. It then proceeds to the `elif` condition, checking if 25 is greater than 20. This is true, so it prints "Warm" and exits the conditional block. The `else` block is never reached.
Question 2: Which of the following code snippets correctly uses the ternary operator to assign 'Adult' to the `status` variable if `age` is 18 or greater, and 'Minor' otherwise?
- status = age >= 18 ? 'Adult' : 'Minor'
- status = 'Adult' if age >= 18 else 'Minor' (Correct answer)
- status = if age >= 18: 'Adult' else: 'Minor'
- status = 'Adult' else 'Minor' if age >= 18
Correct answer: status = 'Adult' if age >= 18 else 'Minor'
The correct syntax for Python's ternary conditional operator is `value_if_true if condition else value_if_false`. This allows for a concise, one-line conditional assignment.
Question 3: A programmer wants to write a script to check for a specific error condition. The program should print 'Error Found' only if `error_code` is not 0 AND `is_critical` is True. Which of the following conditional statements accomplishes this?
- if error_code != 0 or is_critical == True: print("Error Found")
- if not (error_code == 0 and is_critical == True): print("Error Found")
- if error_code != 0 and is_critical: print("Error Found") (Correct answer)
- if error_code == 0 and is_critical == False: print("Error Found")
Correct answer: if error_code != 0 and is_critical: print("Error Found")
The `and` logical operator returns True only if both conditions on its left and right are true. The code `if error_code != 0 and is_critical:` correctly checks if the error code is non-zero and the `is_critical` flag is True. Comparing a boolean variable to `True` with `== True` is redundant.
Question 4: What will be printed to the console when the following Python code is executed? ```python x = 10 y = 20 if x > 5: print("A") if y > 15: print("B") elif x > 8: print("C") else: print("D") ```
- A B (Correct answer)
- A
- C
- A C
Correct answer: A B
The first condition `if x > 5` (10 > 5) is true, so the program enters its block and prints "A". Inside this block, there is a nested `if` statement. It checks `if y > 15` (20 > 15), which is also true, so it prints "B". Since the initial `if` statement was true, the `elif` and `else` blocks are skipped entirely.
Question 5: In Python, what is the primary purpose of the `elif` keyword in a conditional statement?
- To execute a block of code if the initial `if` condition is false.
- To check an additional condition only if all previous `if` and `elif` conditions were false.
- To create a nested level of conditional logic inside an `if` block.
- To check multiple distinct conditions in sequence without excessive nesting. (Correct answer)
Correct answer: To check multiple distinct conditions in sequence without excessive nesting.
The `elif` (else if) keyword allows you to check for multiple conditions sequentially. Python evaluates the `if` condition, then each `elif` condition in order, until one is found to be true. If none are true, the `else` block is executed. This avoids deeply nested `if` statements, making the code more readable.
Question 6: Consider the following code intended to classify a number. What is a potential issue with its logic? ```python num = 0 if num >= 0: print("Positive or Zero") if num == 0: print("Zero") else: print("Negative") ```
- The code will raise a SyntaxError.
- The `else` statement will never be reached.
- It will incorrectly print "Negative" for an input of 0.
- It will print two lines for an input of 0. (Correct answer)
Correct answer: It will print two lines for an input of 0.
This code snippet uses two separate `if` statements, not an `if-elif-else` chain. When `num` is 0, the first condition `num >= 0` is true, printing "Positive or Zero". The program then moves to the second, independent `if` statement. Its condition `num == 0` is also true, so it prints "Zero". The `else` is attached only to the second `if` and is not executed. The result is two lines of output.
What is the output of the following Python code snippet?
```python
temperature = 25
if temperature > 30:
print("Hot")
elif temperature > 20:
print("Warm")
else:
print("Cold")
```