CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Exam 2 Review B: Questions

X2rB.1.

Complete the below Python fragment so that it computes and displays the length of the longest line in the text file.

essay = open('words.txt')
X2rB.2.

Assuming scores.txt is a file of integers, with one integer on each line, complete the below Python fragment so that it computes and displays the average of the numbers in the file.

data = open('scores.txt')
X2rB.3.

What is displayed by the following Python fragment?

words = 'The quick brown fox jumps over the lazy dog'
map = {}
for word in words.split():
    map[len(word)] = word
for k in range(36):
    print k, map[k]
X2rB.4.

Suppose we have a dictionary scores mapping names (each a string) to test scores (each an integer). Write a program fragment that displays the name of each student whose score is 100.

X2rB.5.

Explain as best you can how the IP address 209.65.57.4 comes to be allocated to ozark.hendrix.edu.

X2rB.6.

In the context of networking, why do messages typically include port numbers rather than simply the computers' IP addresses?

X2rB.7.

What does a name server do in the Domain Name System (DNS)?

Exam 2 Review B: Solutions

X2rB.1.
max_length = 0
for line in essay:
    if len(line) > max_length:
        max_length = len(line)
print max_length
X2rB.2.
line_count = 0
total = 0
for line in data:
    line_count += 1
    total += float(line.rstrip())
print 'Average is', total / line_count
X2rB.3.
3 dog
4 lazy
5 jumps
X2rB.4.
for name in scores:
    if scores[name] == 100:
        print name
X2rB.5.

An international agency called ICANN allocates blocks of addresses to Internet service providers, such as AT&T. [Actually, ICANN gives blocks to regional Internet registries, of which there is essentially one for each continent, and these registries allocate within these blocks to ISPs.] They might have given AT&T all addresses that start with 209.65, for example. AT&T then divvies this range of addresses among their customers, such as Hendrix College. Thus, Hendrix College ends up receiving all addresses whose first 22 bits match with 209.65.56.0 — that is, 209.65.56.0 through 209.65.59.255. Hendrix splits its address space some, and they gave the computer science network all addresses whose first 26 bits match with 209.65.57.0. Then the network administrator decided to assign one of those — 209.65.57.4 — to its computer named ozark.

X2rB.6.

A port number, allocated uniquely on the computer for each program running, allows a sender to identify the particular program on the destination computer that should receive the message.

X2rB.7.

A name server receives requests including the name of a computer (such as ozark.hendrix.edu) and responds with the computer's IP address (such as 209.65.57.4).