What is the output of the following?
(i) a=9;
a=a+5;
if a>5:
print(a+3)
else:
print(a-1)
(ii) for i in range(16,50,10):
a=i/5;
print(a)
print(i)
(iii) a,b,c=0,1,0
n=int(input("Enter a number"))
print(a,",",b,end=",")
for x in range(3,n+1):
C=a+b
print(c,end=",")
a,b=b,c
Answers
Answer:
5.1 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
5.2 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
5.3 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
5.4 How many times will the following code print "Welcome to Python"?
count = 0
while count < 10:
print("Welcome to Python")
A. 8
B. 9
C. 10
D. 11
E. infinite number of times
5.5 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
Section 5.3 The for Loop
5.6 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.
5.7 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")
A. BD
B. ABC
C. AC
D. BC
E. AB
5.8 The function range(5) return 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
5.9 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)
5.10 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)
5.11 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
5.12 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
5.13 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
5.14 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
5.15 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