CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Test 1 Review B: Questions

R1b.1.

Write a Python fragment that creates a list named powers containing the first 100 powers of 2: 1, 2, 4, 8,….

R1b.2.

Tabulate the values taken on by each variable as the program fragment below executes.

k = 50
for i in range(6):
    j = 2 ** i
    if i % 2 == 0:
        k = j + k
R1b.3.

Tabulate the values taken on by each variable as the program fragment below executes.

n = 32
k = n + 1
for i in range(27):
    if n % i == 0:
        k = k + i + (n // i)
R1b.4.

Tabulate the values taken on by the variables i and result as the program fragment below executes.

word = 'bananas'
result = 'f'
for i in range(4):
    if word[i:i + 2] != 'na':
        result = result + word[i]
R1b.5.

What will be in the list nums after executing the following fragment?

nums = [422355]
for i in range(6):
    if nums[i] == i:
        nums[i] = nums[i - 1] + 1
R1b.6.

Suppose nums were a list of integers containing [11, 12, 12, 13, 15, 15, 15, 18]. What would be in nums after executing the below fragment?

j = 0
for i in range(1len(nums)):
    if nums[i] != nums[i - 1]:
        nums[j] = num[i]
        j = j + 1
R1b.7.

What are the first and last lines displayed by the below program fragment?

for i in range(12344321):
    s = str(i)
    if s[1] == '4':
        print(s)
R1b.8.

This question has been moved to Hydra.

R1b.9.

This question has been moved to Hydra.

R1b.10.

This question has been moved to Hydra.

R1b.11.

Suppose we have two lists, an lists of strings called candidates which enumerates the names of candidates running in an election, and a list of integers (of the same length) called votes which has the number of votes tallied for each candidate. For example, votes[0] contains the number of votes garnered by the candidate whose name is in candidates[0].

Write a program fragment that prints the name of the candidate who received the most votes in the election. You may assume that there is not a tie.

Test 1 Review B: Solutions

R1b.1.
powers = 100 * [0]
for i in range(100):
    powers[i] = 2 ** i
R1b.2.
i: 0, 1, 2, 3, 4, 5
j: 1, 2, 4, 8, 16, 32
k: 50, 51, 55, 71
R1b.3.
n 32
k 33, 51, 63
i 2, 3, 4, 5, 6
R1b.4.
i:0,1,2,3
result:f,fb,fba,fbaa
R1b.5.
[4, 2, 3, 4, 5, 6]
R1b.6.

[12, 13, 15, 18, 15, 15, 15, 18]

R1b.7.

The first is 1400 and the last is 3499.

R1b.8.

This question has been moved to Hydra.

R1b.9.

This question has been moved to Hydra.

R1b.10.

This question has been moved to Hydra.

R1b.11.
An answer Alternative answer
max_pos = 0
for i in range(1len(candidates)):
    if votes[i] > votes[max_pos]:
        max_pos = i
print(candidates[max_pos])
max_votes = max(votes)
for i in range(len(candidates)):
    if votes[i] == max_votes:
        print(candidates[i])