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

Test 1 Review A: Questions

R1a.1.

If x's value is 18, what is the value of each of the following Python expressions?

a. 3 + x * 2

b. x % 7

c. x // 5

R1a.2.

If k's value is 5, what is the value of each of the following Python expressions?

a. 10 + 2 * k ** 2

b. 127 % k

c. 52 // k * k

R1a.3.

List the values of a, b, and c following the execution of the below fragment.

a = 10
b = 20
c = 40

a = b
a = c
R1a.4.

List the values of a, b, and c following the execution of the below fragment.

a = 10
b = 20
c = 40

a = b
b = c
c = a
R1a.5.

If name refers to the string Arkansas, what is name[3:6]?

R1a.6.

What are the values referenced by a, b, and c following the execution of the below fragment?

a = 'x'
b = 'y'
c = 'z'

a = b
b = c
c = a + 'b'
R1a.7.

The following program will not work, even for a simple input such as 16. Explain what is wrong, and show how to repair the program to work as intended.

x = float(input())
print('Square root is ' + (x ** 0.5))
R1a.8.

Complete the below program so that it reads a string from the user and displays the string where the first letter has been moved to the end and followed by ay. For example, the input nix should produce the output ixnay, or road should produce oadray.

word = input()
R1a.9.

Write a program that reads two integers from the user and displays the two numbers in increasing order, separated by a comma. For example, if the user enters 4 and then 8, it should display “4, 8”; the same output should result if the user enters 8 and then 4.

R1a.10.

Tabulate all values taken on by the variables i, j, and j as the program fragment below executes.

j = 3
k = 4
for i in range(14):
    j = k - j
    k = k + i
R1a.11.

This question removed to Hydra.

R1a.12.

The harmonic number for an integer n is the sum of the reciprocals of the integers from 1 to n. For example, the harmonic number for 3 is 1/1 + 1/2 + 1/3 = 1 5/6. Complete the below program so that it computes and displays the harmonic number corresponding to the integer given by the user. If the user enters 2, the program should display 1.5; for 3, it should display 1.83333333333.

print('Find harmonic number for: ')
n = int(input())
R1a.13.

This question removed to Hydra.

R1a.14.

Complete the program below so that it computes another string containing every other letter from the string typed by the user. For example, if the user enters blackberry, the program should compute baker, and if the user enters steadfastness, the program should compute sedates.

Note that your program should build up its result in the out variable rather than display letters as it goes through the string.

word = input()
out = ''

# your solution here

print('Result is: ' + out)
R1a.15.

What is displayed by the below sequence of Python statements?

data = [235711]
print(data[2])
data[3] = data[1]
print(data[3])
data[1] = 5
print(data[3])
R1a.16.

Suppose vals refers to the list of integers [8, 7, 6, 5, 4]. What will be in this list after executing the following fragment?

for i in range(1len(vals)):
    vals[i] = vals[i - 1] + 1
R1a.17.

This question removed to Hydra.

R1a.18.

Complete the below program fragment so that, given a list vals of integers, it creates another list prefix containing the sum of the entries of vals up to that entry. For example, if vals is [3, 4, 5] before your fragment, then the fragment will create prefix as [3, 7, 12]; the first entry is the same as in vals, the second entry is the sum of the first two entries of vals (3 + 4), and the third entry is the sum of the first three entries of vals (3 + 4 + 5).

prefix = len(vals) * [0]   # creates a list the same length as vals, all 0's

Test 1 Review A: Solutions

R1a.1.

a. 3 + x * 2 is 39

b. x % 7 is 4

c. x // 5 is 3

R1a.2.

a. 10 + 2 * k ** 2 is 60

b. 127 % k is 2

c. 52 // k * k is 50

R1a.3.
a:40
b:20
c:40
R1a.4.
a:20
b:40
c:20
R1a.5.
ans
R1a.6.
a: 'y'
b: 'z'
c: 'yb'
R1a.7.

The value of x ** 0.5 is a floating-point number, and Python does not allow adding a string and a number. The solution is to use the str function to compute the string correspoding to x ** 0.5; that string can be added to another string:

print('Square root is ' + str(x ** 0.5))
R1a.8.
word = input()
first = word[0]
rest = word[1:len(str)]
print(rest + first + 'ay')
R1a.9.
a = int(input())
b = int(input())
smaller = min(ab)
larger = max(ab)
print(str(smaller) + ', ' + str(smaller))
R1a.10.
i: 1, 2, 3
j: 3, 1, 4, 3
k: 4, 5, 7, 10
R1a.11.

This question removed to Hydra.

R1a.12.
An answer Alternative answer
total = 0
for i in range(1n + 1):
    total = total + 1.0 / i
print(total)
total = 0
for i in range(n):
    total = total + 1.0 / (i + 1)
print(total)
R1a.13.

This question removed to Hydra.

R1a.14.
An answer Alternative answer
for i in range(0len(word), 2):
    out = out + word[i]
for i in range(len(word) // 2):
    out = out + word[2 * i]
R1a.15.
5
3
3
R1a.16.

[8, 9, 10, 11, 12]

R1a.17.

This question removed to Hydra.

R1a.18.
An answer Alternative answer
for i in range(len(vals)):
    prefix[i] = sum(vals[0:i + 1])
total = 0
for i in range(len(vals)):
    total = total + vals[i]
    prefix[i] = total