ch 3 quiz practice

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write a Boolean expression for |x - 5| < 4.5

(-4.5 <= x - 5 < 4.5)

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.

(num > 1) and (num < 100) or (num < 0)

what does print(bool((4)) output

True

the word true is: a python keyword a Boolean literal same as value 1 same as value 0

a python keyword a Boolean literal

Given the already defined variables is_full_time_student and age, write an expression that evaluates to True if age is less than 19 or is_full_time_student is True.

age < 19 or is_full_time_student == True

Write an if/else statement that compares sold_yesterday and sold_today, and based upon that comparison assigns sales_trend the value -1 (the case where sold_yesterday is greater than sold_today) or 1.

if sold_yesterday > sold_today: sales_trend = -1 else: sales_trend = 1

Write a selection statement that assigns True to fever if temperature is greater than 98.6.

if temperature > 98.6: fever = True

Write an if/else statement that assigns True to fever if temperature is greater than 98.6; otherwise it assigns False to fever

if temperature > 98.6: fever = True else: fever = False

Write a selection statement that multiplies the value associated with pay by one-and-a-half if worked_overtime is associated with True.

if worked_overtime == True: pay *= 1.5

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear, write a statement that assigns True to norecall if the value of modelYear is not within the recall range and assigns False otherwise. Do not use an if statement in this exercise.

norecall = modelYear < 2001 or modelYear > 2006

Given two variables, is_empty of type bool, indicating whether a class roster is empty or not, and number_of_credits of type int, containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is one or three credits.

(is_empty == False and (number_of_credits == 1 or number_of_credits == 3))

Write a Boolean expression that evaluates to true if either weight is greater than 50 or height is greater than 160, but not both.

(weight > 50 or height > 160) and not (weight > 50 and height > 160)

Write a Boolean expression for |x - 5| > 4.5

(x - 5 > 4.5 and x - 5 <= -4.5)

Write a Boolean expression that evaluates to True if variable num is between 1 and 100 or the number is negative.

(x > 1 and x < 100) or (x < 0)

Write a Boolean expression that evaluates to True if variable num is between 1 and 100

(x > 1) and (x < 100)

Write a Boolean expression that evaluates to true if a number stored in variable num is between 1 and 100.

(x > 1) and (x < 100)

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, *, +

*, +, and, or

Rewrite conditional expression that returns -1 or 1 randomly.

-1 if random.randint(0, 1) == 0 else 1

what does print(int(False)) output?

0

random.randint(0, 1) returns ____________.

0 or 1

what does print(int(True)) output?

1

What is the output of the following code? x = 3 if x >= 2: x += 1 print(x)

4

list 6 relational operators

< less than <= less than or equal to == equals != not equal > greater than >= greater than or equal to

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

=

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, C

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, D

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:

C if isPrime:

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)

C. if radius > 0: print(radius * radius * 3.14159)

Is (x > 0 and x < 10) the same as (x > 0 and x < 10)?

Yes. Since the relational operators have higher precedence than the and operator (see Table 3.7), (x > 0 and x < 10) is the same as (x > 0 and x < 10).

Is (x > 0 || x < 10) the same as (x > 0 || x < 10)?

Yes. Since the relational operators have higher precedence than the || operator (see Table 3.7),(x > 0 || x < 10) is the same as (x > 0 || x < 10).

Is (x > 0 || x < 10 and y < 0) the same as (x > 0 || (x < 10 and y < 0))?

Yes. Since the || operator has higher precedence than the and operator (see Table 3.7), (x > 0 || x < 10 and y < 0) is the same as (x > 0 || (x < 10 and y < 0)).

Write a Boolean expression that evaluates to true if age is greater than 13 and less than 18.

age > 13 and age < 18

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

Evaluate the following expressions: 2 * 2 - 3 > 2 and 4 - 2 > 5 2 * 2 - 3 > 2 or 4 - 2 > 5

false false

Write a multi-way selection statement that adds 1 to minors if age is less than 18, adds 1 to adults if age is 18 through 64 and adds 1 to seniors if age is 65 or older.

if age < 18: minors += 1 elif age <= 64: adults += 1 else: seniors += 1

Write an if/else statement that compares age with 65, adds 1 to senior_citizens if age is greater than or equal to 65, and adds 1 to non_seniors otherwise.

if age >= 65: senior_citizens += 1 else: non_seniors += 1

Write a selection statement that assigns 10,000 to bonus if the goods_sold is greater than 500,000.

if goods_sold > 500000: bonus = 10000

rewrite the conditional expression using if/else statements tax = income * 0.2 if income > 10000 else income * 0.17 + 1000

if income > 10000: tax = income * 0.2 else: tax = income * 0.17 + 1000

Assume that isIsosceles is a bool variable, and that the variables isoCount, triangleCount, and polygonCount have all been initialized. Write a statement that adds 1 to each of these count variables (isoCount, triangleCount, and polygonCount) if isIsosceles is True.

if isIsosceles == True: isoCount += 1 triangleCount += 1 polygonCount += 1

rewrite the conditional expression using if/else statements print(i if number % 3 == 0 else j)

if number % 3 == 0: print(i) else: print(j)

Write a selection statement that decreases the value associated with shelf_life by 4 if the value associated with outside_temperature is greater than 90.

if outside_temperature > 90: shelf_life -= 4

Given that ph refers to a float, write a multi-way selection statement that compares ph to 7.0 and makes the following assignments (respectively) to the variables neutral, base, and acid: 0,0,1 if ph is less than 7 0,1,0 if ph is greater than 7 1,0,0 if ph is equal to 7

if ph < 7: neutral = 0 base = 0 acid = 1 elif ph > 7: neutral = 0 base = 1 acid = 0 else: neutral = 1 base = 0 acid = 0

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 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

rewrite the conditional expression using if/else statements score = 3 * scale if x > 10 else 4 * scale

if x > 10: score = 3 * scale else: score = 4 * scale

Given x and y, each associated with an int, write a fragment of code that associates the larger of these with another variable named max.

if x > y: max = x else: max = y

Write an if statement that assigns 1 to x if y is greater than 0.

if y > 0: x = 1

What will be displayed by the following code? isCorrect = False print("Correct" if isCorrect else "Incorrect")

incorrect

Given two variables, is_empty which is associated with a bool, indicating whether a class roster is empty or not, and number_of_credits which is associated with an int, containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is more than two credits.

is_empty == False and number_of_credits > 2

Given two variables, is_empty which is associated with a bool indicating whether a class roster is empty or not, and number_of_credits which is associated with an int (the number of credits for a class), write an expression that evaluates to True if the class roster is empty or the class is exactly three credits.

is_empty == True or number_of_credits == 3

Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min.

min = x if y < min: min = y if z < min: min = z

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear, write a statement that assigns True to norecall if the value of modelYear does not fall within the two recall ranges and assigns False otherwise. Do not use an if statement in this exercise.

norecall = not (1995 <= modelYear and modelYear <= 1998 or 2004 <= modelYear and modelYear <= 2006)

Write an expression that evaluates to True if and only if is_a_member is False.

not is_a_member

Write an expression that evaluates to True if the int associated with number_of_prizes is divisible (with no remainder) by the int associated with number_of_participants. Assume that the latter is not zero.

number_of_prizes % number_of_participants == 0

Write a statement that toggles on_off_switch. That is, if on_off_switch is False, it is made equal to True; if on_off_switch is True, it is made equal to False.

on_off_switch = not(on_off_switch)

Rewrite the following if statements using a conditional expression if count % 10 == 0: print(count, end = "*") else: print(count, end = "#")

print(count, end = "*" if count % 10 == 0 else "#")

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)

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear, write a statement that assigns True to recalled if the value of modelYear falls within the two recall ranges and assigns False otherwise. Do not use an if statement in this exercise.

recalled = 1995 <= modelYear and modelYear <= 1998 or 2004 <= modelYear and modelYear <= 2006

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear, write a statement that assigns True to recalled if the value of modelYear falls within the recall range and assigns False otherwise. Do not use an if statement in this exercise.

recalled = 2001 <= modelYear and modelYear <= 2006

Given the already defined variables temperature and humidity, write an expression that evaluates to True if and only if temperature is greater than 90 and humidity is less than 10.

temperature > 90 and humidity < 10

Rewrite the following if statements using a conditional expression if ages >= 16: ticketPrice = 20 else: ticketPrice = 10

ticketPrice = 20 if (ages >= 16) else 10

True or false? All the binary operators except = are left-associative.

true

What is the value of the following expression? True or True and False

true

Write a Boolean expression that evaluates to true if weight is greater than 50 and height is greater than 160

weight > 50 and height > 160

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 refers to an int. Write a Boolean expression that is True if the variable x refers an even number.

x % 2 == 0

Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative and y is negative.

x >= 0 and y < 0

Given that the variables x and y have been defined, write an expression that evaluates to True if x is non-negative or y is negative.

x >= 0 or y < 0

What is the output of the following code? x = 0 if x < 4: x = x + 1 print("x is", x)

x is 1

Given the already defined variables years_with_company and department, write an expression that evaluates to True if years_with_company is less than 5 and department is not equal to 99.

years_with_company < 5 and department != 99

Are the following expressions equivalent? a. (x >= 1) and (x < 10) b. (1 <= x < 10)

yes

what does print(bool(0)) output

False


Ensembles d'études connexes

Intro to Criminal Justice- Final Exam

View Set

Mkt 230 Chapters 7,8,11,12,13,14

View Set

EXPH 2045 Exam 1 - Homeostasis/Skeletal

View Set

Intro To Computing - Python 3 Pt 2

View Set

Exercise 7: chemically defined, complex, selective, and differential media

View Set