Python Conditional Statements and Loops Essentials Quiz

  1. Identifying an if statement

    Which of the following is the correct syntax for an if statement that checks if a variable x is equal to 5?

    1. if x equals 5:
    2. if(x = 5):
    3. if (x == 5)
    4. if x == 5:
    5. if x = 5 then:
  2. Purpose of the else clause

    What does the else keyword do in a Python conditional statement?

    1. Skips to the next iteration
    2. Starts a loop
    3. Executes code if the if condition is False
    4. Declares a variable
    5. Ends the program
  3. Role of elif in conditionals

    When using if, elif, and else in a Python program, what is the purpose of elif?

    1. It creates an infinite loop
    2. It is used to import libraries
    3. It restarts the loop
    4. It allows checking multiple alternative conditions
    5. It prints output to the screen
  4. Basic for loop structure

    Which statement will correctly iterate through the items of a list called fruits?

    1. for fruit with fruits:
    2. for fruit : fruits
    3. for fruit into fruits:
    4. for fruit in fruits:
    5. for each fruit over fruits:
  5. Understanding the while loop

    What will a while loop do in Python?

    1. Iterate over a fixed range of values
    2. Skip to the next block of code
    3. Raise a SyntaxError always
    4. Run only once no matter the condition
    5. Continue to execute as long as its condition is True
  6. Effect of break statement

    In a for or while loop, what does the break statement do?

    1. Pauses the program for user input
    2. Raises an exception
    3. Skips just the current iteration
    4. Restarts the current loop
    5. Immediately exits the nearest enclosing loop
  7. Using continue in loops

    What happens when the continue statement is executed inside a loop?

    1. It throws a NameError
    2. It doubles the loop variable
    3. It skips the remaining code in the current loop iteration
    4. It stops the loop completely
    5. It initializes the variable again
  8. Purpose of the pass statement

    Why would you use the pass statement inside a Python function or loop?

    1. To continue the loop conditionally
    2. To have a placeholder that does nothing
    3. To break out of the loop
    4. To print a value to the screen
    5. To declare a new variable
  9. Multiple conditions with if

    How do you test whether a number x is both greater than 0 and less than 10 using a single if statement?

    1. if x == 0 or x == 10:
    2. if x u003E 0 or x u003C 10:
    3. if (x = 0 and x = 10):
    4. if x u003E 0; x u003C 10:
    5. if 0 u003C x u003C 10:
  10. Finding an error in loop code

    What is wrong with this code: for i in range(5) print(i)

    1. range should not be used in Python
    2. print(i) should come before the loop
    3. The loop should end with pass
    4. A colon is missing after the for loop
    5. There should be parenthesis after range
  11. Else with loops

    In Python, an else clause after a loop is executed when:

    1. The loop has more than 5 iterations
    2. The loop completes normally without a break
    3. The loop iterates over an empty sequence
    4. The loop is a for loop only
    5. The loop contains no continue statements
  12. Comparing if and while

    Which of the following best describes the difference between an if statement and a while loop in Python?

    1. A while loop is used only for numbers; if works with strings
    2. While loops cannot use compound conditions
    3. An if statement must have an else, while loops never do
    4. If statements always require indentation, while loops do not
    5. An if statement checks a condition once; a while loop checks repeatedly
  13. Getting the index in a for loop

    Which built-in Python function allows you to loop through both the index and the value in a list?

    1. range()
    2. index()
    3. position()
    4. enumerate()
    5. count()
  14. Selecting odd numbers using continue

    How can you skip printing even numbers in a loop over a list of numbers nums?

    1. Use 'if num % 2 == 0: continue' inside the loop
    2. Use 'break' after checking if num is even
    3. Use 'if num // 2: continue'
    4. Replace continue with pass
    5. End the loop with 'else'
  15. Syntax of nested if statements

    Which of the following illustrates a nested if statement in Python?

    1. if x u003E 5 and if y u003C 10: print('Yes!')
    2. if x u003E 5:n if y u003C 10:n print('Yes!')
    3. if x u003E 5: if y u003C 10: print('Yes!')
    4. if (x u003E 5, y u003C 10): print('Yes!')
    5. if x u003E 5 elif y u003C 10: print('Yes!')