Python - Chapter 4
Analyze the following code: even = False if even = True: print("It is even!")
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:
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
B. x < 5 or y < 5
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
D. x != 5
What will be displayed by the following code? ch = 'F' if ch >= 'A' and ch <= 'Z': print(ch)
F
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")
Income is greater than 3000
What will be displayed by the following code? isCorrect = False print("Correct" if isCorrect else "Incorrect")
Incorrect
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
if isPrime:
The "less than or equal to" comparison operator is __________
<=
The equal comparison operator is __________
==
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)
A,B,D
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)
A. (x > 0 and x < 10) is same as (x > 0 and x < 10) C. (x > 0 or x < 10 and y < 0) is same as (x > 0 or (x < 10 and y < 0))
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
A. not (x == y) C. x > y or x < y
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")
All correct
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
All three are correct, but Code 3 is preferred.
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 ___________
B. (ch >= 'A' and ch <= 'Z') D. ('A' <= ch <= 'Z')
The __________ function immediately terminates the program
sys.exit()
Given |x - 2| <= 4, Which of the following is true?
x - 2 <= 4 and x - 2 >= -4
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")
Code II
Which of the following operators are right-associative A. * B. + C. % D. and E. =
E. =
Analyze the following code. even = False if even: print("It is even!")
The code displays nothing.
What is the value of the following expression? True or True and False
True
The word True is ________
a Python keyword a Boolean literal
random.random() returns ____________
a float number i such that 0 <= i < 1.0
Which of the following code displays the area of a circle if the radius is positive.
if radius > 0: print(radius * radius * 3.14159)
The following code displays ___________ temperature = 50 if temperature >= 100: print("too hot") elif temperature <= 40: print("too cold") else: print("just right")
just right
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")
nothing displayed
To generate a random integer between 0 and 5, use ________________
random.randint(0, 5) random.randrange(0, 6)
Given |x - 2| >= 4, Which of the following is true?
x - 2 >= 4 or x - 2 <= -4
What is the output of the following code? x = 0 if x < 4: x = x + 1 print("x is", x)
x is 1
The order of the precedence (from high to low) of the operators +, *, and, or is:
*, +, and, or
What is y after the following statement is executed? x = 0 y = 10 if x > 0 else -10
-10
random.randint(0, 1) returns ____________
0 or 1
Analyze the following code: Code 1: f number % 2 == 0: even = True else: even = False Code 2: even = number % 2 == 0
Both Code 1 and Code 2 are correct, but Code 2 is better.