CS101 Final Exam (Solutions)

< CS101

Stanford CS101 Final Exam, 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:
SA   ____ / 30

IM 1 ____ / 5
IM 2 ____ / 10
IM 3 ____ / 8
IM 4 ____ / 10
IM 5 ____ / 14

TA 1 ____ / 5
TA 2 ____ / 8
TA 3 ____ / 10
      _____ / 100

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

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

Short-Answer (30)

a. What happens to the bytes stored in RAM when its electrical power is turned off?

b. About how many bytes does the string "hello" take up?

c. 6 bits can store 64 patterns. How many patterns can 7 bits store?

d. Suppose we have 2000 people, each with a 2 MB image file. How many GB do all the images take up?

e. Suppose we are networking with a 100 megabits/second connection. About how long does it take to send a 40 MB file, ignoring all overhead?

f. What mechanism allows a network to detect packet corruption (one word)?

g. Below is a line of HTML code. Draw what this HTML looks like in the browser.
Check out <a href="http://kitten.org/cool.mp4">this video</a>

h. HTTP is based on a request and a response. What computer makes the request, and what computer makes the response?

i. Define "lossy compression" without using the word "lossy"

j. An analog signal is brought into the computer as a series of numbers. What do these numbers represent?

k. What are 3 different ways a bad guy could steal your password to a web site?

l. As outlined in lecture, what key is stored in the chip of an EMV card, and what is that key used for?

m. Circle the most secure of the following passwords:
kitten password kittex ki2ttex kitten1 password1 qwerty

n. How does "ransomware" make money for the bad guys?

For the last questions, you have the following spreadsheet with raw data.
spreadsheet cell picture

o. What exactly goes in the cell B7 to add up the Blue Castle numbers?

p. Suppose the "Mystery Number" is supposed to be the B7 value multiplied by 5. What exactly goes in the B9 cell to compute this?

Image-1 (5)

Write code to change the image as follows: for every pixel, double the red value and set the green value to be half the blue value.

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



Image-2 (10)

We have an image of a red bird in front of a blue ocean background, 500 pixels wide by 300 pixels high. We have a "back" image that is the same size, showing lots of donuts. (Several problems use these two images.)

For a stripe 10 pixels high across the top of both images, copy a grayscale version of the pixels from the donut image to the bird image.

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



Image-3 (8)

We have an image of a red bird in front of a blue ocean background, 500 pixels wide by 300 pixels high. We have a "back" image that is the same size, showing lots of donuts.

Write bluescreen code to detect the red bird, and replace the bird pixels with donut pixels. Rather than use the avg technique, we'll say that the bird is any pixel where the red value is over 200.

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



Image-4 (10)

We have an image of a red bird in front of a blue ocean background, 500 pixels wide by 300 pixels high. We have a "back" image that is the same size, showing lots of donuts.

Write bluescreen code to change the back image as follows: detect the non-bird pixels in the bird image using the red values and copy those pixels to the donut image. Rather than use the avg technique, we'll say that the bird is any pixel where the red value is over 200.

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



Image-5 (14)

We have the monkey.jpg image from lecture showing a brown monkey in front of a blue background, and a grass-field.jpg image that's the same size.

Change the grass-field image by detecting the monkey pixels and blending them into the grass-field image, dividing the monkey values by 10. Detect the monkey with a red-high avg technique.

image = new SimpleImage("monkey.jpg");
back = new SimpleImage("grass-field.jpg");
for (pixel: image) {
  // your code here




















(b) Here we will vary what is done with the monkey pixels: for each monkey pixel detected, change the pixel in the monkey image to be an even blend of the original monkey pixel divided by 2, and grass-field pixel divided by 2. Write below only the lines which need to change from your (a) solution above.

Table-1 (5)

For this question we'll use the baby fields "name" and "rank". Write code to print all the rows where the name starts with "Z" and the rank is over 100.

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


for (row: table) {

Table-2 (8)

For this question we'll use the baby field "name". Write code to count the number of baby names that start with "A" and do not end in "y" and print the total count in the form "count: 42".

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


for (row: table) {

Table-3 (10)

This question uses the survey fields "sport" and "gender" ("male" vs. "female"). What is the gender breakdown of people who identified "soccer" as their favorite sport? Write code to count and print the number of males and females who like soccer:

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


for (row: table) {


Solutions

a. The bytes disappear (RAM is not persistent)
b. About 5 (one per letter)
c. 128 (doubles with each bit)
d. 4000 MB = 4 GB
e. 40 MB file * 8 = 320 megabits, so 3.2 seconds
f. Checksum
g. Check out this video (this video may be underlined)
h. Computer running the browser makes the request, web server makes the response
i. Compression that discards some detail
j. "height"/"amplitude" of signal vs zero line
k. Phishing, dictionary attack (guessing), malware/keylogger, password used at another site the bad guy broke into
l. Secret key in the chip is used to make an encrypted blob to send back to the issuer
m. ki2ttex (not a word, combined with digit not-at-end)
n. It encrypts the victim's files, requires payment for the key to decrypt them
o. =sum(B2:B6)
p. =B7 * 5


Image-1
  pixel.setRed(pixel.getRed() * 2);
  pixel.stGreen(pixel.getBlue() / 2);

Image-2
Key ideas:
-copying from back to image
-grayscale uses the avg of a pixel

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


Image-3
This is a standard bluescreen computation
  if (pixel.getRed() > 200) {  // bird
    pixel2 = back.getPixel(pixel.getX(), pixel.getY();
    pixel.setRed(pixel2.getRed());
    pixel.setGreen(pixel2.getGreen());
    pixel.setBlue(pixel2.getBlue());
  }


Image-4
This is a variation of the previous problem
  if (pixel.getRed() < 200) {  // not bird
    pixel2 = back.getPixel(pixel.getX(), pixel.getY();
    // copy TO the back image
    pixel2.setRed(pixel.getRed());
    pixel2.setGreen(pixel.getGreen());
    pixel2.setBlue(pixel.getBlue());
  }

Image-5
a.
  avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
  if (pixel.getRed() > avg) {  // monkey
    pixel2 = back.getPixel(pixel.getX(), pixel.getY();
    // blend monkey into back
    pixel2.setRed(pixel2.getRed() + pixel.getRed()/10);
    pixel2.setGreen(pixel2.getGreen() + pixel.getGreen()/10);
    pixel2.setBlue(pixel2.getBlue() + pixel.getBlue()/10);
  }


b.
  // if is the same
  // change the monkey pixel to an even blend of the 2
    pixel.setRed(pixel.getRed()/2 + pixel2.getRed()/2);
    pixel.setGreen(pixel.getGreen()/2 + pixel2.getGreen()/2);
    pixel.setBlue(pixel.getBlue()/2 + pixel2.getBlue()/2);


Table-1
  if (row.getField("name").startsWith("Z") &&
      row.getField("rank") > 100) {
    print(row);
  }

Table-2
Table counting: initialize variable outside the loop,
count inside the loop, print result at the end
  count = 0
  for (row: count) {
    if (row.getField("name").startsWith("A") &&
        !row.getField("name").endsWith("y")) {  // !
      count = count + 1;
    }
  }
  print("count:", count);

Table-3
  count1 = 0;
  count2 = 0;
  for (row: count) {
    if (row.getField("sport") == "soccer" &&
        row.getField("gender") == "male") {
      count1 = count1 + 1;
    }
    if (row.getField("sport") == "soccer" &&
        row.getField("gender") == "female") {
      count2 = count2 + 1;
    }
  }
  print("male:", count1);
  print("female:", count2);