Chapter 5 python
The following loop displays _______________. for i in range(1, 11): print(i, end = " ")
1 2 3 4 5 6 7 8 9 10
How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") count += 1
10
How many times is the print statement executed? for i in range(10): for j in range(10): print(i * j)
100
What is sum after the following loop terminates? sum = 0 item = 0 while item < 5: item += 1 sum += item if sum >= 4: continue print(sum)
15
What is the output for y? y = 0 for i in range(0, 10, 2): y += i
20
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
30
What is the output of the following code? x = 0 while x < 4: x = x + 1 print("x is", x)
4
How many times is the print statement executed? for i in range(10): for j in range(i): print(i * j)
45
What is the output for y? y = 0 for i in range(0, 10): y += i
45 Explanation: y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
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
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
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
Will the following program terminate? balance = 10 while True: if balance < 9: continue balance = balance - 9
No
Analyze the following statement: sum = 0 for d in range(0, 10, 0.1): sum += sum + d
The program has a syntax error because the arguments in the range must be integers.
Analyze the following fragment: sum = d = 0 while d != 10.0: d += 0.1 sum += sum + d
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?
add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
c
c
Analyze the following code. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C
count < 100 is always True at Point A count < 100 is always False at Point C
i is 2 followed by 9 is prime
i is 2 followed by 9 is prime
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)
i is 5 isPrime is False
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)
i is 6 isPrime is False
How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("welcome to python")
infinite number of times
What is the number of iterations in the following loop: for i in range(1, n + 1): # iteration
n
What is the number of iterations in the following loop: for i in range(1, n): # iteration
n - 1
Which of the following function is incorrect?
range(0, 3.5) range(2.5, 4.5) range(1, 2.5, 4.5)
Which of the following function returns a sequence 0, 1, 2, 3?
range(0, 4) range(4)
ill the following program terminate? balance = 10 while True: if balance < 9: break balance = balance - 9
yes
The function range(5) return a sequence ______________.
0, 1, 2, 3, 4