CS115: Chapter 3List six relational operators.

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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? A. 1 < x < 100 and x < 0 B. ((x < 100) and (x > 1)) or (x < 0) C. ((x < 100) and (x > 1)) and (x < 0) D. (1 > x > 100) or (x < 0)

ABCD

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)

ABD

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.

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

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

Consider score is 90, what will be the grade? The conditions are tested in the wrong orders.

The equal comparison operator is __________.

==

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

A and B

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

>= 1 = 0

True False

Assume age = 16, what is ticketPrice after ticketPrice = 20 if age >= 16 else 10?

20

How many days in the February of a leap year? Which of the following is a leap year? 500, 1000, 2000, 2016, and 2020?

2000, 2016, 2020

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

<=

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

=

Rewrite the following conditional expressions using if/else statements. a. score = 3 * scale if x > 10 else 4 * scale b. tax = income * 0.2 if income > 10000 else income * 0.17 + 1000 c. print(i if number % 3 == 0 else j)

A: if x > 10: score = 3 * scale else: score = 4 * scale B: if income > 10000: tax = income * 0.2 else: tax = income * 0.17 + 1000 C: if number % 3 == 0: print(i) else: print(j)

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

Rewrite the following statement using a Boolean expression: if count % 10 == 0: newLine = True else: newLine = False

newLine = (count % 10 == 0)

The __________ function immediately terminates the program.

sys.exit()

Analyze the following code. even = False if even: print("It is even!")

the code displays nothing

How do hide the cursor in Turtle?

turtle_hideturle()

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

x is 1

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

D

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

Incorrect. Since isCorrect is False, Incorrect is displayed.

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.

Suppose that when you run the following program you enter the input 2, 3, 6 from the console. What is the output? x = float(input("Enter a number: ")) y = float(input("Enter a number: ")) z = float(input("Enter a number: ")) print("sorted" if x < y and y < z else "not sorted")

Sorted

List the precedence order of the Boolean operators. Evaluate the following expressions: True or True and False True and True or False

The precedence order of the Boolean operators are not, and, or in this order. True or True and False is True True and True or False is True

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.

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

What is True and True? What is True and False? What is True or False? What is False or False? What is not True? What is not False?

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

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

Yes Yes Yes

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

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:

Drag the boxes up or down to form a program that displays HiveFive if the number is a multiple of 5, otherwise if the number is 7, displays LuckySeven, else displays the number.

if number % 5 == 0: print("HiFive") elif number == 7: print("LuckySeven") else: print(number)

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.

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

Drag the boxes up or down to form a correct program to display a grade based on the score.

if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") elif score >= 60: print("D") else: print("F")

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

if y > 0: x = 1

What happens if you enter an integer as 05?

it's the same as entering 5

The following code displays ___________. temperature = 50 if temperature >= 100: print("too hot") elif temperature <= 40: print("too cold") else: print("just right")

just right

How do you draw a red dot with diameter 3?

turtle.dot(3, "red")

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

Suppose, when you run the following program, you enter the input 2, 3, 6 from the console. What is the output? X = float(input("Enter a number: ")) y = float(input("Enter a number: ")) z = float(input("Enter a number: ")) print("(x < y and y < z) is", x < y and y < z) print("(x < y or y < z) is", x < y or y < z) print("not (x < y) is", not (x < y)) print("(x < y < z) is", x < y < z) print("not (x < y < z) is", not (x < y < z))

(x < y and y < z) is True (x < y or y < z) is True not (x < y) is False (x < y < z) is True not (x < y < z) is False

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

False. False

What is tax if income is 1000 and 20000, respectively? a. tax = income * 0.2 if income > 10000 else 0.1 b. tax = income * (0.2 if income > 10000 else 0.1)

For income 1000, a. 0.1 and b. 100.0 For income 20000, a. 4000.0 and b. 4000.0

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

(a) Write a Boolean expression for |x - 5| < 4.5. (b) Write a Boolean expression for |x - 5| > 4.5.

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

(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) && (x < 100) (b) (num > 1) && (num < 100) || 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)

Rewrite the following if statements using a conditional expression. (a) if ages >= 16: ticketPrice = 20 else: ticketPrice = 10 (b) if count % 10 == 0: print(count) else: print(count, end = " ")

(a)ticketPrice = 20 if (ages >= 16) else 10 (b) print(count, end = " " if count % 10 == 0 else " ")

Assuming that x is 1, show the result of the following Boolean expressions. True and (3 > 4) not (x > 0) and (x > 0) (x > 0) or (x < 0) (x != 0) or (x == 0) (x >= 0) or (x < 0) (x != 1) == not (x == 1)

(true) and (3 > 4) False not(x > 0) and (x > 0) False (x > 0) or (x < 0) True (x != 0) or (x == 0) True (x >= 0) or (x < 0) True (x != 1) == not (x == 1) True

The order of the precedence (from high to low) of the operators +, *, and, or is:

*, +, and, or

High to low

+, − (Unary plus and minus) ** (Exponentiation) not *, /, //, % (Multiplication, division, integer division, and remainder) +, − (Binary addition and subtraction) <, <=, >, >= (Relational) ==, != (Equality) and or =, +=, -=, *=, /=, //=, %= (Assignment operators)

Rewrite conditional expression that returns -1 or 1 randomly.

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

What is y after the following statement is executed? x = 0 y = 10 if x > 0 else -10

-10

Assume age = 15, what is ticketPrice after ticketPrice = 20 if age >= 16 else 10?

10

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

BD

Boolean expression: is an expression that evaluates to a Boolean value. Boolean variable: is a variable that holds a Boolean value. Boolean value: are two literals: True and False. Relational operator: =.

Boolean expression: is an expression that evaluates to a Boolean value. Boolean variable: is a variable that holds a Boolean value. Boolean value: are two literals: True and False. Relational operator: =.

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


Kaugnay na mga set ng pag-aaral

TX Life and Health: TX Statutes and Rules Pertinent to Life Insurance Only

View Set

Fundamentals Ch 1 Introduction to Nursing PrepU

View Set

Intro to fine arts- test 2(chap 14-16)

View Set

GI Practice questions from saunders / nurse sara

View Set

Improving Sentence Structure, Part 2

View Set

Chapter 4: Implementing Firewall Technologies

View Set

Ch. 13 - Medical Records Management

View Set

Chapter 13: Palliative and End-of-Life Care

View Set

Lecture 12 Sagittal, Coronal, and Axial planes

View Set