3260 q2
(a) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100. (b) Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100 or the number is negative.
(a) (x > 1) and (x < 100) (b) (num > 1) and (num < 100) or (num < 0)
a) Write a Boolean expression that evaluates to True if variable num is between 1 and 100. (b) Write a Boolean expression that evaluates to True if variable num is between 1 and 100 or the number is negative.
(a) (x > 1) and (x < 100) (b) (x > 1 and x < 100) or (x < 0)
(a) Write a Boolean expression for |x - 5| < 4.5. (b) Write a Boolean expression for |x - 5| > 4.5.
(b) (x - 5 > 4.5 and x - 5 <= -4.5)
How do you simplify the Boolean expression in lines 10-11 in LiveExample 3.7 using the != operator?
(number % 2 == 0) != (number % 3 == 0)
Write a Boolean expression that evaluates to true if weight is greater than 50 and height is greater than 160.
(weight > 50 or height > 160) and not (weight > 50 and height > 160)
If you enter 5, what is the output of the following code? i = int(input("Enter an integer: ")) if i < 4: x = 1 else: x = -1 print(x)
-1
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")
Income is greater than 3000
What is the value of the expression 50 <= x <= 100 if x is 45, 67, or 101?
It is False for x is 45. It is True for x is 67. It is False for x is 101
What is wrong in the following code? if score >= 60.0: print("Grade is D") elif score >= 70.0: print("Grade is C") elif score >= 80.0: print("Grade is B") elif score >= 90.0: print("Grade is A") else: print("Grade is F")
The conditions are tested in the wrong orders.
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 simply better using if even:.
What is wrong in the following code? income = 232323 if income <= 10000: tax = income * 0.1 elif income > 10000 and income <= 20000: tax = 1000 + (income - 10000) * 0.15 print(tax)
This program will have a runtime error, because tax will not be created.
Can the following conversions be allowed? If so, find the converted result. i = int(True) j = int(False) b1 = bool(4) b2 = bool(0)
Yes. i becomes 1, j becomes 0, b1 becomes True, and b2 becomes False
random.random() returns ____________.
a float number i such that 0 <= i < 1.0
Write a Boolean expression that evaluates to true if age is greater than 13 and less than 18.
age > 13 and age < 18
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
if isPrime:
Which of the following code displays the area of a circle if the radius is positive.
if radius > 0: print(radius * radius * 3.14159)
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
Write an if statement that increases pay by 3% if score is greater than 90.
if score > 90: pay *= 1.03
Write an if statement that assigns 1 to x if y is greater than 0.
if y > 0: x = 1
The following code displays ___________. temperature = 50 if temperature >= 100: print("too hot") elif temperature <= 40: print("too cold") else: print("just right")
just right
Rewrite the following statement using a Boolean expression: if count % 10 == 0: newLine = True else: newLine = False
newLine = (count % 10 == 0)
returns a random integer between a and b.
randint(a, b)
returns a random float number between 0.0 and 1.0 (excluding 1.0)
random()
Suppose the random module has been imported, write an expression that returns a random integer between 0 and 9 using the randint function, inclusive.
random.randint(0 , 9)
To generate a random integer between 0 and 5, use ________________.
random.randint(0, 5) random.randrange(0 , 6)
Suppose the random module has been imported, write an expression that returns a random value between 0.0 and 1.0, excluding 1.0.
random.random()
Suppose the random module has been imported, write an expression that returns a random integer between 0 and 9 using the randrange function.
random.randrange(0 , 10)
How do you generate a random integer 0 or 1?
random.randrange(0, 2) or random.randint(0, 1)
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)
returns a random integer between a and b - 1.
rangeint(a, b)
The __________ function immediately terminates the program.
sys.exit()
Analyze the following code. even = False if even: print("It is even!")
the code displays nothing
Write a Boolean expression that evaluates to true if weight is greater than 50 or height is greater than 160.
weight > 50 or height > 160
Assume x is an integer. Write a simple expression that returns true if x is even or false if x is odd.
x % 2 == 0
Assuming x = 4 and y = 5, show the result of the following Boolean expressions. x >= y >= 0 x <= y >= 0 x != y == 5 (x != 0) or (x == 0)
x >= y >= 0 False x <= y >= 0 True x != y == 5 True (x != 0) or (x == 0) True
Are the following expressions equivalent? a. (x >= 1) and (x < 10) b. (1 <= x < 10)
yes
Are the following two statements equivalent? (a) if income <= 10000: tax = income * 0.1 elif income >= 20000: tax = 1000 + \ (income - 10000) * 0.15 (b) if income >= 20000: tax = 1000 + \ (income - 10000) * 0.15 elif income <= 10000: tax = income * 0.1
yes
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.
What is the output of the following code? x = 3 if x >= 2: x += 1 print(x)
4
List six relational operators.
<, <=, ==, !=, >, >=
The "less than or equal to" comparison operator is __________.
<=
The equal comparison operator is __________.
==
The word True is ________.
A Python keyword A boolean literal
random.randint(0, 1) returns ____________.
0 or 1
What is the output of the following code? x = 0 if x < 4: x = x + 1 print("x is", x)
1
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: Code 1: if 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.
What is the output of the following code if number is 14, 15, and 30? (a) if number % 2 == 0: print(number, "is even") if number % 5 == 0: print(number, "is multiple of 5") (b) if number % 2 == 0: print(number, "is even") elif number % 5 == 0: print(number, "is multiple of 5")
For number is 14, (a) displays: 14 is even (b) displays 14 is even For number is 15, (a) displays: 15 is multiple of 5 (b) displays 15 is multiple of 5 For number is 30, (a) displays: 30 is even 30 is multiple of 5 (b) displays 30 is even
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")
II, because condition is tested only once
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