Download 2. [5] What is the output of the following Python program (line

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Halting problem wikipedia , lookup

Transcript
CS 140 Harcourt
Exam 1 Solutions
Fall
2010 Name: Solutions
1. [10 points] Write a Python function drawXcirc that takes three parameters; an x,y coordinate for the
center of the circle and a radius r, and draws a blue circle with a plus in the center as in the picture
below.
For example, calling drawXcirc(50,75,80) will draw the figure with the
canter at (50,75) and with a radius of 80 pixels.
def drawXcirc(x,y,r):
pygame.draw.circle(window, (0,0,255), (x,y),r)
pygame.draw.line(window, (0,0,0), (x,y-r), (x,y+r))
pygame.draw.line(window, (0,0,0), (x-r,y), (x+r,y))
pygame.display.update()
It is important that all coordinates be relative to the parameters x, y, and r. This question is very
similar to a question from the third quiz.
2. [5] What is the output of the following Python program (line numbers are on the left). Write
your answer in the box to the right of the program. There are no errors in the program, it does
print something.
1
2
3
4
5
6
7
def foo(z):
y = 3
print z, y
5
3
y = 8
z = 4
foo(5)
3. [5] From the program in question 2 name all of the arguments and the line number they appear on.
The only argument is 5 on line 7.
4. [5] From the program in question 2 name all of the parameters and the line numbers they appear on.
The parameters are the 5 on line 7 and the z on line 1.
5. [5] From the program in question 2 which lines make up the main program? Give the line numbers.
The main program is composed of lines 5, 6, and 7.
6. [5] From the program in question 2 is there a call to a function? If so which lines?
This program calls function foo on line 7.
7. [5] What is the output of the Python program below? Write your answer in the box.
for i in range(10,2,-2):
if i**2 >= 25:
print i,
10
8
6
A little tricky, because the comma at the
end of the print statement keeps all of the
numbers on the same line. I didn't take off
if you missed that.
8. [5] True/False: The name _8a is a valid variable name. Answer:
True
(why?)
9. [5] Briefly explain what a bit is.
A bit, a contraction for binary digit, is a one or a zero.
10. [5] How many bits are in a megabyte? Show how you derived your answer for partial credit.
The are eight bits in a byte and the prefix mega means million, so a megabyte contains 8 million bits.
More precisely a megabyte is 220. The python statement print 8 * 2 ** 20 would print the
number of bits in a megabyte.
11. [5] What gets printed by the following statement? Write your answer on line provided.
print 93 % 2
1
12. [5] What gets printed by the following statement? Write your answer on line provided.
print 6 % 3
0
13. [5] What would get printed by the following Python statement sequence? Write your answer in the box
to the right of the python code.
x = 7
y = 9
z = x + y / 4 * x - 2 ** 3
print z
This question was taken directly from the first quiz.
13
14. [15] Write a python program that simulates rolling a pair of six-sided dice (two die) 100 times and then
prints the number of times that two sixes were rolled.
Do for homework due Thursday September 30. Name your file exam1q14.py
This counts for both part of your homework 4 grade and some points back on your exam. You
must do this problem on your own without assistance. Turn in a printout at the beginning of
class and make sure your file is named properly.
15. [15] Write a python program that produces the image below in a 255-by-255 window. Hint: Start by
drawing lines that go from black at the top to gradually white at the bottom.
Do for homework due Thursday September 30. Name your file exam1q15.py. Hint, this program
is really pretty straightforward. You just need to draw 255 horizontal lines each one getting a
little less black (more white). Use a for-loop. This counts for both part of your homework 4
grade and some points back on your exam. You must do this problem on your own without
assistance. Turn in a printout at the beginning of class and make sure your file is named
properly.