Chapter - 5 Quiz Programming Joseph-Ira 12.01.2024
Chapter - 5 Quiz Programming Joseph-Ira 12.01.2024
1) For the given pseudocode, which XXX and YYY will output the sum of the input
integers (stopping when -1 is input)? Choices are in the form XXX / YYY.
a. 3
b. 4
c. 5
d. 7
a. 1
b. 3
c. 4
d. 5
4) What should XXX and YYY be so that the final output shows how many negative
values are input?
n = 0
val = Get next input
While val is not 0
If XXX
YYY
val = Get next input
put n to output
a. -1
b. 0
c. 1
d. No such value
c = 'y'
while c == 'y':
# Do something
print('Enter y to continue, n to quit: ', end=' ')
c = input()
print('Done');
a. 'y' only
b. 'n' only
count = 0
while count < 3:
print('loop')
count = count + 1
print(f'Final value of count: {count}')
x = int(input())
while x != 0:
x = x - 2
print(x)
a. 0
b. 2
c. 4
d. 7
x = 18
while x % 3 == 0:
print(x, end=' ')
x = x // 3
a. 6
b. 6 2
c. 18 6
d. 18 6 2
a. 3 7
b. 3 7 0
c. 3 7 0 2
d. 3 7 0 2 -1
11) Which is an essential feature of a while loop having the following form?
while loop_expression:
loop_body
12) How many times does the while loop execute for the given input values of -1 4 0 9?
user_num = 3
while user_num > 0:
# Do something
user_num = int(input())
a. 0
b. 1
c. 2
d. 3
13) Which expression replaces ZZZ to make the loop ask for names until 'quit' is
entered?
a. name == 'quit'
c. name != 'quit'
d. 'quit' is False
14) How many times will the body of the loop be executed?
number = 70
guess = 55
while number != guess:
if number > guess:
guess = guess + 10
else:
guess = guess - 1
print(f'The number is: {guess}')
a. 2
b. 3
c. 7
d. 15
15) Fill in the blank so that the loop displays all odd numbers from 1 to 100.
i = 1
while i <= 100:
print(i)
i = _____
a. 1
b. i + 1
c. 2
d. i + 2
i = 5
while i < 10:
print(i)
i = i + 1
a. 0
b. 4
c. 5
d. 6
i = 0
while i <= 100:
print(i)
i = i + 2
a. 0
b. 49
c. 50
d. 51
x = 0
i = 5
while i > 1:
x = x + i
i = i - 1
a. 0
b. 12
c. 14
d. 15
x = 0
i = 1
while i <= 6:
x += i
i += 2
a. 4
b. 9
c. 15
d. 21
a. Xu:33
Bob:24
Jill:18
b. Bob:24
Jill:18
Xu:33
d. Xu:24
Bob:18
Jill:33
21) What is the missing function name so that the output is: Cairo New York Paris
Sydney?
a. reversed
b. backwards
c. list
d. inverse
22) Fill in the blank so that the output is a count of how many negative values are in
temperatures?
temperatures = [-2, 8, 4, -7, 18, 3, -1]
count = 0
for t in temperatures:
if _____:
count = count + 1
print(f'Total negative temperatures: {count}')
a. t < 0
b. temperatures < 0
c. temperatures[t] < 0
d. t[temperatures] < 0
rentals = {
'skis' : 20.00,
'boots' : 10.00,
'skates' : 4.00
}
for x in rentals:
print(x, end=' ')
d. x x x
24) Which XXX / ZZZ outputs every name/grade pair in the dictionary, as in: Jennifer: A?
grades = {
'Jennifer' : 'A',
'Ximin' : 'C',
'Julio' : 'B',
'Jason' : 'C'
}
for XXX:
print(ZZZ)
a. 4
b. 0 1 2 3
c. 1 2 3 4
d. 0 1 2 3 4
a. 1 4 7
b. 1 11 21
c. 1 3 6 9
d. 1 4 7 10
27) Which range() function call generates every even number between 20 and 30
(including both 20 and 30)?
a. range(20, 30, 2)
b. range(20, 31, 2)
c. range(30, 20, 2)
28) Which choice fills in the blank so that the output prints one line for each item in
sports_list, as in: 1. Hockey?
b. range(len(sports_list-1)
c. range(1, len(sports_list))
d. range(1, len(sports_list)-1)
29) The following program prints the number of integers in my_list that are greater than
the previous integer in the list. Which choice fills in the blank to complete the for loop?
my_list = [ 3, 2, 7, 8, 6, 9 ]
count = 0
for _____:
if my_list[i] > my_list[i-1]:
count = count + 1
print(count)
a. i in range(0, len(my_list))
b. i in range(0, len(my_list)+1)
c. i in range(1, len(my_list))
d. i in range(1, len(my_list)+1)
30) Which of the following loops is best implemented with a for loop?
c. Starting from a user-entered integer, increment the value until the value is a prime
number.
d. Reading values from a temperature sensor until it gives a value greater than 100
degrees.
31) Which of the following loops is best implemented with a while loop?
b. Counting how many keys in a dictionary start with the letter 'A'.
for i in range(10):
for j in range(3):
print(f'{i}. {j}')
a. 3
b. 10
c. 13
d. 30
a. 4
b. 6
c. 9
d. 36
34) Which XXX/YYY combination will create a rectangle of '*' characters, with 5 rows,
and each row containing 10 '*' characters?
for XXX:
for YYY:
print('*', end='')
print()
a. i in range(5) / j in range(10)
b. i in range(10) / j in range(5)
c1 = 'c'
while c1 > 'a':
for i in range(3):
print(f'{c1}{i}', end=' ')
c1 = chr(ord(c1) - 1)
a. c1 c2 c3 b1 b2 b3
b. c2 c1 c0 b2 b1 b0
c. c0 c1 c2 b0 b1 b2 a0 a1 a2
d. c0 c1 c2 b0 b1 b2
36) A programmer must write a 500 line program. Which is most likely the best
approach?
a. Write 1 line, run and debug, write 1 more line, run and debug, repeat
b. Write 10-20 lines, run and debug, write 10-20 more lines, run and debug, repeat
c. Write 250 lines, run and debug, write 250 lines, run and debug
37) A programmer must write a program that lists all the words that appear in a text file
that occur more than 10 times. Which of the following tasks would be a good first step in
an incremental programming process?
c. Display a table of all the words in the file, with how many time that word occurs.
d. Write any import statements needed for the program, and print "Done Step 1".
num = 10;
while num <= 15:
print(num, end=' ')
if num == 12:
break
num += 1
a. 10
b. 10 11
c. 10 11 12
d. 10 11 12 13 14 15
for j in range(2):
for k in range(4):
if (k == 2):
break
print(f'{j}{k}', end=' ')
a. 00 01 02
b. 00 01 02 03
c. 00 01 10 11
d. 00 01 02 10 11 12
for i in range(11):
if i == 6:
continue
else:
print(i, end=' ')
a. 0 1 2 3 4 5
b. 0 1 2 3 4 5 6
c. 0 1 2 3 4 5 7 8 9
d. 0 1 2 3 4 5 7 8 9 10
41) What is the ending value of z?
z = 0
a = 5
while a > 0:
a = a - 1
if a == 2:
continue
z = z + a
a. 7
b. 8
c. 9
d. 10
a. Done
b. 13 984 Done
a. 3
b. 4
c. Done
d. 3 Done
a. 3 8 5 15 12 32 45
b. *3 8 5 15 12 32 45
d. 3 8 *5 15 *12 32 45
num_list = [ 8, 2, 1, 3, 4, 7, 6 ]
for index, value in enumerate(num_list):
if index == value:
print('*', end='')
print(value, end=' ')
a. 8 2 1 *3 4 7 6
b. 8 *2 1 3 4 7 6
c. 8 2 1 *3 *4 7 *6
d. *8 *2 *1 *3 *4 *7 *6