CS 1401 Introduction to Computer Science
Fall 2012 Test 1, MW class section, October 8, 2012

Name: __________________________________________________________

1. What is the value computed by each of the following arithmetic expressions in the Python language?

10 / 8

10 − 8 / 2

10 / 8 / 2

10 / 8.0

2. For each of the following programs, trace the code and list all the points that will be colored (and by what color) by this program:

# program a
im = Raster()
x = 10
y = 8
while x < 12:
  im.set(x,y, orange)
  x = x + 1
  y = y + 2





# program b
im = Raster()
x = 10
y = 8
while x < 12:
  im.set(x,y, orange)
  x = x + 1
y = y + 2






# program c
im = Raster()
x = 10
y = 8
while x < 12:
  im.set(x,y, orange)
x = x + 1
y = y + 2









# program d
im = Raster()
y = 8
for x in range(10,12):
  im.set(x,y, orange)
  im.set(x,y + 5, blue)





# program e
im = Raster()
y = 8
for x in range(10,12):
  im.set(x, y, orange)
im.set(x, y + 5, blue)







# program f
im = Raster()
y = 8
for x in range(12,10):
  im.set(x, y, orange)
im.set(x, y + 5, blue)






# program g
im = Raster()
for x in range(10,12):
  for y in range(x, x + 2):
  if y < 12:
    im.set(x,y, orange)
  else if y < 13:
    im.set(x,y, blue)
  else:
    im.set(x,y, white)



















3. A local grocery store has a discount on 12-can packages of diet coke: if you buy one or two, you pay $4.50 each, but if you buy at least three, the price per package drops to $3.50. So, if you buy two packages, you pay 2 x $4.50 = $9.00, but if you buy four, you pay 4 x $3.50 = $14.00. Define a function (method) cokes(n) that, given a number of packages, returns the amount you need to pay. Write down a code that uses this method to compute the price of three packages.



















4. Define a function diag(image, color) that draws, in a given image, a full diagonal (starting at (0,0)) of a given color.










































5. Use a nested loop to draw a white triangle that covers all the points above the main diagonal.