| CIT 591 Midterm Exam, Fall 2009 | Name ______________________________ |
Please keep all answers short and to the point. Do not add information that is not asked for; you will lose points for getting it wrong.
Enter your name: , then read in, and assign to a variable, a string typed in by the user.Enter your age: , then read in, and assign to a variable, an integer typed in by the user.Your name is var1 and Your age is var2, where var1 and var2 are the values of the two variables entered above.answer. classes.| print classes[-1] | OR | print classes[len(classes) - 1] |
sentence.scores.| print scores[1:-1] | OR | print scores[1:len(scores) - 1] |
list2, a copy of the list variable list1.odd to True if the integer variable count is an odd number, and False otherwise.sum and product to the sum of x and y and the product of x and y, respectively.mid to the "middle" value (neither the max nor the min) of the three integer variables x, y, and z.list1 is a list of integers. Set variable list2 to contain all the positive integers in list1. for loop to print the contents of list variable list, in order, one value per line. | for i in list: print i |
OR | for i in range(0, len(list)): print list[i] |
while loop to print the contents of list variable list, in order, one value per line.| i = 0 while i < len(list): print list[i] i += 1 |
NO CREDIT if your code had the side effect of |
for loop to print the contents of list variable list, in reverse order, one value per line.| for i in range(1, len(list) + 1): print list[-i] |
OR | for i in range(len(list), -1, -1): print list[i] |
negative, zero, or positive, according to whether variable x is less than zero, zero, or greater than zero, respectively.foo.txt, and write all the values in list variable words to it, one value per line. isEven that, given a single integer parameter, returns True if the parameter is an even number, False otherwise. | Also acceptable: | ||
| isEven = lambda n: n % 2 == 0 | def isEven(n): return n % 2 == 0 |
collatz(7) should return 22. evenRand() should return an even number.randBuzz(). The test should pass if randBuzz() returns either a positive number or the string 'buzz', and fail otherwise.
| def testRandBuzz(self): result = randBuzz() assertTrue(result > 0 or result == 'buzz') |
| # BUT NOT def testRandBuzz(self): assertTrue(randBuzz() > 0 or randBuzz() == 'buzz' |
None.| Also acceptable: | Also acceptable: | |||
| a = [[[None] * 10] * 10] | a = [ ] for i in range(0, 10): a.append(10 * [None]) |
a = [0] * 10 for i in range(0, 10): a.append(10 * [None]) |
list1 = [1, 2, [3, 4]] list2 = list1[:] list1[0] = 5 list2[2][0] = 6 print list1, list2[5, 2, [6, 4]] [1, 2, [6, 4]]
list = [1, 2, 3]
list = list.append('3')
print listNone
a = [1,2,3] b = [1,2,3] print a == b, a is bTrue False
def mystery(list):
if len(list) == 1: return list[0]
else: return mystery(list[1:])
The function returns the last value in the input list. The list itself is not changed.
class Robot(object), and you want to create a class to represent a dog robot. What is the difference between saying class Dogbot(object) and saying class Dogbot(Robot) ?Person, with instance variables name and age. If child is an instance of person, it is legal to say child.age += 1, but this is poor practice. What priciple does this violate?Likely to be:
|
Here are some samples of true statements that don't answer the question:
And here's an untrue statement:
|