Programming Quiz 4, Revel MC Questions (Ch 5)

¡Supera tus tareas y exámenes ahora con Quizwiz!

How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") count += 1 A. 8 B. 9 C. 10 D. 11 E. 0

10

What is the output for y? y = 0 for i in range(0, 10, 2): y += i print(y)

20 (i is 0, 2, 4, 6, 8)

What is the output for y? y = 0 for i in range(0, 10): y += i print(y)

45 (y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 4)

What is sum after the following loop terminates? sum = 0 item = 0 while item < 5: item += 1 sum += item if sum > 4: break print(sum)

6 (Sum and item are initialized to 0 before the loop. In the loop, item++ increments item by 1 and item is then added to sum. If sum > 4, the break statement exits the loop. item is initially 0, then 1, 2, 3 and sum is initially 0, and then 1, 3, and 6. When sum is 6, (sum > 4) is true, which causes the break statement to be executed)

How many times is the print statement executed? for i in range(10): for j in range(10): print(i * j) A. 100 B. 20 C. 10 D. 45

A. 100 (The outer loop is executed 10 times for i from 0 to 9 and the inner loop is executed 10 times from j from 0 to 9. So the print statement is executed 100 times. The correct answer is A.)

Analyze the following fragment: sum = d = 0 while d != 10.0: d += 0.1 sum += sum + d A. The program does not run because sum and d are not initialized correctly. B. The program never stops because d is always 0.1 inside the loop. C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9

C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

The function range(5) returns a sequence ______________. A. 1, 2, 3, 4, 5 B. 0, 1, 2, 3, 4, 5 C. 1, 2, 3, 4 D. 0, 1, 2, 3, 4

D. 0, 1, 2, 3, 4

How many times is the print statement executed? for i in range(10): for j in range(i): print(i * j) A. 100 B. 20 C. 10 D. 45

D. 45 (The outer loop is executed 10 times for i from 0 to 9. For each i, the inner loop is executed i times. So, the print statement is executed 0 + 1 + 2 + ... + 9 = 45 times.)

What will be displayed by after the following loop terminates? number = 25 isPrime = True i = 2 while i < number and isPrime: if number % i == 0: isPrime = False i += 1 print("i is", i, "isPrime is", isPrime) A. i is 5 isPrime is True B. i is 5 isPrime is False C. i is 6 isPrime is True D. i is 6 isPrime is False

D. i is 6 isPrime is False (When i is 5, isPrime is assigned False, i += 1 is executed. So, i becomes 6. Now the loop ends since isPrime is False.)

Will the following program terminate? balance = 10 while True: if balance < 9: continue balance = balance - 9

No (Before the loop, balance is 10. In the loop, balance becomes 1. The loop-continuation-condition is always true. When balance < 9 is true, the continue statement is executed to skip the rest statement in the iteration. So the loop continues in an infinite loop.)

How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python")

infinite number of times (count is not changed. It is an infinite loop)

What is the output of the following code? x = 0 while 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

x is 4

The following loop displays _______________. for i in range(1, 11):print(i, end = " ") A. 1 2 3 4 5 6 7 8 9 B. 1 2 3 4 5 6 7 8 9 10 C. 1 2 3 4 5 D. 1 3 5 7 9 E. 2 4 6 8 10

1 2 3 4 5 6 7 8 9 10

What will be displayed when the following code is executed? number = 6 while number > 0: number -= 3 print(number, end = ' ')

3 0

What is the output for y? y = 0 for i in range(10, 1, -2): y += i print(y)

30 (i is 10, 8, 6, 4, 2, 0)

What is sum after the following loop terminates? sum = 0 item = 0 while item < 5: item += 1 if sum >= 4: continue sum += item print(sum)

6 (When sum >= 4, item is not added to sum)

Analyze the following code. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C A. count < 100 is always True at Point A B. count < 100 is always True at Point B C. count < 100 is always False at Point B D. count < 100 is always True at Point C E. count < 100 is always False at Point C

A and E, count < 100 is always True at Point A and count < 100 is always False at Point C (The count is initialized to 0 before the loop. The loop is executed 100 times for count from 0 to 99. Inside the loop body, at Point A, count < 100 is true. After executing count++, count < 100 in Point B may become false. After the loop is exited, count < 100 is false at Point C. So, the correct answer is AE.)

To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. B. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.

A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. (Adding the numbers in the order specified in A will result in a more accurate result than in B, because two numbers to be added will be about the same size.)

Which of the following function is incorrect? A. range(0, 3.5) B. range(10, 4, -1) C. range(1, 3, 1) D. range(2.5, 4.5) E. range(1, 2.5, 4.5)

A. range(0, 3.5), D. range(2.5, 4.5), and E. range(1, 2.5, 4.5) (The arguments in the range function must be integers.)

Which of the following loops prints "Welcome to Python" 10 times? A: for count in range(1, 10): print("Welcome to Python") B: for count in range(0, 10): print("Welcome to Python") C: for count in range(1, 11): print("Welcome to Python") D: for count in range(1, 12): print("Welcome to Python")

B and C (In (A), range(1, 10) has 9 numbers from 1 to 9. In (B), range(0, 10) has 10 numbers from 0 to 9. In (C), range(1, 11) has 10 numbers from 1 to 10. In (D), range(1, 12) has 11 numbers from 1 to 11. The loop will execute 10 times in B and C. Therefore, the correct answer is BC.)

What will be displayed by after the following loop terminates? number = 25 isPrime = True for i in range(2, number): if number % i == 0: isPrime = False break print("i is", i, "isPrime is", isPrime) A. i is 5 isPrime is True B. i is 5 isPrime is False C. i is 6 isPrime is True D. i is 6 isPrime is False

B. i is 5 isPrime is False' (When i is 5, isPrime is assigned False and the break statement is executed to break out of the loop. So, i is 5 afer the loop ends.)

What is the number of iterations in the following loop: for i in range(1, n): # iteration A. 2 * n B. n C. n - 1 D. n + 1

B. n Since range(1, n + 1) returns a sequence of 1, 2, ..., n. The number of iterations is n.

Which of the following function returns a sequence 0, 1, 2, 3? A. range(0, 3) B. range(0, 4) C. range(3) D. range(4)

B. range(0, 4) and D. range(4)(The range(a, b) function returns a sequence from a to b-1 and the function range(b) returns an integer sequence from 0 to b-1. Therefore, both B and D are correct.)

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: sum = 0 for i in range(1, 99): sum += i / (i + 1) print("Sum is", sum) B: sum = 0 for i in range(1, 100): sum += i / (i + 1) print("Sum is", sum) C: sum = 0 for i in range(1.0, 99.0): sum += i / (i + 1) print("Sum is", sum) D: sum = 0 for i in range(1.0, 100.0): sum += i / (i + 1) print("Sum is", sum)

B: sum = 0 for i in range(1, 100): sum += i / (i + 1) print("Sum is", sum) (In (A), range(1, 99) is a sequence from 1 to 98. In (C and D), the arguments in the range function are non-integers. The correct answer is B.)

Suppose the input for number is 9. What will be displayed by the following program? number = int(input("Enter an integer: ")) isPrime = True for i in range(2, number): if number % i == 0: isPrime = False print("i is", i) if isPrime: print(number, "is prime") break else: print(number, "is not prime") A. i is 3 followed by 9 is prime B. i is 3 followed by 9 is not prime C. i is 2 followed by 9 is prime D. i is 2 followed by 9 is not prime

C. i is 2 followed by 9 is prime When i is 2, number % 2 is 1, isPrime is True.

Given the following four patterns, Pattern A Pattern B Pattern C Pattern D 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5 1 2 3 1 2 3 4 3 2 1 1 2 3 4 1 2 3 4 1 2 3 4 3 2 1 1 2 3 1 2 3 4 5 1 2 5 4 3 2 1 1 2 1 2 3 4 5 6 1 6 5 4 3 2 1 1 Which of the pattern is produced by the following code? for i in range(1, 6 + 1): for j in range(6, 0, -1): print(j if j <= i else " ", end = " ") print()

Pattern C (The outer loop displays a line. The inner loop displays all the numbers in the line. The outer loop is repeated 6 times for i from 1 to 6. For each i, the inner loops displays i as the first number in the line since j is from 6 to 1. j is printed for j <= i. Otherwise, a space is printed. Thus, this code will print Pattern C.)

Analyze the following statement: sum = 0 for d in range(0, 10, 0.1): sum += sum + d A. The program has a syntax error because the range function cannot have three arguments. B. The program has a syntax error because the arguments in the range must be integers. C. The program runs in an infinite loop. D. The program runs fine.

The program has a syntax error because the arguments in the range must be integers.

Will the following program terminate? balance = 10 while True: if balance < 9: break balance = balance - 9 A. Yes B. No

Yes (Yes. Before the loop, balance is 10. The loop-continuation-condition is always true. In the first iteration, balance is reduced to 1. In the second iteration, the break statement is executed since (balance < 9) is now true.)


Conjuntos de estudio relacionados

La Ciudad de las Bestias - Capítulos 1-5

View Set

CHAPTER 3 - THE OPERATIONS MANAGEMENT FUNCTION

View Set

RDBMS Relational Database Management System

View Set

Chapter 14: Water Table and Aquifers

View Set

[EDMENTUM]: Principles of Design: Mastery Test

View Set

Chapters 15 & 16, Civil Liberties

View Set

International Investments Midterm--

View Set

PN Adult Medical Surgical Online Practice 2023 B

View Set

Inclined Planes, Wedges, and Screws

View Set