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?
- if x equals 5:
- if(x = 5):
- if (x == 5)
- if x == 5:
- if x = 5 then:
Purpose of the else clause
What does the else keyword do in a Python conditional statement?
- Skips to the next iteration
- Starts a loop
- Executes code if the if condition is False
- Declares a variable
- Ends the program
Role of elif in conditionals
When using if, elif, and else in a Python program, what is the purpose of elif?
- It creates an infinite loop
- It is used to import libraries
- It restarts the loop
- It allows checking multiple alternative conditions
- It prints output to the screen
Basic for loop structure
Which statement will correctly iterate through the items of a list called fruits?
- for fruit with fruits:
- for fruit : fruits
- for fruit into fruits:
- for fruit in fruits:
- for each fruit over fruits:
Understanding the while loop
What will a while loop do in Python?
- Iterate over a fixed range of values
- Skip to the next block of code
- Raise a SyntaxError always
- Run only once no matter the condition
- Continue to execute as long as its condition is True
Effect of break statement
In a for or while loop, what does the break statement do?
- Pauses the program for user input
- Raises an exception
- Skips just the current iteration
- Restarts the current loop
- Immediately exits the nearest enclosing loop
Using continue in loops
What happens when the continue statement is executed inside a loop?
- It throws a NameError
- It doubles the loop variable
- It skips the remaining code in the current loop iteration
- It stops the loop completely
- It initializes the variable again
Purpose of the pass statement
Why would you use the pass statement inside a Python function or loop?
- To continue the loop conditionally
- To have a placeholder that does nothing
- To break out of the loop
- To print a value to the screen
- To declare a new variable
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?
- if x == 0 or x == 10:
- if x u003E 0 or x u003C 10:
- if (x = 0 and x = 10):
- if x u003E 0; x u003C 10:
- if 0 u003C x u003C 10:
Finding an error in loop code
What is wrong with this code: for i in range(5) print(i)
- range should not be used in Python
- print(i) should come before the loop
- The loop should end with pass
- A colon is missing after the for loop
- There should be parenthesis after range
Else with loops
In Python, an else clause after a loop is executed when:
- The loop has more than 5 iterations
- The loop completes normally without a break
- The loop iterates over an empty sequence
- The loop is a for loop only
- The loop contains no continue statements
Comparing if and while
Which of the following best describes the difference between an if statement and a while loop in Python?
- A while loop is used only for numbers; if works with strings
- While loops cannot use compound conditions
- An if statement must have an else, while loops never do
- If statements always require indentation, while loops do not
- An if statement checks a condition once; a while loop checks repeatedly
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?
- range()
- index()
- position()
- enumerate()
- count()
Selecting odd numbers using continue
How can you skip printing even numbers in a loop over a list of numbers nums?
- Use 'if num % 2 == 0: continue' inside the loop
- Use 'break' after checking if num is even
- Use 'if num // 2: continue'
- Replace continue with pass
- End the loop with 'else'
Syntax of nested if statements
Which of the following illustrates a nested if statement in Python?
- if x u003E 5 and if y u003C 10: print('Yes!')
- if x u003E 5:n if y u003C 10:n print('Yes!')
- if x u003E 5: if y u003C 10: print('Yes!')
- if (x u003E 5, y u003C 10): print('Yes!')
- if x u003E 5 elif y u003C 10: print('Yes!')