cs 100 introduction to computer science midterm sample ...blerner/cs100/midterm/sample...cs 100...

9
CS 100 Introduction to Computer Science Midterm Sample Solutions The answers below are shown in blue. 1. Convert the following numbers from binary to decimal. a. 1110 8 + 4 + 2 = 14 b. 1010 8 + 2 = 10 c. 1110 0000 128 + 64 + 32 = 224 2. Convert the following numbers from decimal to binary. a. 39 39 = 32 + 7 = 32 + 4 + 3 = 32 + 4 + 2 + 1 = 10 0111 b. 100 100 = 64 + 36 = 64 + 32 + 4 = 110 0100 3. Below is the function to copy a picture to a new canvas with a left and a top margin that we saw in class. Show how you would change this function so that the top and bottom margin could be different from the left and right margins. def copyPicture ( picture , margin ) : def copyPicture ( picture, leftRightMargin, topBottomMargin ) : width = getWidth ( picture ) height = getHeight ( picture ) canvas = makeEmptyPicture ( width + margin * 2 , height + margin * 2 ) canvas = makeEmptyPicture ( width + leftRightMargin * 2 , height + topBottomMargin * 2 ) for y in range ( height ) : for x in range ( width ) : originalPixel = getPixel ( picture , x , y ) color = getColor ( originalPixel ) copyPixel = getPixel ( canvas , x + margin , y + margin ) copyPixel = getPixel ( canvas , x + leftRightMargin , y + topBottomMargin ) setColor ( copyPixel , color ) # This is indented to the same level as the “for y” line. return ( canvas ) page of 1 9

Upload: others

Post on 24-Jan-2021

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Introduction to Computer Science Midterm Sample Solutions The answers below are shown in blue.

1. Convert the following numbers from binary to decimal. a. 1110

8 + 4 + 2 = 14 b. 1010

8 + 2 = 10 c. 1110 0000

128 + 64 + 32 = 224 2. Convert the following numbers from decimal to binary.

a. 39

39 = 32 + 7 = 32 + 4 + 3 = 32 + 4 + 2 + 1 = 10 0111 b. 100

100 = 64 + 36 = 64 + 32 + 4 = 110 0100 3. Below is the function to copy a picture to a new canvas with a left and a top margin that

we saw in class. Show how you would change this function so that the top and bottom margin could be different from the left and right margins. def copyPicture ( picture , margin ) :def copyPicture ( picture, leftRightMargin, topBottomMargin ) :

width = getWidth ( picture ) height = getHeight ( picture )

canvas = makeEmptyPicture ( width + margin * 2 , height + margin * 2 ) canvas = makeEmptyPicture ( width + leftRightMargin * 2 , height + topBottomMargin * 2 )

for y in range ( height ) : for x in range ( width ) : originalPixel = getPixel ( picture , x , y ) color = getColor ( originalPixel )

copyPixel = getPixel ( canvas , x + margin , y + margin ) copyPixel = getPixel ( canvas , x + leftRightMargin , y + topBottomMargin )

setColor ( copyPixel , color ) # This is indented to the same level as the “for y” line. return ( canvas )

page ! of !1 9

Page 2: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

4. Here is a function that modifies a picture to keep only the redness in the pixels. def justRed ( picture ) : for pixel in getPixels ( picture ) : setGreen ( pixel , 0 ) setBlue ( pixel , 0 )

a. (10 points) Show how you would change this function so that it makes the changes on a new canvas created inside the function and sent back to the caller. The original picture should not be changed.

def justRed2 ( picture ) : canvas = makeEmptyPicture ( getWidth(picture) , getHeight(picture) ) for row in range (getHeight(picture)) : for col in range (getWidth(picture)) : pictPixel = getPixel (picture, col, row) canvasPixel = getPixel ( canvas, col, row) setGreen ( canvasPixel , 0 ) setBlue ( canvasPixel , 0 ) setRed ( canvasPixel, getRed (pictPixel) ) return canvas

b. (5 points) Write a test function that lets the user select a file with a file browser, calls the function you wrote in part a and displays the resulting picture to the user. def testJustRed () : pict = makePicture ( pickAFile()) newPict = justRed2 (pict) show (newPict)

page ! of !2 9

Page 3: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

5. For the function below, show what the function computes by filling in the grid on the right using the grid on the left as the original picture. def mystery ( picture ) : width = getWidth ( picture ) height = getHeight ( picture )

canvas = makeEmptyPicture ( width , height )

for y in range ( height ) : for x in range ( width / 2 ) : originalPixel = getPixel ( picture , x , y )

color = getColor ( originalPixel )

copyPixel = getPixel ( canvas , x , y ) setColor ( copyPixel , color ) copyPixel2 = getPixel ( canvas , width / 2 + x , y )

setColor ( copyPixel2 , color )

# This is indented to the same level as the “for y” line. return ( canvas )

page ! of !3 9

A B C D

E F G H

I J K L

M N O P

A B A B

E F E F

I J I J

M N M N

Page 4: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

6. For the function below, explain in English what the function displays on the screen when it is done.

def mystery2 ( picture ) : width = getWidth ( picture ) height = getHeight ( picture )

redCanvas = makeEmptyPicture ( width, height ) greenCanvas = makeEmptyPicture ( width, height ) blueCanvas = makeEmptyPicture ( width, height ) for y in range ( height ) : for x in range ( width ) : originalPixel = getPixel ( picture , x , y ) redness = getRed ( originalPixel ) greenness = getGreen ( originalPixel ) blueness = getBlue ( originalPixel ) redPixel = getPixel ( redCanvas , x , y ) greenPixel = getPixel ( greenCanvas , x , y ) bluePixel = getPixel ( blueCanvas , x , y )

setColor ( redPixel , makeColor (redness , 0 , 0 ) ) setColor ( greenPixel , makeColor (0 , greenness , 0 ) ) setColor ( bluePixel , makeColor (0 , 0 , blueness ) ) # These are indented to the same level as the “for y” line. show ( redCanvas ) show ( greenCanvas ) show ( blueCanvas )

It shows 3 canvases, one contains just the red values from the original image, one just the green values and one just the blue values.

page ! of !4 9

Page 5: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

7. Briefly describe what each of these functions does. a. def mystery1 () :

canvas = makeEmptyPicture (100, 100) for row in range ( getHeight ( canvas ) ) : for col in range ( row ) : pixel = getPixel ( canvas , col , row ) setColor ( pixel , makeColor ( 255 , 0 , 0 ) ) # show lines up with the "for row" line show ( canvas )

This creates a new picture that is 100 pixels wide and 100 pixels tall. It draws a red triangle in the bottom left corner. It displays the draw-ing.

b. def mystery2 ( picture , newColor ) : for row in range ( 0, getHeight ( picture ) , 10 ) : for col in range ( 0, getWidth ( picture ) , 10 ) : pixel = getPixel ( picture, col, row ) setColor ( pixel , newColor )

This changes an existing picture by placing dots in every 10th row and 10th column. The color of the dots is determined by the newColor parameter. The modified picture is not displayed by the function.

8. Do these two functions do the same thing or different things? Explain your answer. def mystery3 (picture) : for pixel in getPixels ( picture ) : setRed ( pixel , 0 )

def mystery4 (picture ) : width = getWidth ( picture) height = getHeight ( picture ) for col in range ( 0 , width / 2 ) : for row in range ( 0 , height / 2 ) : setRed ( getPixel ( picture , col, row ) , 0 )

They do not do the same thing. mystery3 removes all the red from every pixel in the pic-ture. mystery4 only removes red from the top, left corner of the picture. This is because mystery3 operates over all the pixels, while mystery4 only considers pixels that are in the left half of the columns and top half of the rows.

page ! of !5 9

Page 6: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

9. The following function does not work correctly. The programmer expects a result like the picture on the left, but gets the result on the right. How can you fix this function?

def buggyChangeCorner ( picture , newColor ) :

# Visit the top half of the rows

for row in range ( getHeight ( picture ) / 2 ) :

# For each row, visit the left half of the row

for col in range ( getWidth ( picture ) / 2 ) :

# Change the color of the pixel

pixel = getPixel ( picture , col, row )

setColor ( pixel , newColor )

# This is indented to line up with the “for row” line.

redness = getRed ( pixel )

greenness = getGreen ( pixel )

blueness = getBlue ( pixel )

newColor = makeColor ( redness + 1, greenness, blueness)

page ! of !6 9

Indent these lines to line up with the “for col” line.

Desired result Buggy result

Page 7: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

10.Answer the questions below for the following function: 1. def negative ( picture ) :

2. pixels = getPixels ( picture )

3. for p in pixels :

4. # Get original RGB value of the pixel

5. redness = getRed ( p )

6. greenness = getGreen ( p )

7. blueness = getBlue ( p )

8.

9. # Negate the RGB color

10. setRed ( p , 255 - redness )

11. setGreen ( p , 255 - greenness )

12. setBlue ( p , 255 - blueness )

a. What is the name of the function defined?

negative b. On what line does redness get assigned a value?

5 c. On what line does p get assigned a value?

3 d. Is the result of this function a grayscale picture or a color picture?

color e. If a pixel is black in the original picture, what color is that pixel when the function is

complete?

white

page ! of !7 9

Page 8: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

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

a. A computer stores all data in binary.

True

b. In RGB, white is represented by the value 200, 200, 200.

False, 255, 255, 255

c. A for loop is used to execute a collection of statements multiple times.

True

d. A function defines an abstraction by giving a name to a set of instructions.

True

e. Calling the range function like this range (1, 10, 3)produces the list 1, 4, 7.

True

f. The pixel in an image at the top left corner is in row 1, column 1.

False, 0,0

g. In Python, you can indent lines any way that you want.

False, indent lines to indicate they are inside something like a function or for loop.

h. Using integer arithmetic, 1 / 3 evaluates to 0.

True

i. All functions have parameters.

False. Some functions take no parameters, like pickAFile().

j. A byte can hold any number between 0 and 255.

True.

k. A sequence of 8 bits, like 00111110, could be interpreted as a number or a letter.

True.

l. The syntax of a programming language construct specifies the meaning of the con-struct.

False, syntax are the grammar rules. Semantics are the meaning.

m. range ( 2 , 10, 2 ) returns an array containing 2, 4, 6, 8.

True.

page ! of !8 9

Page 9: CS 100 Introduction to Computer Science Midterm Sample ...blerner/cs100/midterm/Sample...CS 100 Sample Midterm Solutions 4. Here is a function that modifies a picture to keep only

CS 100 Sample Midterm Solutions

n. When we pass a parameter to a function, we must use the same variable name in the function we are calling and the calling function, like:

def func ( picture ) : …

picture = makePicture ( “/Users/blerner/snicker.jpg”)func (picture)

False. They do not need to be the same. When we call a function, we don’t even need to use a variable, we can use an expression.

o. Nested for loops can be used to change rectangular sections of a picture.

True. p. The statement “age = age + 1” increases the value that is in the age variable by 1.

True. q. Defining a function is one way to create an abstraction in programming.

True.

page ! of !9 9