wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python: CH05 - If Statements MC

Total questions: 24

Worksheet time: 12mins

Name
Class
Date
1.

What is the fundamental purpose of a conditional test in Python?

a)

To assign a new value to a variable.

b)

To create a loop that repeats code.

c)

To examine a condition that evaluates to True or False.

d)

To define a new function.

2.

In Python, what is the operator used to check for equality (whether two values are the same)?

a)

=

b)

!=

c)

===

d)

==

3.

What is the difference between the single equal sign (=) and the double equal sign (==) in Python?

a)

= is used for comparison; == is used for mathematical operations.

b)

= is a statement (assignment); == is a question (comparison).

c)

= is used for integers; == is used for strings.

d)

There is no functional difference; they are interchangeable.

4.

The expression 'apple' == 'Apple' evaluates to which Boolean value in Python?

a)

True

b)

False

c)

SyntaxError

d)

None

5.

How can you perform a case-insensitive check for equality in Python, as demonstrated in the text?

a)

By using the case_ignore() method.

b)

By converting both strings to uppercase using the upper() method before comparison.

c)

By converting the variable's value to lowercase using the lower() method before comparison.

d)

By using the is_equal keyword.

6.

Which operator is used to check for inequality (whether two values are not the same)?

a)

!=

b)

>

c)

<

d)

==

7.

What is the purpose of the or keyword when checking multiple conditions?

a)

It passes only if both conditions are True.

b)

It passes only if the first condition is True.

c)

It passes when either or both of the individual tests pass.

d)

It reverses the result of the entire expression.

8.

Given age_0 = 22 and age_1 = 17, what is the result of age_0 >= 21 or age_1 >= 18?

a)

True

b)

False

c)

40

d)

None

9.

What is the purpose of the not keyword (although not explicitly shown in examples, it is a logical operator)?

a)

To combine two True conditions.

b)

To reverse the logical result of a conditional test.

c)

To prevent a line of code from executing.

d)

To define a variable that is not a Boolean.

10.

In a simple if statement, when is the indented block of code executed?

a)

Always.

b)

Only if the conditional test evaluates to False.

c)

Only if the conditional test evaluates to True.

d)

Never.

11.

What is the role of indentation in Python if statements and for loops?

a)

It makes the code look nicer, but does not affect execution.

b)

It is required for defining which lines of code belong to the conditional block.

c)

It separates the conditional test from the action.

d)

It is used only for comments and documentation.

12.

What does the else statement allow you to do in an if-else block?

a)

Test a second condition that is independent of the first.

b)

Define an action that runs only when the conditional test fails (is False).

c)

Define an action that runs only when the conditional test passes.

d)

Stop the program entirely.

13.

When is an if-elif-else chain most appropriate to use?

a)

When you only need to check one condition.

b)

When you have exactly two possible actions to take.

c)

When you need to test more than two possible situations and only want one block of code to execute.

d)

When you are combining multiple logical operators.

14.

In an if-elif-else chain, if the initial if test passes, what happens to the subsequent elif and else blocks?

a)

They are all executed sequentially.

b)

Python skips the rest of the tests in the chain.

c)

They are executed only if they contain a print() function.

d)

They are executed only if they have no errors.

15.

What does the keyword elif stand for?

a)

Else Fail

b)

Else If

c)

End If

d)

Execute If

16.

Why is it sometimes more concise to set a price variable inside an if-elif-else chain and use a single print() call afterwards?

a)

It makes the if statement run faster.

b)

It requires fewer lines of code total.

c)

It makes the code easier to modify because only one print() call needs to be changed to update the output message.

d)

It automatically adds the dollar sign to the output.

17.

What is the recommended practice for styling around comparison operators (like ==, <=, etc.) according to PEP 8 guidelines?

a)

No spaces should be used.

b)

Use a single space around comparison operators.

c)

Two spaces should be used after the operator.

d)

Use a newline after the comparison operator.

18.

Which keyword is used to check whether a specific value is already contained within a list?

a)

has

b)

inside

c)

in

d)

contains

19.

If requested_toppings = ['mushrooms', 'onions'], what is the result of 'pepperoni' in requested_toppings?

a)

True

b)

False

c)

Error

d)

mushrooms

20.

Which keyword is used to check if a value does not appear in a list?

a)

not in

b)

!in

c)

is not

d)

absent

21.

What happens when an empty list (e.g., requested_toppings = []) is used as a conditional test in an if statement?

a)

Python raises a TypeError.

b)

Python evaluates the list as False.

c)

Python evaluates the list as True.

d)

Python runs the code block anyway.

22.

Why is it important to use a series of independent if statements instead of an if-elif-else chain when checking requested pizza toppings?

a)

Because if-elif-else only works with numbers.

b)

Because more than one condition might be True, and you want to act on every condition that is True.

c)

Because if-elif-else is less efficient than independent if statements.

d)

Because independent if statements are easier to indent.

23.

What is a key purpose of combining if statements with a for loop when processing a list (like toppings)?

a)

To check if the list is empty.

b)

To watch for special values that need to be treated differently than the other items.

c)

To prevent the loop from running forever.

d)

To convert the list into a dictionary.

24.

In the example of checking available vs. requested toppings, if a requested topping is not in the list of available toppings, which block of code runs?

a)

The elif block.

b)

The next independent if statement.

c)

The for loop continues immediately.

d)

The else block inside the loop.