Python Chapter 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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.

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

newLine = (count % 10 == 0)

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

Suppose x = 3 and y = 2; show the output, if any, of the following code. What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2? Draw a flowchart for the code. if x > 2: if y > 2: z = x + y print("z is", z) else: print("x is", x)

. The output is "x is 3" if x = 3 and y = 2. The output is "z is 7" if if x = 3 and y = 4. No output if if x = 2 and y = 2.

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

4

List six relational operators.

<, <=, ==, !=, >, >=

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 Hint: True is a Boolean literal just like integer literal 10. True is also a keyword

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)

The "less than or equal to" comparison operator is __________. A. < B. <= C. =< D. << E. !=

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.

The equal comparison operator is __________. A. <> B. != C. == D. =

C. ==

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:

Suppose x = 2 and y = 4. Show the output, if any, of the following code. What is the output if x = 3 and y = 2? What is the output if x = 3 and y = 3? Draw a flowchart for the code. if x > 2: if y > 2: z = x + y print("z is", z) else: print("x is", x)

Note: else matches the first if clause. The output is "x is 2" if x = 2 and y = 4. No output if if x = 3 and y = 2. The output is "z is 6" if if x = 3 and y = 3.

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

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.

Boolean value

are two literals: True and False.

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

Boolean expression

is an expression that evaluates to a Boolean value.

Relational operator

is one of the six operators: <, <=, ==, !=, >, >=.

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)

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)

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)

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.randrange(0.0,1.0)

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)

random()

returns a random float number between 0.0 and 1.0 (excluding 1.0)

rangeint(a, b)

returns a random integer between a and b - 1.

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

if y > 0: x = 1

Boolean variable

is a variable that holds a Boolean value.

randint(a, b)

returns a random integer between a and b.

random.randint(0, 1) returns ____________. A. 0 B. 1 C. 0 or 1 D. 2

C. 0 or 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 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") A. I B. II C. III D. IV

B. II Hint: II is best because the condition is tested only once.

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

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: if even = True: is not a correct syntax. Hint: even is False. Nothing is displayed.

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

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

What is the output of the following code? x = 0 if x < 4: x = x + 1 print("x is", x) A. x is 0 B. x is 1 C. x is 2 D. x is 3 E. x is 4

B. x is 1 Hint: Since x is 0, x < 4 is True. x + 1 is assigned to x. x now becomes 1.

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) Hint: In A, :Since radius is positive, radius != 0 is False. In B, Since radius is positive, radius == 0 is False. In C, Since radius is positive, radius > 0 is True. In D, Since radius is positive, radius <= 0 is False. C is correct.

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

C. just right

The __________ function immediately terminates the program. A. sys.terminate() B. sys.halt() C. sys.exit() D. sys.stop()

C. sys.exit()

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.

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) A. 4 B. 1 C. a syntax error is raised because x is not assigned. D. -1

D. -1

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.

What is the output 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

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") A. I and II B. II and III C. I, II, and III D. III and IV E. All correct

E. All correct Hint: All the code are correct. From this question, you can see that there are many different ways to write a selection statement, but the code in II is the best.

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


संबंधित स्टडी सेट्स

Appointment Scheduling Chapter 9

View Set

Classroom and Grading Expectations

View Set

6.3.6 Practice Questions Networking Media

View Set

Health Promotion and Maintenance Practice Exam

View Set

Final Exam ISTM 310 (Chapter 1 - Chapter 12)

View Set

Kei te pēhea? - Asking how someone is feeling

View Set

Insurance Terms and Related Concepts

View Set

NURS 3325 Leading and Managing in Nursing

View Set

Noahs Financial Accounting vocab Ch 1-3

View Set