Introduction to Programming Using Python, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors from the Instructor's Resource Website.

Chapter 4 Selections


Section 4.2 Boolean Types, Values, and Expressions
4.1  The "less than or equal to" comparison operator is __________.

A. <
B. <=
C. =<
D. <<
E. !=

4.2  The equal comparison operator is __________.
A. <>
B. !=
C. ==
D. =

4.3  The word True is ________.
A. a Python keyword
B. a Boolean literal
C. same as value 1
D. same as value 0

Section 4.3 Generating Random Numbers
4.4  To generate a random integer between 0 and 5, use ________________.
A. random.randint(0, 5)
B. random.randint(0, 6)
C. random.randrange(0, 5)
D. random.randrange(0, 6)

4.5  random.randint(0, 1) returns ____________.
A. 0
B. 1
C. 0 or 1
D. 2

4.6  random.random() returns ____________.
A. a float number i such that 0 < i < 1.0
B. a float number i such that 0 <= i < 1.0
C. a float number i such that 0 <= i <= 1.0
D. a float number i such that 0 < i < 2.0

Sections 4.4-4.10
4.7  Which of the following code displays the area of a circle if the radius is positive.
A. if radius != 0: print(radius * radius * 3.14159)
B. if radius >= 0: print(radius * radius * 3.14159)
C. if radius > 0: print(radius * radius * 3.14159)
D. if radius <= 0: print(radius * radius * 3.14159)

4.8  What is the output of the following code?

x = 0
if x < 4:
    x = x + 1

print("x is", x)
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4

4.9  Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
A. if isPrime = True:
B. if isPrime == True:
C. if isPrime:
D. if not isPrime = False:
E. if not isPrime == False:

4.10  Analyze the following code:

even = False
if even = True: 
    print("It is even!")
A. The program has a syntax error in line 1 (even = False)
B. The program has a syntax error in line 2 if even = True is not a correct condition. It should be replaced by if even == True: or if even:.
C. The program runs, but displays nothing.
D. The program runs and displays It is even!.

4.11  Analyze the following code.

even = False
if even:
    print("It is even!")
A. The code displays It is even!
B. The code displays nothing.
C. The code is wrong. You should replace if even: with if even == True:
D. The code is wrong. You should replace if even: with if even = True:

4.12  Suppose x = 1, y = -1, and z = 1. What will be displayed by the following statement?

if x > 0:
    if y > 0:
        print("x > 0 and y > 0")
elif z > 0:
    print("x < 0 and z > 0")
A. x > 0 and y > 0
B. x < 0 and z > 0
C. x < 0 and z < 0
D. nothing displayed

4.13  The following code displays ___________.

temperature = 50

if temperature >= 100:
    print("too hot")
elif temperature <= 40:
    print("too cold")
else:
    print("just right")
A. too hot
B. too cold
C. just right
D. too hot too cold just right

4.14  Analyze the following code:

Code 1:

if number % 2 == 0: 
    even = True
else: 
    even = False

Code 2:

even = number % 2 == 0
A. Code 1 has compile errors.
B. Code 2 has compile errors.
C. Both Code 1 and Code 2 have compile errors.
D. Both Code 1 and Code 2 are correct, but Code 2 is better.

4.15  Suppose income is 4001, what will be displayed by f the following code?

if income > 3000:
    print("Income is greater than 3000")
elif income > 4000:
    print("Income is greater than 4000")
A. none
B. Income is greater than 3000
C. Income is greater than 3000 followed by Income is greater than 4000
D. Income is greater than 4000
E. Income is greater than 4000 followed by Income is greater than 3000

4.16  Suppose you write the code to display "Cannot get a driver's license" if age is less than 16

and "Can get a driver's license" if age is greater than or equal to 16
Which of the following code is correct?

I: 
if age < 16:
    print("Cannot get a driver's license")
if age >= 16: 
    print("Can get a driver's license")

II:
if age < 16: 
    print("Cannot get a driver's license")
else:
    print("Can get a driver's license")

III:
if age < 16:
    print("Cannot get a driver's license")
elif age >= 16: 
    print("Can get a driver's license")
 
IV:
if age < 16:
    print("Cannot get a driver's license")
elif age == 16: 
    print("Can get a driver's license")
elif age > 16:
    print("Can get a driver's license")
A. I and II
B. II and III
C. I, II, and III
D. III and IV
E. All correct

4.17  Suppose you write the code to display "Cannot get a driver's license" if age is less than 16

and "Can get a driver's license" if age is greater than or equal to 16
Which of the following code is the best?

I: 
if age < 16: 
    print("Cannot get a driver's license")
if age >= 16:
    print("Can get a driver?s license")

II:
if age < 16: 
    print("Cannot get a driver's license")
else:
    print("Can get a driver's license")

III:
if age < 16: 
    print("Cannot get a driver's license")
elif age >= 16: 
    print("Can get a driver's license")

IV:
if age < 16: 
    print("Cannot get a driver's license")
elif age == 16:
    print("Can get a driver's license")
elif age > 16: 
    print("Can get a driver's license")
A. I
B. II
C. III
D. IV

4.18  The __________ function immediately terminates the program.
A. sys.terminate()
B. sys.halt()
C. sys.exit()
D. sys.stop()

Section 4.11 Logical Operators
4.19  Which of the Boolean expressions below is incorrect?
A. True and 3 => 4
B. !(x > 0) and (x > 0)
C. (x > 0) or (x < 0)
D. (x != 0) or (x = 0)
E. (-10 < x < 0)

4.20  Which of the following is the correct expression that evaluates to True if the number x is between 1 and 100 or the number is negative?

21. To check whether a char variable ch is an uppercase letter, you write ___________.
A. (ch >= 'A' and ch >= 'Z')
B. (ch >= 'A' and ch <= 'Z')
C. (ch >= 'A' or ch <= 'Z')
D. ('A' <= ch <= 'Z')

4.21  Given |x - 2| <= 4, Which of the following is true?
A. x - 2 <= 4 and x - 2 >= 4
B. x - 2 <= 4 and x - 2 > -4
C. x - 2 <= 4 and x - 2 >= -4
D. x - 2 <= 4 or x - 2 >= -4

4.22  Given |x - 2| >= 4, Which of the following is true?
A. x - 2 >= 4 and x - 2 <= -4
B. x - 2 >= 4 or x - 2 <= -4
C. x - 2 >= 4 and x - 2 < -4
D. x - 2 >= 4 or x - 2 <= -4

4.23  Assume x = 4 and y = 5, Which of the following is true?
A. x < 5 and y < 5
B. x < 5 or y < 5
C. x > 5 and y > 5
D. x > 5 or y > 5

4.24  Assume x = 4 and y = 5, Which of the following is true?
A. not (x == 4)
B. x != 4
C. x == 5
D. x != 5

4.25  Assume x = 4 and y = 5, Which of the following is true?
A. not (x == 4)
B. x != 4
C. x == 5
D. x != 5

4.26  Which of the following is equivalent to x != y?
A. not (x == y)
B. x > y and x < y
C. x > y or x < y
D. x >= y or| x <= y

4.27  What will be displayed by the following code?

ch = 'F'
if ch >= 'A' and ch <= 'Z':
    print(ch)
A. F
B. f
C. nothing
D. F f

Section 4.14 Conditional Expressions
4.28  What is y after the following statement is executed?

x = 0
y = 10 if x > 0 else -10
A. -10
B. 0
C. 10
D. 20
E. Illegal expression

4.29  Analyze the following code fragments that assign a boolean value to the variable even.

Code 1: 
if number % 2 == 0:
    even = True
else: 
    even = False

Code 2: 
even = True if number % 2 == 0 else False

Code 3:
even = number % 2 == 0

 
A. Code 2 has a syntax error, because you cannot have True and False literals in the conditional expression.
B. Code 3 has a syntax error, because you attempt to assign number to even.
C. All three are correct, but Code 1 is preferred.
D. All three are correct, but Code 2 is preferred.
E. All three are correct, but Code 3 is preferred.

4.30  What will be displayed by the following code?

isCorrect = False
print("Correct" if isCorrect else "Incorrect")
A. Correct
B. Incorrect
C. nothing
D. Correct Incorrect

Section 4.15 Operator Precedence and Associativity
4.31  The order of the precedence (from high to low) of the operators +, *, and, or is:
A. and, or, *, +
B. *, +, and, or
C. *, +, and, or
D. *, +, or, and
E. or, and, *, +

4.32  Which of the following operators are right-associative.
A. *
B. +
C. %
D. and
E. =

4.33  What is the value of the following expression?

True or True and False
A. True
B. False

4.34  Which of the following statements are True?
A. (x > 0 and x < 10) is same as (x > 0 and x < 10)
B. (x > 0 or x < 10) is same as (0 < x < 10)
C. (x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0))
D. (x > 0 or x < 10 and y < 0) is same as ((x > 0 or x < 10) and y < 0)