# CS101 Midterm Solution

Stanford CS101 midterm, Spring 2012-2013

I though the questions on here were pretty demanding, so I was pleased to see so many high scores. The median was 86/100. Solutions and notes and the end of this doc. (The 0's at the far left of the histogram represent people who did not take the exam.) If you have questions about your exam, please give the exam to Nick.

• This is a closed note, closed computer, closed phone, closed calculator exam
• Just you and your pen!
• Time is 60 minutes
• 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" to be printed with exam:

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

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

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)

```

## Problem 1 Short Answer (30)

What does Moore's law state (one sentence)?

Give an example of something computed by a single machine code instruction.

Which of the following provides solid state persistent storage? Circle one or more:
CPU, RAM, hard disk drive, flash drive, none of these

What does "2 gigahertz" mean?

How many distinct patterns can be stored with one bit?

How many distinct patterns can be stored with one byte?

Suppose you write 10 essays a year for 4 years at Stanford. Each essay takes up 80 KB on average. How much space is required to store the 4 years of Stanford essays in MB?

Suppose you keep 10 digital images a year for 4 years at Stanford. Each image takes up 4 MB on average. How much space is required to store the 4 years of images in MB?

You have 100 hours of audio, recorded at a rate of 1 MB per minute. How much space does that take up in GB?

Fill in each blank to complete the following sentence:

When Firefox.exe is double clicked, the _________________________
instructions inside Firefox.exe are copied to

_________________________ where the _________________________ runs them.

Which one of the following computer languages handles the most detail automatically for the programmer (circle one): machine code instructions, low-level computer language, high-level computer language

What is printed by the following code?

```z = "print";
print(z, "x");
z = "hi";
print(z, "x");
```

What exactly does the following code do to the image:

```image = new SimpleImage("something.jpg");
pixel = image.getPixel(0, 0);
pixel.setRed(255);
pixel.setGreen(255);
pixel.setBlue(0);
```

## Problem 2 (5)

Write code to make the following changes to an image. For every pixel, halve the green value, and change the blue value to be double the red value.

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

```

## Problem 3 (5)

Consider a vertical stripe 50 pixels wide running down the left side of the image. Write code to set the red value to 255 for all the pixels in the stripe.

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

```

## Problem 4 (20)

Write code to detect the green pixels of the given image using the avg technique and change those pixels to grayscale.

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

```

## Problem 5 (20)

This problem concerns a modified bluescreen computation that uses three images. As set up by the provided code, image shows a green leaf in front of a red "bluescreen", back1 shows Yosemite, and back2 shows Stanford.

Write code to detect the background parts of image as usual using the avg technique. Usually we copy the pixel's values from a single back image, but for this problem do the following: copy the red and green values from back1 and the blue value from back2.

```image = new SimpleImage("leaf.jpg");
back1 = new SimpleImage("yosemite.jpg");
back1.setSameSize(image);
back2 = new SimpleImage("stanford.jpg");
back2.setSameSize(image);

for (pixel: image) {

```

## Problem 6 (20)

For this problem, image is "fish-green.jpg" showing a fish in front of a green background, and back shows "paris.jpg". Write code to detect the fish using the avg technique, and blend those pixels into the paris image, dividing each fish pixel value by 10.

```image = new SimpleImage("fish-green.jpg");
back = new SimpleImage("paris.jpg");
back.setSameSize(image);

for (pixel: image) {

```

## Solutions

Problem 1

• 2 points apiece unless otherwise noted
• Moore's law states that the density of transistors on a chip doubles every 18-24 months.
• Example of machine instruction: add two numbers
• The only *solid state* persistent storage in the list is: flash drive
• 2 gigahertz = 2 billion operations per second
• 1 bit = 2 patterns
• 1 byte = 256 patterns
• Space arithmetic problems: 3.2 MB, 160 MB, 6 GB
• When Firefox.exe is double clicked, the machine instructions inside Firefox.exe are copied to RAM where the CPU runs them. (3 points)
• High level languages handle more detail automatically
• print x (next line) hi x (3 points)
• Changes the upper left pixel to be yellow (4 points)

Problem 2

```image = new SimpleImage(“something.jpg”);
for (pixel : image){
pixel.setGreen(pixel.getGreen() / 2);
pixel.setBlue(pixel.getRed() * 2);
}

2 pts for getting setGreen statement right
3 pts for getting setBlue statement right
```

Problem 3

```image = new SimpleImage(“something.jpg”);
for (pixel : image){
if (pixel.getX() < 50) {   // <= and 49, 51 also OK
pixel.setRed(255);
}

}

4 points for getting if test right
1 point for getting setRed statement right
-1 if wrote for instead of if
-2 if used x < 50 without specifying x = pixel.getX()
```

Problem 4

```image = new SimpleImage(“something.jpg”);

for (pixel: image) {
avg = ((pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3);
if (pixel.getGreen() > avg) {
pixel.setRed(avg);
pixel.setGreen(avg);
pixel.setBlue(avg);
}
}

1 point for calculating the average
8 points for the if statement
2 for comparing the color green
6 for comparing against the avg
2 points for setting red (to anything)
2 points for setting blue (to anything)

2 points for setting green (to anything)
5 points for setting the three colors to avg
(separate from the 6 points given above)
```

Problem 5

```image = new SimpleImage(“leaf.jpg”);
back1 = new SimpleImage(“yosemite.jpg”);
back1.setSameSize(image);
back2 = new SimpleImage(“stanford.jpg”);
back2.setSameSize(image);

for (pixel: image) {
avg = ((pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3);
if (pixel.getRed() > avg) {
x = pixel.getX();
y = pixel.getY();
pixel2 = back1.getPixel(x, y);
pixel.setRed(pixel2.getRed());
pixel.setGreen(pixel2.getGreen());
pixel3 = back2.getPixel(x, y);  // key: from back2
pixel.setBlue(pixel3.getBlue());
}
}

1 point for calculating the average
5 points for the correct single if statement
-2 points if comparison didn’t work
14 points for the body
```

Problem 6

```image = new SimpleImage("fish-green.jpg");
back = new SimpleImage("paris.jpg");
back.setSameSize(image);

for (pixel: image) {
avg = ((pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3);
if (pixel.getGreen() < avg) {
pixel2 = back.getPixel(pixel.getX(),pixel.getY());
pixel2.setGreen(pixel2.getGreen() + pixel.getGreen()/10);
pixel2.setRed(pixel2.getRed() + pixel.getRed()/10);
pixel2.setBlue(pixel2.getBlue() + pixel.getBlue()/10);
}
}

1 point for calculating the average
6 points for the if statement (4 points for the correct comparison)
2 for setting back pixel
6 for blending
3 points for setting pixel2
3 points for dividing pixel by 10
```