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

Test 2 Review A: Questions

R2a.1.

Both functions and methods allow names to be associated with more complex computations. For example, with strings, you can use the len function and the count method. What distinguishes them?

R2a.2.

What does the below fragment display if next is a list of integers containing [3, 4, 5, −1, 2, 6, −2].

loc = 1
while loc >= 0:
    print(loc)
    loc = next[loc]
R2a.3.

Write a regular expression that accepts only strings of letters that begin with a capital letter followed by one or more lower-case letters.

R2a.4.

Write a regular expression for strings containing two integers separated by either a hyphen or a slash. Examples include 450-1377 and 12/25.

R2a.5.

Write a regular expression that describes strings containing the lower-case letter e followed by a digit, with possibly some intervening spaces. Examples include 3e8, the  100K, and e2, but not e-5.

R2a.6.

Write a regular expression that describes all words of lower-case letters that begin with w and contain an r. Examples include weird, word, wrong, and wear, but not awkward, war-weary, or www.hendrix.edu. (The latter two are bad because they contain characters that are not lower-case letters — a hyphen and a period.)

R2a.7.

Write a regular expression for each of the following concepts.

a. a string of alphabetic letters starting with the letter c
b. a string of digits that doesn't start with 0
R2a.8.

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

n = 1
for i in range(14):
    for j in range(14):
        k = i * j
        n = n + k
R2a.9.

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

n = 1
for i in range(14):
    for j in range(14):
        k = i * j
    n = n + k
R2a.10.

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

base = 'barber'
res = 'a'
for i in range(len(base)):
    k = -1
    for j in range(i):
        if base[j] == base[i]:
            k = j
    if k < 0:
        res = res + base[i]
print(res)
R2a.11.

What is displayed by the following program?

for i in range(5):
    template = '{1:4.2f} {0:3d}'
    output = template.format(ii * 0.05)
    print(output)
R2a.12.

What value for template would result in the following strings being produced?

template.format(100031.6228'sqrt') → “sqrt(1000) = 31.623
template.format(90009.1050'log') → “log (9000) =  9.105

Test 2 Review A: Solutions

R2a.1.

A function is initiated using just its name, and any information it needs is supplied in the parameters, but a method must be invoked by preceding the method name with the value it's operating on. Thus, if we have a string variable called name, you'd invoke the count method by writing name.count('e'), but you'd invoke the len function by writing len(name).

R2a.2.
1
4
2
5
6
R2a.3.
[A-Z][a-z]+
R2a.4.
[0-9]+[-/][0-9]+
R2a.5.
.**[0-9].* (There is a space between the e and the asterisk.)
R2a.6.
w[a-z]*r[a-z]*
R2a.7.
a. c[a-zA-Z]*
b. [1-9][0-9]*
R2a.8.
i:1, 2, 3
j:1,2,3,1,2,3,1,2,3
k:1,2,3,2,4,6,3,6,9
n:1,2,4,7,9,13,19,22,28,37
R2a.9.
i:1, 2, 3
j:1,2,3,1,2,3,1,2,3
k:1,2,3,2,4,6,3,6,9
n:1,4,10,19
R2a.10.
i 0, 1, 2, 3, 4, 5
j 0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4
k −1, −1, −1, −1, 0, −1, −1, 2
res ab, aba, abar, abare

The program displays abare.

R2a.11.
0.00   0
0.05   1
0.10   2
0.15   3
0.20   4
R2a.12.
{2:4s}({0:4d}) = {1:6.3f}