CS101 2012 Midterm Solution

< CS101

This is the CS101 midterm from 2012 (with solutions). That year covered similar but not identical material to this year. Note that I re-used most the short-answer questions at the start of this exam as our homework-4.

Short Answer

a. True or false - the color (r:50, g:50, b:50) is darker than the color (r:25, g:25, b:25):

b. What exactly does the following code do to the image:

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

c. Number of bits in a byte:

d. For each row, write the number of distinct patterns possible for the given number of bits (do not write the actual patterns, just the number of patterns)

e. A hard drive records 1's and 0's on a spinning platter as what physical phenomenon:

f. You have 100 image files, each taking up 300 KB. How much space do they take up overall (include the units in your answer):

g. What is the total capacity of four 500 MB hard drives plus one 2 GB flash drive (include the units in your answer):

h. What specifically is the type of input and output of a C++ compiler (i.e. the compiler scenario from lecture):

3 points apiece unless noted otherwise

Coding Problems

Problem 1 (8 points)

Write code to make the following change to an image. For every pixel, set the red and green value to 200 and the blue value to 0.

(b) What does resulting image look like when you have done this (2 word answer): (answer yellow rectangle)

image = new SimpleImage("something.jpg");
for (pixel: image) {
  pixel.setRed(200);
  pixel.setGreen(200);
  pixel.setBlue(0);
}

Problem 2 (8 points)

Write code to make the following change to an image. For every pixel in the right half of the image, set the red value to 255.

image = new SimpleImage("something.jpg");
for (pixel: image) {
  if (pixel.getX() > image.getWidth() / 2) {
    pixel.setRed(200);
  }
}
Problem 3 (10 points)

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

image = new SimpleImage("something.jpg");
for (pixel: image) {
  pixel.setRed(pixel.getRed() * 2);
  pixel.setGreen(pixel.getBlue() * 3);
}

Problem 4 (20 points)

This problem concerns a sort of 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 blue value is greater than double the green value, copy over the corresponding pixel from the back image. Exception: For this problem, our standard definition of "copy over a pixel" is changed as follows: copy over the pixel normally, except do not copy over the blue value.

image = new SimpleImage("image.jpg");
back = new SimpleImage("back.jpg");
for (pixel: image) {
  if (pixel.getBlue() > pixel.getGreen() * 2) {
    pixel2 = back.getPixel(pixel.getX(), pixel.getY())
    pixel.setRed(pixel2.getRed());
    pixel.setGreen(pixel2.getGreen());
  }
}

Problem 5 (10 points)

Write code to make the following change to an image. For every pixel, consider the average value of its red, green, and blue values. If the average value is more than 200, set the red value to 0.

image = new SimpleImage("image.jpg");
for (pixel: image) {
  avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
  if (avg > 200) {
    pixel.setRed(0;
  }
}

Problem 6 (20 points)

Write code to make the following change to an image. For every pixel, consider the average value of its red, green, and blue values. If the red value is more than double the average value, set the red value to be the same as the average value. In all cases (i.e. regardless of the red/average relationship) set the green value to 0.

image = new SimpleImage("image.jpg");
for (pixel: image) {
  avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
  if (pixel.getRed() > avg * 2) {
    pixel.setRed(avg);
  }
  pixel.setGreen(0);
}