Exam 1 Sample Exam, Solutions

[Note: this webpage last modified Friday, 04-Feb-2011 19:44:51 EST]

Here are my solutions to the exam 1 sample exam. If you see any mistakes please let me know.

Problem 1

False. Red, green, blue values can range between 0 and 255.

Problem 2

True.

Problem 3

False. variable will be a string/text, so multiplying it by 3 does not produce 30. Actually, Python allows you to multiply a string by 3, but what it does is to put 3 copies of the original string.

Problem 4

False. The correct command would any of these:

  >>> setColor(pixel, red)

  >>> setColor(pixel, makeColor(255, 0, 0))
  
  >>> color = makeColor(255, 0, 0)
  >>> setColor(pixel, color)

  >>> setRed(pixel, 255)
  >>> setGreen(pixel, 0)
  >>> setBlue(pixel, 0)

Problem 5

(A) is correct. (B) is missing the : on the function definition and has a mixup between pixel in the for loop line and p on the other lines. (C) is missing the / 3 to make the average. (D) has * 3 instead of / 3.

Problem 6

(B) is correct.

Problem 7

(A) is correct. (B) is not calling getWidth and getHeight correctly. (C) is just giving the width. (D) is dividing instead of multiplying the width and height.

Problem 8

(B) is correct, making myPic4 a picture with 4 copies of the original, then myPic16 has 4 copies of that - so 16 copies of the original.

Problem 9

For this problem, if you remembered you could just give the JES code and that would be fine. But all you need to say is something like the following. I would make a for loop that looks at each pixel in the picture. For each pixel, I would compute the brightness of the pixel. If the brightness is greater than a certain amount, say 100, I would set the pixel to be white. if it is less than that amount, I would set the pixel to be black.

Problem 10

This problem is a bit trickier because it is something you have never thought of before. I think it is fair to ask about one question like this on a test, and I understand that not everyone would figure it out. Just try not to panic, and do your best.

I would make for loops where one ranges over the x values (0 to width), and the other ranges over the y values (0 to height). I would set the color of the pixel depending on the y value. I would want the pixels on the top to be white and the pixels on the bottom to be black. One way to do this would be to set each of the red, green, and blue values of a pixel at position (x, y) to be equal to:
(255 * (height - y)) / height.

You would get most of the credit if you explained everything as I have done even if you did not come up with the exact correct formula like I have.

Problem 11

Here is my function for this.

  def lightenBlue(pic, value):
    for pixel in getPixels(pic):
      setBlue(pixel, getBlue(pixel)*value)

Another version of the function that would work fine would be this.

  def lightenBlue(pic, value):
    for pixel in getPixels(pic):
      b = getBlue(pixel)
      setBlue(pixel, b*value)

Problem 12

Here is my function for this.

  def changeBlueToBlack(pic):
    for pixel in getPixels(pic):
      b = getBlue(pixel)
      if (b == 255):
        setColor(pixel, black)

Another version of the function that would work fine would be the following. This is because since we have assumed a pixel is solid red, green, or blue, if it is blue, then just setting the blue to be 0 will make it black.

  def changeBlueToBlack(pic):
    for pixel in getPixels(pic):
      b = getBlue(pixel)
      if (b == 255):
        setBlue(pixel, 0)