Selections Flashcards
A Boolean expression is an expression that evaluates to a ______ value _____ or ______.
A Boolean expression is an expression that evaluates to a Boolean value True or False.
The word True is \_\_\_\_\_\_\_\_. A. a Python keyword B. a Boolean literal C. same as value 1 D. same as value 0
A. a Python keyword
B. a Boolean literal
What does the randint(a, b) function do?
The randint(a, b) function can be used to generate a random integer between a and b, inclusively.
How do you generate a random integer i such that 0 <= i < 20?
random.randrange(0, 20) or random.randint(0, 19)
How do you generate a random integer i such that 10 <= i < 20?
random.randrange(10, 20) or random.randint(10, 19)
How do you generate a random integer i such that 10 <= i <= 50?
random.randrange(10, 50 + 1) or random.randint(10, 50)
How do you generate a random integer 0 or 1?
random.randrange(0, 2) or random.randint(0, 1)
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)
A. random.randint(0, 5)
D. random.randrange(0, 6)
Hint: In A, randint(0, 5) returns a sequence from 0, 1, 2, 3, 4, 5. In B, randint(0, 6) returns a sequence from 0, 1, 2, 3, 4, 5, 6. In C, randrange(0, 5) returns a sequence from 0, 1, 2, 3, 4. In D, randrange(0, 6) returns a sequence from 0, 1, 2, 3, 4, 5. A and D are correct.
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
B. a float number i such that 0 <= i < 1.0
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:
B. if isPrime == True:
C. if isPrime:
Both correct, but C is better.
Write an if statement that increases pay by 3% if score is greater than 90, otherwise it increases pay by 1%.
if score > 90:
pay *= 1.03
else:
pay *= 1.01
What is the outpout of the code in (a) and (b) if number is 30 and 35?
(a)
if number % 2 == 0:
print(number, “is even.”)
print(number, "is odd.") (b) if number % 2 == 0: print(number, "is even.") else: print(number, "is odd.")
If number is 30,
(a) displays
30 is even
30 is odd
(b) displays
30 is even
If number is 35,
(a) displays
35 is odd
(b) displays
35 is odd
Are the following statements correct? Which one is better?
(a)
if age < 16:
print(“Cannot get a driver’s license”)
if age >= 16:
print(“Can get a driver’s license”)
(b) if age < 16: print("Cannot get a driver's license") else: print("Can get a driver's license")
Both are correct. (b) is better. Both conditions in (a) are tested. In (b) the condition is tested only once.
Rewrite the following statement using a Boolean expression: if count % 10 == 0: newLine = True else: newLine = False
newLine = (count % 10 == 0)
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 simply better using if even:.
C. The program runs, but displays nothing.
D. The program runs and displays It is even!.
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 simply better using if even:.
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:
B. The code displays nothing.
Hint: even is False. Nothing is displayed.
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.
D. Both Code 1 and Code 2 are correct, but Code 2 is better.
Suppose income is 4001, what will be displayed by 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
B. Income is greater than 3000
Hint: Since income is 4001, the condition (income > 3000) is true. So statement for the true case is executed. The elif clause is not executed in this case.