cs 100 introduction to computer science midterm …blerner/cs100/midterm/... · cs 100 introduction...

6
CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from binary to decimal. a. 1110 b. 1010 c. 1110 0000 2. Convert the following numbers from decimal to binary. a. 39 b. 100 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 ) : width = getWidth ( picture ) height = getHeight ( picture ) canvas = makeEmptyPicture ( width + margin * 2 , height + margin * 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 ) setColor ( copyPixel , color ) # This is indented to the same level as the “for y” line. return ( canvas ) 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. 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. 1

Upload: vudang

Post on 26-Aug-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 100 Introduction to Computer Science Midterm …blerner/cs100/midterm/... · CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from

CS 100 Introduction to Computer Science Midterm Sample Questions

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

b. 1010

c. 1110 0000

2. Convert the following numbers from decimal to binary. a. 39

b. 100

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 ) : width = getWidth ( picture ) height = getHeight ( picture ) canvas = makeEmptyPicture ( width + margin * 2 , height + margin * 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 ) setColor ( copyPixel , color ) # This is indented to the same level as the “for y” line. return ( canvas )

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.

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.

!1

Page 2: CS 100 Introduction to Computer Science Midterm …blerner/cs100/midterm/... · CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from

CS 215 Midterm

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 )

!2

A B C D

E F G H

I J K L

M N O P

Page 3: CS 100 Introduction to Computer Science Midterm …blerner/cs100/midterm/... · CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from

CS 215 Midterm

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 )

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 )

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 )

!3

Page 4: CS 100 Introduction to Computer Science Midterm …blerner/cs100/midterm/... · CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from

CS 215 Midterm

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 )

9. (5 points) 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)

!4

Desired result Buggy result

Page 5: CS 100 Introduction to Computer Science Midterm …blerner/cs100/midterm/... · CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from

CS 215 Midterm

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 pixel5. redness = getRed ( p )6. greenness = getGreen ( p )7. blueness = getBlue ( p )8. 9. # Negate the RGB color10. setRed ( p , 255 - redness )11. setGreen ( p , 255 - greenness )12. setBlue ( p , 255 - blueness )

a. What is the name of the function defined?

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

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

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

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

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

a. A computer stores all data in binary.

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

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

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

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

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

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

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

i. All functions have parameters.

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

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

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

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

!5

Page 6: CS 100 Introduction to Computer Science Midterm …blerner/cs100/midterm/... · CS 100 Introduction to Computer Science Midterm Sample Questions 1. Convert the following numbers from

CS 215 Midterm

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)

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

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

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

!6