CS101 Midterm

< CS101

CS101 Midterm with Solutions


The median was 94, so people did really well. I'll curve our course grades somewhat, but with people doing this well, I'll also be more generous with the letter grades. For grading problems, please give your exam to Nick or a TA for a regrade. I would say a score below 70 is worrying and a score below 55 is at risk of not passing this course. Last chance: for people who do much better on the final than the midterm, we will count the midterm less in their final grade. The final will be cumulative, combining all the midterm material plus the post-midterm material.

Stanford CS101 midterm, Spring 2014-2015

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 (100 pts):
1. ____ / 20
2. ____ / 10
3. ____ / 10
4. ____ / 15
5. ____ / 25
6. ____ / 20
   _____ / 100

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.setAsBig(other_image)
 

Problem 1 Short Answer (20 points)

a. What is printed by the following code:

alice = "meh";
print(alice, "and", alice);

 

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

c. How many distinct patterns can be stored with 8 bits:

d. Using the 24-month version of Moore's law, in 8 years chip capacity increases by:

e. What is the name of the type of code that runs directly on the CPU, without requiring translation:

f. What is the name of the supervisory system on a computer that starts and manages programs running on the computer:

g. What is the total capacity of one 2 GB flash drive plus ten 500 MB hard drives, in GB:

h. Suppose there are about 100,000 people associated with Stanford. How much space is required to store a 100 KB jpg of each person, in GB:

i. Suppose we have a 400 mbps LAN. How many megabytes per second can it carry, ignoring all overhead:

j. On an ethernet LAN, what does a rogue computer do to intercept packets intended for someone else on the LAN (one sentence):

Problem 2 (10 points)

This is like a 5-10-20 puzzle, but with the numbers 2-3-4. Write code to change the image by multiplying the red values by 2, the green values by 3, and the blue values by 4.

image = new SimpleImage("something.jpg");
for (pixel: image) {
  // your code here















Problem 3 (10 points)

a. Suppose the donut.jpg image is 500 pixels wide by 300 pixels high. Write code to make a pure-white, horizontal stripe on the bottom half of the image. It's fine to use literal numbers in your code to identify the bottom half (as we did in lecture), or any other technique which gets the correct result.

image = new SimpleImage("donut.jpg");
for (pixel: image) {
  // your code here















b. Suppose we wanted to make a stripe in the top half instead of the bottom half. Briefly, what change to the (a) code above will make a stripe in the top half?

Problem 4 (15 points)

The blue-pool.jpg image shows a blue pool of water surrounded by red sand. Write bluescreen code to detect the red sand pixels and replace them with pixels from leaves.jpg. Rather than the avg technique, for this problem assume that any pixel with red over 100 is the red sand.

image = new SimpleImage("blue-pool.jpg");
back = new SimpleImage("leaves.jpg");
back.setAsBig(image);

for (pixel: image) {
  // your code here



Problem 5 (25 points)

a. The green-flower.jpg image shows a green flower in front a blue background. Write code to detect the flower pixels with the avg technique, looking for green-high. Change the back yosemite image by blending only the green values of the flower (ignoring the red and blue values) into the back image green values, dividing by 10.

image = new SimpleImage("green-flower.jpg");
back = new SimpleImage("yosemite.jpg");
back.setAsBig(image);

for (pixel: image) {
  // your code here
























b. Assume your (a) code above works correctly. The (a) code should use a green-high strategy to detect the flower pixels. Write below the single line of if-test code which, if substituted into the (a) code, would implement the blue-low strategy to detect the flower pixels.

Problem 6 (20 points)

The red-apple.jpg image shows a red apple in front of a campus scene of green grass and blue sky. Write code to detect the apple using the avg technique. Change the apple to grayscale, except make the apple dark gray by setting the red/green/blue values to half what they would be for regular grayscale.

image = new SimpleImage("red-apple.jpg");

for (pixel: image) {
  // your code here

Solutions

Problem 1.
a. meh and meh
b. 8
c. 256 (some wrote this as 2 to the 8th power)
d. 16
e. machine code
f. operating system
g. 7 GB
h. 10 GB
i. 50 MB per second
j. Don't discard packets addressed to others on the LAN

Problem 2.
for (pixel: image) {
  pixel.setRed(pixel.getRed() * 2);
  pixel.setGreen(pixel.getGreen() * 3);
  pixel.setBlue(pixel.getBlue() * 4);
}
-1 for using an incorrect constant to multiply a color.


Problem 3.
a.
for (pixel: image) {
  if (pixel.getY() > 149) {
    pixel.setRed(255);
    pixel.setGreen(255);
    pixel.setBlue(255);
  }
}

-4 for totally missing if-statement.
-3 for using incorrect comparison operator (< instead of >)
-2 for not setting the pixel to white (255, 255, 255)
-2 for not setting the correct boundary in conditional. e.g. pixel.getY() < 300)
-0 149 vs. 150

b.
-5 for not flipping the comparison operator used in a.


Problem 4.
if (pixel.getRed() > 100) {
  //get pixel2 from back
  pixel2 = back.getPixel(pixel.getX(), pixel.getY());

  //copy rgb values from pixel2
  pixel.setRed(pixel2.getRed());
  pixel.setGreen(pixel2.getGreen());
  pixel.setBlue(pixel2.getBlue());
}

-5 missing or incorrect if statement
-5 not setting pixel2, or getting pixel value from "back"
-5 not setting pixel's RGB values to those of pixel2


Problem 5.
a. Out of 20 points
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);
}
-3 not taking average
-6 not detecting high-green in the if conditional
-4 not getting the pixel from the back image
-7 no blending code
-4 changing color of the wrong pixel (i.e. pixel instead of pixel2)
-3 dividing the wrong pixel's green color by 10
-2 changing red and/or blue values
-2 not dividing the average by 3
-4 missing the green value of pixel in the blending
-3 missing the green value of pixel2 in the blending

b. Out of 5 points
if (pixel.getBlue() < avg)
-4 incorrect color
-4 wrong direction of sign
-2 using setBlue instead of getBlue


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

-5 if grayscaled but did not grayscale using half of the average
-3 for incorrect avg calculation
-10 missing if-statement
-10 if no grayscaling was performed
-8 if some semblance of grayscaling was done
-8 if average was not used in attempt to grayscale. e.g. pixel.setRed(pixel.getRed() / 2)
-5 if grayscaling used avg/2 but the value was not passed into the function calls correctly
-2 if used avg*2 instead of avg/2
-3 if no avg declaration