Pre Final

< CS101

This is information and some sample problems in advance of the final exam. (I'll also publish the 2013 exam separately.) The final exam will be in Hewlett 200 (near our regular room), June 10th 7:00-9:00pm (2 hours). This is a closed-note exam. Just bring yourself and a couple pens.

This page includes all of the 2012 final and solutions. Note that the topics that quarter were similar but not identical to the topics this quarter.

The exam will look a lot like the midterm but twice as long: closed-note with a mix of short answer and code-writing questions spanning the whole quarter. In particular, the code writing will cover images processing, tables, and spreadsheets (spreadsheets limited to the small set of things we did on the spreadsheet homework problems). Note that the "cheat sheet" section below now includes table and spreadsheet syntax.

Writing code on a piece of paper is different from typing code into the computer, and our grading criteria take that into account. We do not grade off for trivial syntax errors that you would easily catch when running on the computer. So if you write poxel.setReeed(4); or omit one parenthesis or comma or something, we'll give full credit, so long as the key ideas of the solution are there and we can see what you meant.

This is closed everything exam: no notes, no computer, no cell phone, no calculator. Just arrive with you and your pen.

The exam will contain the reference cheat sheet below listing the major phrases of syntax we have used, so you don't need to memorize the superficial details. The points on the exam will come from using the syntax to solve little problems, just like the homework. If a question requires a little rote code at the start, often the question will include that code already done, and the points will be from writing the rest of the required code.

How To Study

My advice to study for the exam is that you be comfortable with all the homework code problems and lecture code examples. You should be able to type in the code for any of these without looking at any example code. This is a higher bar than the homeworks, where you very likely scrolled back to remind yourself from earlier examples. A great way to study is to select a problem, delete any existing code, and then see if you can type in a reasonable solution just from reading the problem statement.

Code reference "cheat sheet" to be included with exam:

Image loop:
 image = new SimpleImage("something.jpg");
 for (pixel: image) {

 -pixel.setRed(val)  // likewise for green and blue
 -pixel.getRed()     // likewise for green and blue
 -pixel.getX(), pixel.getY()
 -image.getWidth(), image.getHeight()
 -image.getPixel(x, y)

 if (100 > 50) {
   // body lines

Table loop:
 table = new SimpleTable("baby-2010.csv");
 for (row: table) {

Table functions:
 -s1.startsWith(s2), s1.endsWith(s2)
Table field names:
 baby: name, rank, gender, year
 survey: color, tv, movie, book, sport, soda

  =sum(B1:B10)   // sum up column of numbers
  =A1 + A2 * 10   // arithmetic based on other cells  

Short Answer 1 (10)

a. Circle any of the following which offer persistent byte storage:

CPU, Flash Memory, Hard Drive, Random Access Memory (RAM), Ethernet


b. How many distinct patterns can be stored with 3 bits?


c. Note that Firefox is written in C++. What is the predominant type of instruction stored in the Firefox.exe file?


d. Alice has 1000 images, each 400 KB in size. About how much space do they take up overall?


e. Suppose we have a video camera that records at a rate of 2 GB per hour. Recording for 15 minutes creates about how much data, expressed in MB?


Short Answer 2 (15)

a. Suppose computers A, B, C, and D are connected to an ethernet cable, similar to the setup shown in lecture. Computer A begins transmitting a packet on the cable, but it collides with another transmission, so A stops transmitting. What does computer A do next?


b. Suppose we are using the following checksum scheme: add up all the bytes, and take just the last two digits of the sum. What is the checksum of the following bytes: 101, 202, 103, 100, 210, 120


c. What problem is addressed by checksums? (one sentence answer)


d. A computer connected to the internet is identified by what kind of address (the specific name, not just "internet address"):


e. Circle any of the addresses below which are not valid internet addresses:,,,,


f. True or false. Routing on the internet from Nick's office to the east bay is about 15 hops, and to Poland is about 1000 hops.


g. The following is the output of a famous networking utility.

 1  yoza-vlan70 (  2.039 ms
 2  bbra-rtr-a (  0.932 ms
 3  boundarya-rtr (  3.174 ms
 4  dca-rtr (  27.085 ms
 5 (  2.485 ms
 6 (  3.262 ms
 7 (  4.046 ms
 8 (  14.252 ms
 9 (  9.117 ms
10 (  4.975 ms
11 (  4.761 ms

What does this output show? (one sentence)


h. You open your laptop and type "" into your browser and hit return. Three questions, 1-3 word answers:

What computer does your laptop contact to request this web page?


What is the content of the request?


What is the content of the response?


Short Answer 3 (15)

a. What does the above sound like?


b. Why is a digital signal more resistant to noise?


c. Suppose we have these 6 samples: 1000, 1001, 1010, 1005, 1006, 1002. Consider the compression scheme from lecture, recording the first sample followed by the local delta for each sample. Write out the encoding of the 6 samples using this scheme:


d. Is the above compression scheme lossy or lossless?


e. Suppose we have two versions of the same image, differing only in their JPEG compression level. Image A is compressed at q1 and is 100 KB. Image B is compressed at q5 and is 400 KB. Which of these two images is more likely to exhibit JPEG compression artifacts?


f. Circle any of the following which are video encoding formats:


g. "Network effect" means that users select among competing products based on what feature of each product?


h. Suppose Stanford is using the open source software YoYoMatic widely and is dependent on it. Describe an example scenario where Stanford would gain significant benefit from having access to the YoYoMatic source code.


i. Suppose there is a website and you have a password to log in to it. What are three different, non-physical techniques a bad guy might use to obtain your password (non-physical meaning other than breaking in to your house and making you tell them):

j. Consider the brake pedal in a car -- what is the abstraction the brake pedal presents to the driver?


Problem 4 (5)

Write code to print all the girl rows where the rank is greater than 900.

table = new SimpleTable("baby-2010.csv");

for (row: table) {

Problem 5 (5)

Write code to count the number of people who identified "purple" as their favorite color and print the result in the following format.

purple: 5


table = new SimpleTable("survey-2012.csv");

for (row: table) {

Problem 6 (5)

Here is some code that does something.

image = new SimpleImage("flowers.jpg");
for (pixel: image) {

Suppose that flowers.jpg has 150,000 pixels. Describe the output of the above program:

Problem 7 (5)

This problem is similar to a bluescreen computation. As usual, we have two images -- image and back, and the back image is at least as large as the main image. Write code that for each pixel in image, if the red, green, or blue value is less than 60, then copy just the blue value from back to image.

image = new SimpleImage("image.jpg");
back = new SimpleImage("back.jpg");

for (pixel: image) {

Problem 8 (10)

Write code to print the rows of all names beginning with J, except do not print John or Jess.

table = new SimpleTable("baby-2010.csv");

for (row: table) {

Problem 9 (10)

Consider the people who identified "coke" as their favorite soda. Write code to count and print how many of them identified "red" or "blue" as their favorite color with output in the following format.

red: 6
blue: 8


table = new SimpleTable("survey-2012.csv");

for (row: table) {

Problem 10 (8)

We'll say that a pixel is intense if its red or green value is over 150. Write code below to count the number of intense pixels in flowers.jpg and print result in the following format.

intense: 12623


image = new SimpleImage("flowers.jpg");

for (pixel: image) {

Problem 11 (12)

We'll say that "A" names start with "A" and end with any letter other than "a". Likewise "B" names start with "B" and end with any letter other than "b". Count both types of name and print the result in the following format:

A: 123
B: 61


table = new SimpleTable("baby-2010.csv");

for (row: table) {


Short Answer 1
a. Flash memory, Hard drive
b. 8
c. Machine code instructions
d. 400 MB
e. 500 MB

Short Answer 2
a. A waits a random amount of time
b. 36
c. Detect a storage or transmission error of a group of bytes
d. IP address
e. The one with 257 is the only invalid one
f. false
g. This is traceroute output showing the hops to
h. The computer is The content of the request is /news/today.html.
The response is the HTML from inside the today.html file.

Short Answer 3
a. Low and high notes at the same time.
b. Receiver only needs to distinguish 0 from 1, allowing them to see past then noise.
c. 1000, 1, 9, -5, 1, -4
d. Lossless
e. Image A. i.e. there's no free lunch.
f. h.264
g. How many other people select it.
h. If Stanford needs some particular bug fixed, Stanford could fix it in
the source code and compile their own .exe to use.
i. (here are 4 in rough order of prevalence)  phishing, dictionary attack (guessing),
installed malware on your machine observes/steals password ("keylogger"),
cracking passwords at some other site and hoping that you use the same 
username/password at
j. Pushing the brake pedal slows the car

Problem 4.
table = new SimpleTable("baby-2010.csv");
for (row: table) {
  if (row.getField("gender") == "girl" && row.getField("rank") >= 900) {

Problem 5
table = new SimpleTable("survey-2012.csv");
count = 0;
for (row: table) {
  if (row.getField("color") == "purple") {
    count = count + 1;
print("purple:", count);

Problem 6
It prints the string "pixel" 150,000 times, then the string "image" once.

Problem 7
image = new SimpleImage("image.jpg");
back = new SimpleImage("back.jpg");

for (pixel: image) {
  pixel2 = back.getPixel(pixel.getX(), pixel.getY());
  if (pixel.getRed() < 60 ||
      pixel.getGreen() < 60 ||
      pixel.getBlue() < 60) {

Problem 8
Note: != means not-equals, i.e. the opposite of ==
table = new SimpleTable("baby-2010.csv");
for (row: table) {
  if (row.getField("name").startsWith("J") &&
      row.getField("name") != "John" &&
      row.getField("name") != "Jess") {

Problem 9
table = new SimpleTable("survey-2012.csv");
count1 = 0;  // red
count2 = 0;  // blue
for (row: table) {
  if (row.getField("soda") == "coke" &&
      row.getField("color") == "red") {
    count1 = count1 + 1;

  if (row.getField("soda") == "coke" &&
      row.getField("color") == "blue") {
    count2 = count2 + 1;

print("red:", count1);
print("blue:", count2);

Problem 10
image = new SimpleImage("flowers.jpg");
count = 0;
for (pixel: image) {
  if (pixel.getRed() > 150 ||
      pixel.getGreen() > 150) {
    count = count + 1;
print("intense:", count);

Problem 11
table = new SimpleTable("baby-2010.csv");
count1 = 0;  // A
count2 = 0;  // B
for (row: table) {
  if (row.getField("name").startsWith("A") &&
      !row.getField("name").endsWith("a") ) {
    count1 = count1 + 1;

  if (row.getField("name").startsWith("B") &&
      !row.getField("name").endsWith("b") ) {
    count2 = count2 + 1;

print("A:", count1);
print("B:", count2);