Sample Test 2b
Sample Test 2b
Name: _________________________
Section:_________________________
1. (1 pt) What was the point of showing the Excel chart demonstration in class?
2. (2 pts) Write a while loop that prints the numbers starting at 500 down to 100. Count down by
10’s. Make sure to print both the numbers 500 and 100.
3. (2 pts) Write a function that prints the name of your favorite liquid beverage.
4. (1 pt) Write code that calls the function in the prior problem.
CmSc 150, Fall 2012 Test 2 Test 2
5. (3 pts) Write a function that will take in three numbers, and return how many are the same. For
example, passing in 5, 5, and 5 should return a three. Passing in 5, 4, 4 should return 2. And
passing in 3, 4, 5 would return 0.
6. (2 pts) Write code that will call the function in the prior problem and print the result. Make up
the numbers that are passed in.
def b():
print ("b start")
c()
print ("b end")
def a():
print ("a start")
c()
b()
print ("a end")
def c():
print ("c")
a()
CmSc 150, Fall 2012 Test 2 Test 2
def a(x):
print("A1",x)
x=x+5
print("A2",x)
return x
def b(x):
print("B1",x)
a(x)
print("B2",x)
x=5
print("G1",x)
b(x)
print("G2",x)
def sum(a,b):
print (a+b)
x = sum(11,22)
10. (3 pts) Write a function that will take in an array, and print each element individually:
CmSc 150, Fall 2012 Test 2 Test 2
11. (5 pts) Write a function that will take in an array, and return the sum.
12. (3 pts) Take a look at the two blocks of code below. Explain or write out how their output will be
different:
# Block 1
for i in range(3):
for j in range(3):
print ("*",end="")
print ()
# Block 2
for i in range(3):
for j in range(3):
print ("*",end="")
print ()
CmSc 150, Fall 2012 Test 2 Test 2
x=[[3,[4,5]],6,7,8,[9,10]]
print (len(x))
14. (4 pts) This code should count the number of times a number appears in an array, in this case 2.
There are 5 things wrong with this code. Find at least 4:
def countNumbers(list,key):
count=0
if list == key:
count += 1
return count
myList=[3,4,32,4,23,4,5,3,2,2,43,56,7,54,43,2,4]
c=countNumbers(list,2)
print(count)
CmSc 150, Fall 2012 Test 2 Test 2
15. (2 pts) What two things are wrong with this code? Explain.
def printCircleAreaChart(start,end):
for r in range(start, end):
def circleArea(radius):
area=3.14159*radius**2
print (area)
area = circleArea(r)
print (r, area)
printCircleAreaChart(10,30)
print("A\\\\B\nC\"D")
print ("00"+"11")
print (00+11)