0% found this document useful (0 votes)
5 views16 pages

Chapter - 5 Quiz Programming Joseph-Ira 12.01.2024

The document contains a series of programming questions focused on loops, conditionals, and data structures in Python. Each question presents a scenario or code snippet and asks for specific outputs or the correct way to implement certain logic. The questions cover a range of topics including while loops, for loops, input handling, and dictionary operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Chapter - 5 Quiz Programming Joseph-Ira 12.01.2024

The document contains a series of programming questions focused on loops, conditionals, and data structures in Python. Each question presents a scenario or code snippet and asks for specific outputs or the correct way to implement certain logic. The questions cover a range of topics including while loops, for loops, input handling, and dictionary operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 5

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.

val = Get next input


XXX

While val is not -1


YYY
val = Get next input

Put sum to output

a. sum = val / sum = val

b. sum = val / sum = sum + val

c. sum = 0 / sum = sum + val

d. sum = 0 / sum = val

2) How many times will the body of the loop execute?

my_list = [6, 2, 8, -1, 12, 15, -7]


x = Get first my_list value

While x is not negative


put "Positive number!" to output
x = Get next my_list value

Put "Done" to output

a. 3

b. 4

c. 5
d. 7

3) What is the ending value of count?

my_list = [3, -4, 0, -1, 2, 1, 8]


n = 0
count = 0

While n < length of my_list


If my_list[n] > 0
count = count + 1
n = n + 1

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. XXX: val < 0, YYY: n = n + 1

b. XXX: val < 0, YYY: n = n + val

c. XXX: val > 0, YYY: n = n + 1

d. XXX: val < 0, YYY: val = val + 1

5) Which input value causes "Goodbye" to be output next?


x = int(input())
while x >= 0:
# Do something
x = int(input())
print('Goodbye')

a. -1

b. 0

c. 1

d. No such value

6) Which input for variable c causes "Done" to be output next?

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

c. Any value other than 'y'

d. No such value (infinite loop)

7) What is the output?

count = 0
while count < 3:
print('loop')
count = count + 1
print(f'Final value of count: {count}')

a. Prints 'loop' once, then 'final value of count: 1'

b. Prints 'loop' three times, then 'final value of count: 3'

c. Prints 'loop' three times, then 'final value of count: 4'

d. Prints 'loop' forever (infinite loop)


8) What initial value of x will cause an infinite loop?

x = int(input())
while x != 0:
x = x - 2
print(x)

a. 0

b. 2

c. 4

d. 7

9) What is the output?

x = 18
while x % 3 == 0:
print(x, end=' ')
x = x // 3

a. 6

b. 6 2

c. 18 6

d. 18 6 2

10) What is the output?

my_list = [3, 7, 0, 2, -1, 8]


index = 0
while my_list[index] > 0:
print(my_list[index], end=' ')
index += 1

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

a. The loop_expression should be affected by the loop_body

b. The loop_expression should not be affected by the loop_body

c. The loop_body should get user input

d. The loop_body should update at least two variables

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?

name = input("What is your name ('quit' to exit)? ")


while ZZZ:
print(f'Hello, {name}')
name = input("What is your name ('quit' to exit)? ")

a. name == 'quit'

b. name is not '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

16) How many times does the following loop iterate?

i = 5
while i < 10:
print(i)
i = i + 1

a. 0

b. 4
c. 5

d. 6

17) How many times does the following loop iterate?

i = 0
while i <= 100:
print(i)
i = i + 2

a. 0

b. 49

c. 50

d. 51

18) What is the ending value of x?

x = 0
i = 5
while i > 1:
x = x + i
i = i - 1

a. 0

b. 12

c. 14

d. 15

19) What is the ending value of x?

x = 0
i = 1
while i <= 6:
x += i
i += 2

a. 4
b. 9

c. 15

d. 21

20) What is the output?

names = ['Bob', 'Jill', 'Xu']


ages = [24, 18, 33]
for index in [2, 0, 1]:
print(f'{names[index]}:{ages[index]}')

a. Xu:33
Bob:24
Jill:18

b. Bob:24
Jill:18
Xu:33

c. Xu, Bob, Jill:33, 24, 18

d. Xu:24
Bob:18
Jill:33

21) What is the missing function name so that the output is: Cairo New York Paris
Sydney?

cities = ['Sydney', 'Paris', 'New York', 'Cairo']


for c in _____(cities):
print(c, end=' ')

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

23) What is a possible output?

rentals = {
'skis' : 20.00,
'boots' : 10.00,
'skates' : 4.00
}
for x in rentals:
print(x, end=' ')

a. skis boots skates

b. 20.00 10.00 4.00

c. skis: 20.00 boots: 10.00 skates: 4.00

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. name in grades / name + ': ' + grade


b. grade in grades / name[grades] + ': ' + grade

c. name in names / grades[name] + ': ' + grades[grade]

d. name in grades / name + ': ' + grades[name]

25) What sequence is generated by range(4)?

a. 4

b. 0 1 2 3

c. 1 2 3 4

d. 0 1 2 3 4

26) What sequence is generated by range(1, 10, 3)

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)

d. range(20, 22, 24)

28) Which choice fills in the blank so that the output prints one line for each item in
sports_list, as in: 1. Hockey?

sports_list = [ 'Hockey', 'Football', 'Cricket' ]


for i in _____:
print(f'{i+1}. {sports_list[i]}')
a. range(len(sports_list))

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?

a. Asking a user to enter names until the user enters 'Quit'.

b. Counting the number of negative values in a list of integers.

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?

a. Checking to see if a list of integers contains the value 12.

b. Counting how many keys in a dictionary start with the letter 'A'.

c. Asking the user to enter positive integers, exiting by entering -1.


d. Looping through the characters in a string, and displaying 'yes' if it contains a vowel.

32) How many times will the print statement execute?

for i in range(10):
for j in range(3):
print(f'{i}. {j}')

a. 3

b. 10

c. 13

d. 30

33) How many times will the print statement execute?

for i in range(1, 3):


for j in range(8, 12, 2):
print(f'{i}. {j}')

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)

c. i in range(1, 5) / j in range(1, 10)

d. i in range(0, 4, '*'), j in range(0, 9, '*')


35) What is the output?

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

d. Write 500 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?

a. Display a list of all the unique words in the file.

b. Display the file's contents.

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

38) What is the output?

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

39) What is the output?

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

40) What is the output?

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

42) What is the output?

id_list = [13, 984, 231, 140]


for id in id_list:
if id != 231:
print(id, end=' ')
else:
print('Done')

a. Done

b. 13 984 Done

c. 13 984 231 140 Done

d. 13 984 140 Done

43) What is the output?

names = [ 'Gerry', 'Preet', 'Jimin', 'Susan' ]


index = 0
while index < len(names):
if names[index] == 'Susan':
break
else:
index += 1
else:
print('Done')
print(index)

a. 3

b. 4

c. Done

d. 3 Done

44) What is the output?

num_list = [ 3, 8, 5, 15, 12, 32, 45 ]


for index, value in enumerate(num_list):
if index > 0:
if value < num_list[index-1]:
print('*', end='')
print(value, end=' ')

a. 3 8 5 15 12 32 45

b. *3 8 5 15 12 32 45

c. *3 *8 5 *15 12 *32 *45

d. 3 8 *5 15 *12 32 45

45) What is the output?

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

You might also like