Top Banner
CS 100 Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each of the following pieces of code, indicate what color p would have at the end of the code if it is black when the code starts. a. redness = getRed (p) if redness < 50 : setColor (p, green) if redness < 100 : setColor (p, pink) if redness < 150 : setColor (p, orange) b. redness = getRed (p) if redness < 50 : setColor (p, green) elif redness < 100 : setColor (p, pink) elif redness < 150 : setColor (p, orange) c. redness = getRed (p) if redness < 50 : setColor (p, green) if redness < 100 : setColor (p, pink) else : setColor (p, orange) d. redness = getRed (p) if redness > 50 : setColor (p, green) elif redness > 100 : setColor (p, pink) elif redness > 150 : setColor (p, orange) e. redness = getRed (p) for x in range (redness) : setColor (p, green) 1
9

CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

Jun 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Introduction to Computer Science Final Sample Questions, Fall 2015

1. For each of the following pieces of code, indicate what color p would have at the end of the code if it is black when the code starts.

a. redness = getRed (p) if redness < 50 : setColor (p, green) if redness < 100 : setColor (p, pink) if redness < 150 : setColor (p, orange)

b. redness = getRed (p) if redness < 50 : setColor (p, green) elif redness < 100 : setColor (p, pink) elif redness < 150 : setColor (p, orange)

c. redness = getRed (p) if redness < 50 : setColor (p, green) if redness < 100 : setColor (p, pink) else : setColor (p, orange)

d. redness = getRed (p) if redness > 50 : setColor (p, green) elif redness > 100 : setColor (p, pink) elif redness > 150 : setColor (p, orange)

e. redness = getRed (p) for x in range (redness) : setColor (p, green)

!1

Page 2: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

2. Here are 3 attempts to copy a sound into a new sound and play the new sound as well as view its sound wave. Which of these work and which do not? For the ones that do not work, briefly explain what the problem is. You should not assume that exactly one of the attempts works. You can assume that the file being loaded for the original sound contains a sound and that the computer audio output is working properly. a. def copySound1 (fromSound, toSound) :

for index in range (getLength (fromSound)) : sample = getSampleValueAt (fromSound, index) setSampleValueAt (toSound, index, sample)

def testCopySound1 () :sound1 = makeSound ("/Users/blerner/Documents/cs100/mediasources/a440.wav")sound2 = makeEmptySound (getLength(sound1))copySound1 (sound1, sound2)explore(sound2)

b. def copySound2 (fromSound, toSound) : toSound = makeEmptySound (getLength (fromSound)) for index in range (getLength (fromSound)) : sample = getSampleValueAt (fromSound, index) setSampleValueAt (toSound, index, sample) def testCopySound12 () :

sound1 = makeSound ("/Users/blerner/Documents/cs100/mediasources/a440.wav")sound2 = makeEmptySound (getLength(sound1))copySound2 (sound1, sound2)explore(sound2)

c. def copySound3 (fromSound) : toSound = makeEmptySound (getLength (fromSound)) for index in range (getLength (fromSound)) : sample = getSampleValueAt (fromSound, index) setSampleValueAt (toSound, index, sample) return toSound def testCopySound13 () :

sound1 = makeSound ("/Users/blerner/Documents/cs100/mediasources/a440.wav")sound2 = copySound3 (sound1)explore(sound2)

!2

Page 3: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

3. We want to build a sound as a square wave. The sound has a sampling rate of 2,000 samples per second. We want the square wave to a have a frequency of 500 cycles per second, and an amplitude of 42 (it is a very quiet sound). a. What are the values of the first ten samples in the sound?

4. Consider the following function def f1(picture1, picture2):

for y in range(getHeight(picture1)): for x in range(getWidth(picture1)): pixel1 = getPixel(picture1, x, y) pixel2 = getPixel(picture2, x, y) if getRed(pixel2) == 0 and getGreen(pixel2) == 0 and getBlue(pixel2) == 0:

setColor(pixel2, getColor(pixel1))

Given the two following images, draw what the two images look like after calling f1(pic1, pic2). Your drawing can be rough, and if one of the images hasn’t changed, you can just say so.

!3

pic2pic1

Page 4: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

5. What output do the following functions produce when called? a. def f2():

for x in range(2): for y in range(3): print x,y print x,y

b. def f3():

for x in range(2): print x for y in range(3): print x,y print x,y

6. For each of the following pieces of code, indicate what gets printed.

a. num = 0 if num < 50 : print “small” if num < 100 : print “medium” if num < 150 : print “large”

b. num = 0 if num < 50 : print “small” elif num < 100 : print “medium” elif num < 150 : print “large”

c. num = 0 if num < 50 : print “small” if num < 100 : print “medium” else : print “large”

d. num = 0 if num > 50 : print “small” elif num > 100 : print “medium” elif num > 150 : print “large”

e. num = 0 for x in range (num) : print “hi”

!4

Page 5: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

7. Here is a function that draws horizontal lines over a picture every 10th row. def addLines ( picture ) : for row in range ( 0 , getHeight ( picture ) , 10 ) : for col in range ( getWidth ( picture ) ) : pixel = getPixelAt ( picture , col , row ) setColor ( pixel, black ) return picture

a. Show how you would change this function so that it draws vertical lines in every 10th column in addition to the horizontal lines.

b. Write a test function that uses a file chooser to allow the user to select a file, dis-plays the original image, calls the function above and then displays the modified im-age.

8. Draw the sound waves produced by these functions. a. def soundWave ( ) :

newSound = makeEmptySound ( 22050 ) samplesPerCycle = 22050 / 220 increment = 20000 / samplesPerCycle for index in range ( 220 ) : for i in range ( samplesPerCycle ) : setSampleValueAt (newSound, index * samplesPerCycle + i, -10000 + increment * i)

return newSound

b. def soundWave2 ( ) : sound = makeEmptySound ( 22050 ) sampPerCycle = 22050 / 220 value = 1000 for index in range ( 220 ) : for i in range ( sampPerCycle / 2 ) : setSampleValueAt ( sound , index * sampPerCycle + i , value ) for i in range ( sampPerCycle / 2 ) : setSampleValueAt (sound, index * sampPerCycle + sampPerCycle / 2 + i, -value) value = value + value / 100 return sound

9. Briefly explain what this function does. def function1(sound, param):

sound2 = makeEmptySound(getLength(sound))for index in range(param, getLength(sound)):

value = getSampleValueAt(sound, index)setSampleValueAt(sound2, index - param, value)

return sound2

!5

Page 6: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

10.Answer the following questions about this collection of sound waves. You can assume these are all zoomed in and drawn to the same scale.

a. Which of these sound waves represents the highest pitch?

b. Which of these sound waves is the loudest?

Sound 1:

Sound 2:

!6

Page 7: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

11. For each of the following pieces of code, indicate what gets printed.

a. value = 25if value > 20 :

print "big"elif value > 10 :print "medium"else :

print "small"

b. value = 25if value > 20 :

print "big"if value > 10 :

print "medium"else :

print "small"

c. value = 5if value < 20 :

print "not big"if value > 10 :

print "medium"else :

print "small"

d. value = 25if value > 20 :

print "big"value = 0 - value

if value > 10 :print "medium"

else :print "small"

e. value = 25if value > 20 :

print "big"value = 0 - value

elif value > 10 :print "medium"

else :print "small"

!7

Page 8: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

12.The following function does not work correctly. Please fix it.

def buggyFlip ( picture ) : width = getWidth ( picture ) for x in range ( width ) : for y in range ( getHeight ( picture ) ) : pixel1 = getPixel ( picture , x , y ) color = getColor ( pixel1 ) flippedPixel = getPixel ( picture , width - x - 1 , y ) setColor ( flippedPixel, color )

13.One of the functions we developed for sounds was normalize, which stretched the sound to be as loud as possible, thus making full use of the available range. I would like you to contemplate a function that performs a similar function for grayscale images.

a. What would a function like this do to the image?

b. Why would we want something like this?

14.What output does each of the following produce?

a. age = 20print "age"

b. print 11 / 2

c. for num in range (3) : print num

d. def fun (param) : print paramdef testFun () : param = 10 fun (3)testFun()

e. # Assume pixel is properly set to a pixel in an imagesetColor (pixel, makeColor (200, 150, 100)) print getRed (getColor (pixel))

Original What we want to happen What actually happens

!8

Page 9: CS 100 Introduction to Computer Science Final Sample ...blerner/cs100/final/FinalSampleQuestio… · Introduction to Computer Science Final Sample Questions, Fall 2015 1. For each

CS 100 Sample final questions

15.True / false. If you answer false, explain what you think is false about the statement.

a. Chromakey is a method to turn an image into music by playing different notes for each pixel in the image.

b. The frequency of a sound determines its pitch.

c. To capture a sound that has a maximum frequency of 20,000 cycles per second re-quires at least 100,000 samples per second.

d. Normalizing a sound means to change its frequency to be between 4000 and 8000 cycles per second.

e. “123” + “123” evaluates to “123123” in Python.

f. A pixel is the smallest unit in a picture and has a single color.

g. RGB stands for Red-Gray-Black.

h. An if-statement should always have an else part.

i. 1.0 / 2.0 and 1 /2 produce the same result.

j. An RGB color where all three components have the same value is a shade of gray.

k. In a nested for loop, the inner for loop is executed multiple times for each execution of the outer for loop.

l. The purpose of a return statement is to communicate information from a function to the place where the function was called.

m. Sound waves that represent speech are less regular than sound waves that represent musical notes.

n. Sample values in a sound are always greater than or equal to 0.

o. The components of an RGB color are always greater than or equal to 0.

p. The order in which parameters are passed in a function call is important.

q. When we enlarge an image by doubling its width and doubling its height, the en-larged image has 4 times as many pixels as the original image.

!9