# CS101 Final Exam (Solutions)

Stanford CS101 Final Exam, Spring 2013-2014

Name (last, first): __________________, _________________

I agree to the letter and spirit of the honor code, that no illicit aid has been used or provided:

(signed)______________________________________

Scoring:
SA 1 ____ / 10
SA 2 ____ / 10
SA 3 ____ / 10
IM 1 ____ / 6
IM 2 ____ / 8
IM 3 ____ / 12
IM 4 ____ / 12
TA 1 ____ / 6
TA 2 ____ / 8
TA 3 ____ / 10
SS 1 ____ / 8
_____ / 100

• This is a 2 hour, closed note, closed computer, closed phone, closed calculator exam
• Just you and your pen!
• The points for each problem are in parens e.g. (4) (not set yet)
• We will not mark off for superficial syntax errors, so long as we can discern the right idea
• For answers involving math, you can write an expression, such as "100 * 6 / 20" and we will do the math for you.
• For all image problems, you may omit the final `print(image);` line
• When using the avg detection technique, you may omit the `* 1.2` factor
• Good luck!

## Code Reference

Code reference "cheat sheet"

```Image loop:
image = new SimpleImage("something.jpg");
for (pixel: image) {
pixel.setRed(0);
}
print(image);

Image functions:
-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)
-image.setSameSize(other_image)

If-Statement:
if (100 > 50) {
// body lines
}

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

Table functions:
-table.convertToLowerCase()
-row.getField(field_name)
-s1.startsWith(s2), s1.endsWith(s2)

Table field names:
baby: name, rank, gender, year
survey: color, tv, gender, movie, book, sport, soda

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

a. Draw a line from each item on the left to its category on the right. One of the categories on the right goes with 2 items on the left.

```CPU                        Persistent storage

Flash Drive                Brains

RAM                        Temporary storage

Hard Disk
```

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

c. We have a 10 MB image file. How long does it take to transmit the file on a 100 megabits-per-second network connection. As usual for our network-math problems, assume that there is zero overhead.

d. Alice takes a picture resulting in a 15 MB image file once a day for 300 days. How much space do they all take up in GB?

a. In the original ethernet design, before transmitting a packet, a computer waits for what?

b. To send and receive packets on the internet, each computer is identified by its what?

c. Suppose a computer receives a packet, computes its checksum, and the computed checksum matches the packet's embedded checksum. What does this imply?

d. Below is a line of HTML code. Draw what this HTML looks like in the browser.

It was the best of times, it was <b>the worst</b> of times.

```
```

e. Here is a url http://www.stanford.edu/class/cs101/example.html

Circle the domain name part of the url.

a. What is the output of an analog-digital (A/D) converter?

b. Suppose Alice has a pub/priv key pair and the pub key is public, as usual. What steps does Bob follow to encrypt a file to be sent to Alice?

c. What are three different ways that a bad guy could obtain your password to a site?

d. What distinguishes an EMV card from the previous generation of cards?

## Image-1 (6)

Write code to change every pixel in the image like this: double the green value and and set the blue value to 0.

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

```

## Image-2 (8)

Suppose we have an image showing a red balloon in front of blue sky. For this question, parts (a) and (b) each require just a single line of if-statement code and nothing else.

a. Assume that the loop and avg = xxx code are all provided. Write the one-line if-statement code to detect a balloon pixel by testing its red value using the avg technique:

```image = new SimpleImage("something.jpg");
for (pixel: image) {
avg = < something >
// Write if-statement here:

```

b. Now write an if-statement that detects the balloon pixel just as in (a), but in this case use only its blue value with the avg technique:

```image = new SimpleImage("something.jpg");
for (pixel: image) {
avg = < something >
// Write if-statement here:

```

## Image-3 "Eye Of The Tiger" (12)

We have an image of a yellow and black striped tiger in front of a green jungle background. We have a "back" image that is the same size, showing lots of little orange kittens. Write bluescreen code to detect the yellow parts of the tiger with the avg technique and replace the yellow tiger pixels with kitten pixels from the back image.

```image = new SimpleImage("tiger.jpg");
back = new SimpleImage("kittens.jpg");
for (pixel: image) {

```

## Image-4 (12)

We have an image 800 pixels wide and 500 pixels high, and a back image of the same size. Here you'll write code to change the back image. Consider pixels in the image that are in a vertical stripe 200 pixels wide running down the right edge of the image. Within that stripe, if the red and blue values of a pixel are both over 200, then blend the blue value of that pixel divided by 10 into the corresponding blue pixel value of the back image.

```image = new SimpleImage("something.jpg");
back = new SimpleImage("back.jpg");
for (pixel: image) {

```

## Table-1 (6)

For this question we'll use the survey fields "color" and "soda". Write code to print all the rows where color is "red" and soda is not "coke".

```table = new SimpleTable("survey.csv");
table.convertToLowerCase();
for (row: table) {

```

## Table-2 (8)

Write code to count the number of baby names that start with "A" or "B" and print the total count like this:

```count: 142
```
```table = new SimpleTable("baby.csv");
for (row: table) {

```

## Table-3 (10)

This question uses the survey fields "color", "tv", and "gender". We'll say that someone is "hip" if their favorite color is "blue" and their favorite TV show is "game of thrones". What is the gender breakdown of hipness? Write code to count and print the number of hip males and females like this:

```male: 17
female: 18
```
```table = new SimpleTable("survey.csv");
table.convertToLowerCase();
for (row: table) {

```

For this problem, you have the following spreadsheet with raw data for a stadium-step running class, split into morning and afternoon groups.

a. What exactly goes in the cell immediately to the right of "Morning Total:" to compute the total of the morning numbers?

b. What exactly goes in the cell immediately to the right of "Afternoon Total:" to compute the total of the afternoon numbers?

c. For some reason, the Grand Total is defined as 10 times the morning total plus 20 times the afternoon total. What exactly goes in the cell immediately to the right of "Grand Total:" to compute this?

## Solutions

```SA1
a.
cpu = brains
flash drive + hard disk = persistent storage
RAM = temporary storage
b. 256
c. 8/10 second
d. 4500 MB = 4.5 GB

SA2
a. quiet on the wire
c. The packet was not corrupted/messed-up in transit
d. It was the best of times, it was the worst of times
e. www.stanford.edu

SA3
a. numbers or samples
b. encrypt with the pub key
c. external guessing (dictionary), phishing, malware/keylogger, social engineering, hack site password table
d. chip on the card, secret "key" on the card does not leave the card

Image-1
pixel.setGreen(pixel.getGreen() * 2);
pixel.setBlue(0);

Image-2
a.  if (pixel.getRed() > avg) {  // i.e. the -high vs. -low strategies
b.  if (pixel.getBlue() < avg) {

Image-3
avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
// yellow is red+green, so look for red
if (pixel.getRed() > avg) {
// this is standard bluescreen code
pixel2 = back.getPixel(pixel.getX(), pixel.getY());
pixel.setRed(pixel2.getRed());
pixel.setGreen(pixel2.getGreen());
pixel.setBlue(pixel2.getBlue());
}
common errors: incorrectly detecting pixels to change. (should be (pixel.getRed() > avg)

Image-4
if (pixel.getX() > 600 &&
pixel.getRed() > 200 &&
pixel.getBlue() > 200) {
pixel2 = back.getPixel(pixel.getX(), pixel.getY());
pixel2.setBlue(pixel2.getBlue() + pixel.getBlue()/10);  // much going on on this line
}

Table-1
if (row.getField("color") == "red" && row.getField("soda") != "coke") {
print(row);
}

Table-2
count = 0;
table = new SimpleTable("baby.csv");
for (row: table) {
// use || in the test
if (row.getField("name").startsWith("A") ||
row.getField("name").startsWith("B")) {
count = count + 1;
}
}
print("count:", count);

common mistakes: initializing count inside for-loop, failing to initialize count,
printing count in every iteration of for-loop, using separate counts for A, B,
but not summing at the end.

Table-3
count1 = 0;
count2 = 0;
table = new SimpleTable("survey.csv");
for (row: table) {
if (row.getField("color") == "blue" &&
row.getField("tv") == "game of thrones" &&
row.getField("gender") == "male") {
count1 = count1 + 1;
}

if (row.getField("color") == "blue" &&
row.getField("tv") == "game of thrones" &&
row.getField("gender") == "female") {
count2 = count2 + 1;
}
}
print("male:", count1);
print("female:", count2);

common mistakes: not initializing counts, initializing counts inside for-loop,
failing to print, printing inside for-loop, failing to update one of the counts.

SS1
a. =sum(b5:b10)
b. =sum(e5:e13)
c. =b11*10 + e14*20
```