โ† All Python Flashcard Decks

Control Flow: Conditional Statements Flashcards

6 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Control Flow: Conditional Statements flashcards as text
  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") ```

    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.

  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?

    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.

  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?

    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.

  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") ```

    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.

  5. In Python, what is the primary purpose of the `elif` keyword in a conditional statement?

    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.

  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") ```

    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.

Written and edited by , founder of Practice Test Geeks, with AI assistance. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Editorial policy