daniel_garcia
daniel_garcia 4d ago โ€ข 0 views

Multiple Choice Questions on 'If' Statements in Python for Beginners

Hey everyone! ๐Ÿ‘‹ Struggling with 'if' statements in Python? No worries, we've all been there! These are super fundamental for making your programs smart and reactive. I've put together a quick study guide and some practice questions to help you nail them down. Let's get coding! ๐Ÿš€
๐Ÿ’ป Computer Science & Technology
๐Ÿช„

๐Ÿš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

โœจ Generate Custom Content

1 Answers

โœ… Best Answer

๐Ÿ“š Quick Study Guide: Python 'If' Statements

  • ๐Ÿ‘‰ What are 'If' Statements? They allow your program to make decisions and execute different blocks of code based on whether a condition is true or false. Think of them as the brain of your code!
  • โœ… Basic Syntax: An if statement starts with the if keyword, followed by a condition, and ends with a colon (:). The code to be executed if the condition is true is indented below it.
    if condition:
        # code to execute if condition is True
  • โš ๏ธ Indentation is Crucial: Python uses indentation (whitespace) to define code blocks. Incorrect indentation will lead to an IndentationError. Typically, 4 spaces are used.
  • ๐Ÿ’ก elif and else:
    • โžก๏ธ elif (else if): Used to check multiple conditions sequentially. If the preceding if or elif condition is false, Python checks the next elif.
    • ๐Ÿ”š else: Catches all cases where none of the preceding if or elif conditions were true. It's optional and always comes last.
    if condition1:
        # code for condition1
    elif condition2:
        # code for condition2
    else:
        # code if neither condition1 nor condition2 is true
  • ๐Ÿง Comparison Operators: Used within conditions to compare values:
    • == (equal to)
    • != (not equal to)
    • < (less than)
    • > (greater than)
    • <= (less than or equal to)
    • >= (greater than or equal to)
  • ๐Ÿ”— Logical Operators: Combine multiple conditions:
    • โž• and: Both conditions must be true.
    • โž— or: At least one condition must be true.
    • ๐Ÿšซ not: Reverses the boolean result of the condition.

๐Ÿง  Practice Quiz: Python 'If' Statements

1. What will be the output of the following Python code?

x = 10
if x > 5:
    print("Hello")
if x < 12:
    print("World")
  1. Hello
  2. World
  3. Hello
    World
  4. Error

2. Which keyword is used to handle multiple conditions sequentially in an 'if' statement chain?

  1. else if
  2. elif
  3. then
  4. otherwise

3. What is the correct way to check if a variable age is between 18 and 65 (inclusive)?

  1. if age >= 18 and age <= 65:
  2. if 18 <= age <= 65:
  3. if age >= 18 or age <= 65:
  4. Both A and B

4. What will happen if the indentation is incorrect in an 'if' statement block?

x = 5
if x > 3:
print("Greater") # Incorrect indentation
  1. The code will run, but with unexpected output.
  2. It will print "Greater" if x is greater than 3.
  3. It will raise an IndentationError.
  4. It will be ignored by the interpreter.

5. Consider the following code:

num = 7
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

What will be the output?

  1. Even
  2. Odd
  3. Error
  4. Nothing (no output)

6. What is the output of this code?

a = 10
b = 20
if a > 15 or b > 15:
    print("Condition Met")
else:
    print("Condition Not Met")
  1. Condition Met
  2. Condition Not Met
  3. Error
  4. True

7. Which of the following conditions evaluates to True?

  1. not (5 > 3 and 10 < 5)
  2. "Python" == "python"
  3. (10 != 10) or (False)
  4. 3 + 2 == 6
Click to see Answers

๐Ÿ”‘ Answer Key:

  1. C. Both x > 5 (10 > 5 is True) and x < 12 (10 < 12 is True) are true, so both print statements execute.
  2. B. elif is the correct keyword for "else if" in Python.
  3. D. Both if age >= 18 and age <= 65: and if 18 <= age <= 65: are valid and correct ways to check if age is within the range. Python supports chained comparisons.
  4. C. Python uses indentation to define code blocks. Incorrect indentation will result in an IndentationError.
  5. B. num % 2 == 0 (7 % 2 == 0) is False, so the else block executes, printing "Odd".
  6. A. The condition a > 15 or b > 15 evaluates to (10 > 15) or (20 > 15) which is False or True, resulting in True. So, "Condition Met" is printed.
  7. A. Let's break it down: (5 > 3 and 10 < 5) is (True and False) which is False. Then not (False) is True.
    • B: "Python" == "python" is False due to case sensitivity.
    • C: (10 != 10) or (False) is (False) or (False) which is False.
    • D: 3 + 2 == 6 is 5 == 6 which is False.

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! ๐Ÿš€