CSC 015: Sample Midterm Examination I
1. What is the output from each of the following code fragments? Choose any five.
(a) Fragment 1 (arithmetic expressions)
a = 1
b = 3
c = 9
d = 2
e = 4
f = 2.0
print (a + c // d * f - c)
print ((b+d)%3 ** (20-c*d))
(b) Fragment 2 (if statements)
act = "x"
second = "-j"
third = "10"
funcs = "Acdrtux"
opts = "bBCfFhijkKlLmM"
found = 0
if funcs.endswith(act) or funcs[0] != "A":
print (’Action found:’, act)
found = 1
else:
print (’Action invalid:’, act)
if second[1] == "J":
print (’Option invalid:’, second)
else:
print (’Option found:’, second)
print (’Parameter is’, third)
if found == 1:
print (’Action + Option’)
(c) Fragment 3 (for loop)
sum = 0
for val in range(1, 4):
sum = sum + 2 * val
print (sum)
avg = sum / 3
print (avg)
1
(d) Fragment 4 (list indexing)
i = 2
groovy = [1, ’Krish’, 3, ’George!’, 5, ’Monkey’, 7]
print (2 * groovy[-1])
print (groovy[2*i+1])
j = 0
print (groovy[i] + groovy[j])
print (groovy[i+j])
(e) Fragment 5 (while loop)
index = 0
letterlist = [’b’, ’f’, ’j’, ’p’, ’v’]
query = ’e’
while index < 5 and letterlist[index] != query:
index = index + 1
print (index)
(f) Fragment 5 (Functions and calls)
def mystery1(N, M):
numer = N ? M
denom = N + M
accel = 10*numer/denom
return accel
mystery1(2*Y+1, X+Y-4*Y)
2. What is/are the error(s) in each of the following fragments? Suggest corrections for each
error. Assume that all the variables are defined (and have values).
(a) Fragment 1
x = 5
count = 3
if x = 5
2 * count = count
(b) Fragment 2
#Wait for user to input a positive number
inputval = input("Please enter a positive number: ")
while inputval >= 0
print "That is not a positive number. Try again.")
inputval = input("Please enter a positive number: ")
2
(c) Fragment 3
# First define the function called ‘mystery’
def mystery(first, second):
result = first + second - 1
return result
# Main program
a = 1
b = 2
def mystery(a) #Function call
print (result)
3. Write Python statements for the following problems (assume that all variables have been
assigned values). Answer any three parts of this question.
(a) Problem 1: A student passes a course if their absence is less than 3 and at least one of
the following is true:
• the finalgrade is at least 90, or
• the averagegrade is at least 65.
Write an if statement that prints “pass” or “fail” as appropriate, based on absence,
finalgrade and averagegrade.
(b) Problem 3: In a certain program that I wrote, I had the following sequence of statements:
doji_tracker(symb[0], 12, 0)
doji_tracker(symb[1], 12, 10)
doji_tracker(symb[2], 12, 20)
doji_tracker(symb[3], 12, 30)
doji_tracker(symb[4], 12, 40)
doji_tracker(symb[5], 12, 50)
doji_tracker(symb[6], 12, 60)
doji_tracker(symb[7], 12, 70)
doji_tracker(symb[8], 12, 80)
As you can see, there is a lot of repetition going on here. Write a for loop to shrink all
those lines into just two lines.
(c) Problem 3: Define a function called redDiff that takes as parameter a list called pixel.
It should return the difference between the first and third elements in the tuple.
(d) Problem 4: Write a Python statement to input a positive number called inputval.
Then, as long as inputval is less than 0, repeatedly prompt the user to input a new
value for inputval.
4. Write a function called UP that takes as parameter a variable called myNumList. The function
should return how many numbers in myNumList are respectively greater than the next number
in the list. For example, if the list given is: [12, 16, 18, 11, 9, 15, 3], then there are
3
three numbers (18, 11 and 15) that are greater than the next number (respectively, 11, 9 and
3) in the list.
Write a function called monotone that takes as parameter a list L of floats. The function
should return True if every number in L is bigger than the number before it in L. Otherwise
it should return False.
4