Python questions QUESTION 1 How many iterations does the fol
Python questions
QUESTION 1
How many iterations does the following loop execute? (n initially is a strictly positive integer number.)
while n > 0:
n = n // 10
Impossible to tell.
No (zero)
This is an infinite loop.
Equal to the number of decimal digits in n
QUESTION 2
What does the following code fragment display? Assume N is a natural (positive integer) number.
s = 0
i = 1
while i < N:
s += i
i += 1
print(s)
The sum of the first N-1 natural numbers.
The product of the first N-1 natural numbers.
The value of N.
The sum of the first N natural numbers.
QUESTION 3
What does the following code fragment display? Assume N is a natural (positive integer) number.
s = 1
i = 0
while i < N:
s *= 10
i += 1
print(s)
N! (N factorial)
10N
10! (10 factorial)
QUESTION 4
What applications benefit from having an infinite loop? (Check all that applies.)
A program that reads all lines from a text file and displays them in the reverse order.
A Web server.
A program that
A Web browser
QUESTION 5
Which statements immediately terminate a loop within a function? (Check all that applies.)
pass
break
return
quit
QUESTION 6
Which value can be used as a sentinel to terminate a loop in which users are asked to enter floating point numbers?
Any integer number.
Any negative number.
\"quit\"
0
QUESTION 7
What is wrong with the following loop?
while busy:
busy = check_if_busy()
The condition must be written as while busy==True:.
Functions cannot be used within a \"while\" loop.
The value of the
ariable
QUESTION 8
Experiment with the following function and find out what it calculates:
def foobar(x):
r, r1 = x / 2, x
while abs(r - r1) > 1e-10:
r, r1 = r1, (r + x / r) / 2
return r
The square root of x.
The absolute value of
The sine of x.
Nothing that I can think of.
QUESTION 9
The following code fragment was supposed to count all occurrences of square brackets in the string text. Why does not it work?
count = 0
i = 0
while i < len(text):
if text[i] == \"[\" or \"]\": count += 1
i += 1
print(count)
The body of the
The variable i must be initialized with
The closing brackets
The value of the variable i eventually becomes greater than the largest index in the string, and the selection operator [] crashes the program.
| Impossible to tell. | ||
| No (zero) | ||
| This is an infinite loop. | ||
| Equal to the number of decimal digits in n |
Solution
5.Which statements immediately terminate a loop within a function? (Check all that applies.)
Answer :-
break
quit
Explanation :-
* Break statement immediately terminate a loop within a function.
* quit statement immediately stops execution flow of a loop within a function.
7.What is wrong with the following loop?
while busy:
busy = check_if_busy()
Answer :-
* The condition must be written as while busy==True:.


