# Exam Questions for Chapters 14 and 15¶

The following questions test what you have learned in chapters 14 and 15. Click the “Start” button when you are ready to begin the exam. Click the “Pause” button to pause the exam (you will not be able to see the questions when the exam is paused). It will show how much time you have used, but you have unlimited time. Click on the “Finish Exam” button at the end when you are done. The number correct, number wrong, and number skipped will be displayed at the bottom of the page. Feedback for each answer will also be shown as well as your answer.

You will not be able to change your answers after you hit the “Finish Exam” button.

15-7-1: Which of the following expressions gives you 3 possible results for all values of y?
• y % 1
• Since every value is evenly divisible by 1 this will always return 0.
• y % 2
• This will return two possible values 0 if even and 1 if odd.
• y % 3
• This will return 0, 1, or 2.
• y % 4
• This will return 0, 1, 2, or 3.
15-7-2: Which of the following statements actually changes the image?
• newPixel = image.Pixel(r, g, b)
• This creates a new pixel with the given red, green, and blue values.
• img.setPixel(x, y, newPixel)
• This updates the pixel values in the image at x and y with the colors in the pixel.
• p = img.getPixel(x, y)
• This gets the pixel at the given x and y location.
• win = ImageWin(img.getWidth(), img.getHeight())
• This creates a window that can be used to display the image.

15-7-3: What does the following code do to the image?

from image import *

# CREATE AN IMAGE FROM A FILE
img = Image("arch.jpg")

# LOOP THROUGH ALL PIXELS
for x in range(img.getWidth()):
for y in range(img.getHeight()):
p = img.getPixel(x, y)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()

# VALUES FOR THE NEW COLOR
if r < 120:
r = 0
elif r < 240:
r = 200
else:
r = 255

# CREATE THE COLOR
newPixel = image.Pixel(r,g,b)

# CHANGE THE IMAGE
img.setPixel(x, y, newPixel)

win = ImageWin()
img.draw(win)

• The program removes all the red from the image
• This would be true if the red was always set to 0.
• The program changes all the red pixels to a single color
• This would be true if the red was always set to the same value.
• The program changes all the pixels to have some red
• If r is less than 120 the red is removed (set to 0).
• The program changes the image to only have 3 values of red
• The red will be sets to 0, 200, or 255.

15-7-4: What does the following code draw?

from turtle import *
space = Screen()
height = space.window_height()
maxY = height / 2
sue = Turtle()
sue.pensize(10)
sue.left(90)

for index in range(5):
sue.penup()
if index % 2 == 0:
sue.color('red')
else:
sue.color('black')
sue.goto(index * 10, -1 * maxY)
sue.pendown()
sue.forward(height)

• Vertical stripes that alternate between red and black and start with red.
• Sue turns left 90 so the stipes are vertical. The first element in range(5) is 0 so the stripes start with red.
• Vertical stripes that alternate between black and red and start with black.
• This would be true if the color was set to black when index is even and red when index is odd.
• Horizontal stripes that alternate between red and black and start with red.
• This would be true if sue didn't turn left 90 degrees at the start.
• Horizontal stripes that alternate between black and red and start with black.
• This would be true if sue didn't turn left 90 degrees at the start and if the color was set to black when the index is even and red when it is odd.
15-7-5: What does random.randrange(10,20) return?
• A random value between 10 and 20
• This would be true if it was random.randrange(10,21)
• A random value between 11 and 19
• This would be true if it was random.randrange(11,20)
• A random value between 11 and 20
• This would be true if it was random.randrange(11,21)
• A random value between 10 and 19
• The randrange function returns a random value between the first parameter value and one less than the second parameter value.
Next Section - Chapter 16 - Working with Collections