Skip to content

Module 5 Chapter 3 Answers

Question 1

What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?

  • A) Start the statement with a “#” character
  • B) Underline all of the conditional code
  • C) Indent the line below the if statement
  • D) Begin the statement with a curly brace “{“

Answer: C) Indent the line below the if statement
Explanation: In Python, indentation is used to define the block of code under an if statement. Indented code is executed only when the condition is true.


Question 2

Which of these operators is not a comparison / logical operator?

  • A) ==
  • B) >=
  • C) =
  • D) >
  • E) !=

Answer: C) =
Explanation: = is an assignment operator, not a comparison operator. The correct operator for equality comparison is ==.


Question 3

What is true about the following code segment?

if x == 5 :
    print('Is 5')
    print('Is Still 5')
    print('Third 5')
  • A) Depending on the value of x, either all three of the print statements will execute or none of the statements will execute.
  • B) The string ‘Is 5’ will always print out regardless of the value for x.
  • C) The string ‘Is 5’ will never print out regardless of the value for x.
  • D) Only two of the three print statements will print out if the value of x is less than zero.

Answer: A) Depending on the value of x, either all three of the print statements will execute or none of the statements will execute.
Explanation: Since all print statements are indented under the if condition, they will execute only if x == 5. Otherwise, none will execute.


Question 4

When you have multiple lines in an if block, how do you indicate the end of the if block?

  • A) You capitalize the first letter of the line following the end of the if block
  • B) You use a curly brace { after the last line of the if block
  • C) You de-indent the next line past the if block to the same level of indent as the original if statement
  • D) You omit the semicolon ; on the last line of the if block

Answer: C) You de-indent the next line past the if block to the same level of indent as the original if statement
Explanation: Python uses indentation to define blocks of code. To end an if block, you simply remove the indentation for the next line.


Question 5

You look at the following text:

if x == 6 :
    print('Is 6')
    print('Is Still 6')
    print('Third 6')

It looks perfect but Python is giving you an ‘Indentation Error’ on the second print statement. What is the most likely reason?

  • A) Python randomly emits ‘Indentation Errors’ on perfectly good code.
  • B) Python thinks ‘Still’ is a misspelled word in the string.
  • C) Python has reached its limit on the largest Python program that can be run.
  • D) You have mixed tabs and spaces in the file.

Answer: D) You have mixed tabs and spaces in the file.
Explanation: Python requires consistent indentation. Mixing spaces and tabs can cause indentation errors.


Question 6

What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?

  • A) iterate
  • B) A closing curly brace followed by an open curly brace like this }{
  • C) otherwise
  • D) else

Answer: D) else
Explanation: The else keyword is used in Python to specify a block of code that runs if the if condition is false.


Question 7

What will the following code print out?

x = 0
if x < 2 :
    print('Small')
elif x < 10 :
    print('Medium')
else :
    print('LARGE')
print('All done')
  • A) Small
  • B) MediumAll done
  • C) Small Medium LARGE All done
  • D) Small All done

Answer: E) Small All done
Explanation: Since x = 0, the if condition x < 2 is true, so ‘Small’ is printed. The elif and else parts are skipped, and then ‘All done’ is printed.


Question 8

For the following code:

if x < 2 :
    print('Below 2')
elif x >= 2 :
    print('Two or more')
else :
    print('Something else')

What value of x will cause ‘Something else’ to print out?

  • A) x = 2.0
  • B) x = -2.0
  • C) This code will never print ‘Something else’ regardless of the value for x.
  • D) x = -22

Answer: C) This code will never print ‘Something else’ regardless of the value for x.
Explanation: The else block is unreachable because all values for x are covered by the if and elif conditions.


Question 9

In the following code, which will be the last line to execute successfully?

astr = 'Hello Bob'
istr = int(astr)
print('First', istr)
astr = '123'
istr = int(astr)
print('Second', istr)
  • A) 6
  • B) 3
  • C) 1
  • D) 2

Answer: C) 1
Explanation: The program will crash at istr = int(astr) (line 2) because 'Hello Bob' cannot be converted to an integer.


Question 10

For the following code:

astr = 'Hello Bob'
istr = 0
try:
    istr = int(astr)
except:
    istr = -1

What will the value be for istr after this code executes?

  • A) NaN
  • B) false
  • C) -1
  • D) A random number

Answer: C) -1
Explanation: The try block fails, and the except block sets istr to -1.