ch 5 questions: loops

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

what's the output; i = 0 while i < 5: for j in range(i, 1, -1): print(j, end = " ") print("****") i += 1

**** **** 2 **** 3 2 **** 4 3 2 ****

Can you convert any for loop to a while loop? List the advantages of using for loops.

*Yes.* The advantages of for loops are simplicity and readability. Compilers can produce more efficient code for the for loop than for the corresponding while loop.

what's the output: for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1

0 0 1 0 1 2 0 1 2 3

What is the output of the following code? for i in range(1, 10, 2): print(i, end = '')

13579

What is the output of the following code? x = 85 for i in range(2, x): if x % i == 0: print(i, end = '') break

5

What is the output of the following code? x = 85 for i in range(2, x): if x % i == 0: print(i, end = ' ') continue

5 17

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

What is the output for y? y = 0 for i in range(10, 1, -2): y += i print(y) A. 10 B. 40 C. 30 D. 20

C Hint: i is 10, 8, 6, 4, 2, 0

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

C. 3 0

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

What is the output for y? y = 0 for i in range(0, 10, 2): y += i print(y) A. 9 B. 10 C. 11 D. 20

D

What is the output for y? y = 0 for i in range(0, 10): y += i print(y) A. 10 B. 11 C. 12 D. 13 E. 45

E

How many times are the following loop bodies repeated? What's the print out? i = 1 while i < 10: if i % 2 == 0: print(i) i += 1

executed 9 times; printout: 2, 4, 6, 8 on separate lines

whats the error in the code: count = 0 while count < 100: count += 1

indentation error

How many times are the following loop bodies repeated? i = 1 while i < 10: if i % 2 == 0: print(i) i += 1

infinite

How many times are the following loop bodies repeated? i = 1 while i < 10: if i % 2 == 0: print(i)

infinite

Convert the following while loop into a for loop. i = 1 sum = 0 while sum < 10000: sum = sum + i i += 1

sum = 0 for i in range(1, 10000): if sum < 10000: sum = sum + i

Convert the following for loop statement to a while loop: sum = 0 for i in range(1001): sum = sum + i

sum = 0 i= 0 while i <= 1000: sum += i i += 1

Suppose the input is 2 3 4 5 0 (one number per line). What is the output of the following code? number = 0 sum = 0 for count in range(5): number = int(input("Enter an integer: ")) sum += number print("sum is", sum) print("count is", count)

sum is 14 count is 4

Count the number of iterations in the following loops. count = 5 while count < n: count = count + 3

the ceiling of (n-5)/3 times

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

x is 4

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

10 times

what's the output: i = 1 while i <= 5: num = 1 for j in range(1, i + 1): print(num, end = "G") num += 2 print() i += 1

1G 1G3G 1G3G5G 1G3G5G7G 1G3G5G7G9G

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

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

A E

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)

ADE CANNOT BE FLOATS IN RANGE

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.

B

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

B

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)

BD

whats the error in the code: count = 0 while count < 100: print(count) count -= 1

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)

Suppose the input is 2 3 4 5 0 (one number per line). What is the output of the following code? number = int(input("Enter an integer: ")) max = number while number != 0: number = int(input("Enter an integer: ")) if number > max: max = number print("max is", max) print("number", number)

max is 5 number is 0

Count the number of iterations in the following loops. count = 0 while count < n: count += 1

n times

Count the number of iterations in the following loops. for count in range(n): print(count)

n times

Count the number of iterations in the following loops. count = 5 while count < n: count += 1

n-5 times

whats the output: i = 5 while i >= 1: num = 1 for j in range(1, i + 1): print(num, end = "xxx") num *= 2 print() i -= 1

1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx

How many times is the following loop executed? count = 1 *while* count < 5: count += 3

2

What is the output of the following code? count = 1 *while* count < 4: count += 1 print(count, end = ' ')

2 3 4

How many times is the following loop executed? for count in range(1, 5): # Execute the body

4

What is count after the loop is finished? for count in range(1, 5, 3): # Execute the body

4

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

BC

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) A. BCD B. ABCD C. B D. CDE E. CD

C

what's the error in the code: count = 0 while count < 100: print(count)

infinite loop


Set pelajaran terkait

NE 106-Spring 2021- Test 3-Practice Questions-Newborn

View Set

Chapter 38: The Child with a Gastrointestinal/Endocrine Disorder

View Set

Software Language Engineering (CS3480)

View Set