Computer Science 101
Survey of Computer Science
Exam 2 Fall, 2010
Name: Pledged:
- (25 points) Complete the following:
- In Python, assume that we have already executed the statement
scores = [85,90,75,80,94,65,84]
Give the value of len(scores): 7
Give the value of scores[3]: 80 - Assuming that we are using the random library of Python, circle the ones of the following that would not be possible values obtained from randrange(10,20):
5 10 1520 25 - What would be printed if we executed the statement
print range(3,7)
in Python? [3,4,5,6] - What is the largest number that we can express using 5-bit unsigned numbers?
Give answer in decimal: 31 - What is the largest positive number that we can express using 5-bit 2’s
complement numbers? Give answer in decimal: 15 - Here Log indicates logarithm to base 10.
Log (10,000) = 4 - Here Lg indicates logarithm to base 2.
Lg (512) = 9 - If the decibel level of a sound wave increases by 20 decibels, then the actual
pressure is multiplied by 100. - The frequency of a sound wave is the number of cycles per second. This determines the pitch of the sound – how high or how low the sound seems to us.
- One type of redundancy that can be exploited in attempting to compress a picture
Redundant information. Explain what this means: For example, large sections
of the same color would not need 24 bits for every pixel, etc.
- (5 points) In Python, assume that we have already executed the statement
scores = [85,90,75,80,94,65,84]
Using a loop (either a while-loop or a for-loop), write a section of Python code that would add 5 to each of the values in the list scores.
for num in scores:
num = num+ 5
OR:
index = 0
while index < len(scores):
scores[index] = scores[index] + 5
index = index + 1 - (20 points) Perform the following conversions.
- Express the decimal number 156 as a 10-bit 2’s complement number.
156 = 128 + 16 + 8 + 4; so 0010011100 - Express the decimal number -156 as a 10-bit 2’s complement number.
Flip bits and add 1: 1101100011 + 1 = 1101100100 - Express the 5-bit 2’s complement number 01110 as a decimal number.
8 + 4 + 2 = 14 - Express the 5-bit 2’s complement number 10010 as a decimal number.
-X : 01101 + 1 = 01110 or 14; so X = -14
- (25 points) Assume that, in JES, we have successfully executed the command
myPicture = makePicture(“FallColonnade.jpg”)
For each of the following, write one or more statements to achieve the task. Parts a. through d. refer to the picture object called myPicture. - Print out the red component of the pixel at location (32, 200).
thePixel = getPixel(myPicture, 32,200)
redAmt = getRed(thePixel)
print redAmt - Change the red component of the pixel at (32,200) to be 0, leaving the green and blue components alone.
thePixel = getPixel(myPicture, 32,200)
setRed(thePixel,0) - Print out the red component of the pixel in the upper left corner of the picture.
thePixel = getPixel(myPicture, 0, 0)
redAmt = getRed(thePixel)
print redAmt - Print out the red component of the pixel in the upper right corner of the picture.
width = getWidth(myPicture)
thePixel = getPixel(myPicture, width-1, 0)
redAmt = getRed(thePixel)
print redAmt - Complete the code for the following function:
# This function sets the value of the red component of all of the pixels of
#thePicture to zero, leaving the green and blue values unchanged.
def zeroRed(thePicture) :
for thePixel in getPixels(thePicture):
setRed(thePixel,0)
- (25 points) Assume that, in JES, we have executed the command
mySound = makeSound(“preamble.wav”)
For each of the following, write one or more statements to achieve the task. The parts a. through d. refer to the sound object called mySound. - Print out the sample value of the first sample in mySound.
value = getSampleValueAt(mySound,0)
print value
- Set the first sample value of mySound to be 0.
setSampleValueAt(mySound,0,0) - Set the last sample value of mySound to be 0.
length = getLength(mySound)
setSampleValueAt(mySound,length-1,0) - Set all sample values of mySound to be 0.
length = getLength(mySound)
for index in range(length) :
setSampleValueAt(mySound,index, 0) - Complete the code for the following function:
# This function sets the value of all samples of theSound in the section starting at
#location phraseBegin and ending at location phraseEnd to 0. The other
#samples are left unchanged.
def erasePhrase(theSound, phraseBegin, phraseEnd) :
for index in range(phraseBegin, phraseEnd+1) :
setSampleValueAt(theSound,index,0)