Exam Review

[Note: this webpage last modified Monday, 06-Feb-2012 20:09:21 EST]

This webpage contains information about the exams...

Exam2, Final Exam

The final exam covers the same material as Exam1. The information on classes and objects that we have done in class since then will not be on the exam.

Exam1, Second Exam

The second exam covers readings/homeworks up through Week 11 on the schedule. It is cumulative. The exam does not cover images or turtles, but covers pretty much everything else we have done in class. See the list of topics below for more details.

Exam1 Format

Same as Exam0, see the format of exam0 below.

Exam1 Topics

Exam1 Sample Questions

This is a similar number and type of questions as what you can expect on the real exam. Here are the breakdowns on each type of question, how much they'll be worth, and how much time I hope you can finish each question in.

Note: you can consider all the questions from exam0 and the sample exam from exam0 as sample questions as well - since the test is cummulative.

True/False: 10 questions, 1 pt each, 10 pts total, 1 minute per question, 10 minutes total.
List the variables: 3 questions, 3 pts each, 9 pts total, 3 minutes per question, 9 minutes total.
Fix syntax errors: 2 questions, 3 pts each, 6 pts total, 3 minutes per question, 6 minutes total.
Give code/fix logical errors: 4 questions, 4 pts each, 16 pts total, 5 minutes per question, 20 minutes total.

That adds up to 41 points and 45 minutes. You will have the whole classtime to take the test.

  1. True or False. Strings, tuples, and dictionaries are all immutable. That means once a variable is created, the value of it cannot be changed.
  2. True or False. The following code has a syntax error.
    def total(myList):
      x = 0
      for val in myList:
        x = x+val
      return total
    print(total([2, 4, 6]))
    
  3. True or False. The following code has a logical error.
    def total(myDict):
      x = 0
      for val in myDict:
        x = x+val
      return total
    print(total({'test1':95, 'test2':99, 'test3':100}))
    
  4. True or False. The code below prints the value 24.
    z = [9, 8, 7]
    x = 0
    for i in range(0, len(z)):
      x=x+1
    print(str(x))
    
  5. True or False. print('wonderful'[1:3]) prints the value 'on'.
  6. True or False. The following code has a syntax error.
    x = (1, 2, 7)
    x[0] = 3
    
  7. True or False. The following code has a logical error.
    f = open('somefile.txt','r')
    s = f.read
    f.close()
    print(s)
    
  8. True or False. The following code prints the value 4.
    msg = 'Hello world. It is great.'
    words = msg.split(' ')
    print(str(len(words)))
    
  9. True or False. The following code prints the value 13.
    d={'name':[2,1,0], 'what':10, 11:'okay'}
    x=d['name']
    print(x[0]+d['what'])
    
  10. True or False. The following is the correct way to read a webpage URL into a string.
    f = open('http://cs.indstate.edu/~jkinne/cs151-f2011/index.htlm')
    s = f.read()
    f.close()
    print(s)
    
  11. List the values of the variables.
    def func(myList):
      for i in range(0, len(myList)):
        myList[i] = myList[i]+myList[i]
    L = ['a', 'b', 'c']
    L = func(L)
    #
    # L: 
    # myList: 
    # i: 
    #
    
  12. List the values of the variables. Assume the for loop takes the keys out of the dictionary in the order they are typed in the line that creates the dictionary.
    d = {'the':'word', 'a':'another', 'it':'what'}
    small = 'aaa'
    for k in d:
      if d[k] < small: 
        small = d[k]
    print(small)
    #
    # d:
    # small:
    # k:
    # 
    
  13. List the values of the variables. Assume you have a file called file.txt that has the following contents.
    96 88 77 100 -1 55
    What are the values when you then run this code.
    f = open('file.txt', 'r')
    s = f.read()
    f.close()
    L = s.split(' ')
    index = 0
    while float(L[index]) > 0:
      print(L[index])
      L[index] = -1*float(L[index])
      index = index + 1
    #
    # s:
    # L: 
    # index:
    #
    
  14. Fix the syntax errors. The following code is supposed to take the average of a list of numbers. There are 3 syntax errors.
    def avg(myList):
      total == 0
      for num in myList
        total = total + num
      return total / float(len(myList))
    print(avg([1 2, 3, 4, 5]))
    
  15. Fix the syntax errors. The following code is supposed to ask the user for some information and put it into a dictionary. There are 3 syntax errors.
    name = raw_input('What is your name? ')
    age = raw_input['What is your age? ']
    when = raw_input(When will the world end? )
    d = ['name':name, 'age':age, 'when':when]
    
  16. Give code. Give code for a function that opens a file called 'words.txt', reads the text into a string, splits that string at each ' ', and prints the total number of words that result.
    
    
  17. Give code. Give code for a function that asks the user for a word. The code whould print whether the word is in the first 1/3 of the dictionary, middle 1/3, or last third. Assume the first 1/3 are words that start with 'a' through 'h', the middle third are words that start with 'i' through 'p', and the last third are words that start with 'q' through 'z'.
    
    
  18. Fix the logical errors. The following code is supposed to find all people in a dictionary that are male and print their names. There are 2 logical errors.
    d = {'Jeff':{'sex':'m', 'age':'young'},
         'Catherine':{'sex':'f', 'age':'middle'},
          'Davy':{'sex':m', 'age':'old'}}
    for k in d:
      if d[k]['sex'] == 'Male':
        print(d[k])
    
  19. Fix the logical errors. The following function is supposed to compute n!, the factorial of a number. There are 2 logical errors.
    def factorial(n):
      val = 0
      for i in range(1, n+1):
        val = val * i
      return 
    print('Factorial of 10 is ' + str(factorial(10)))
    

Exam0, First Exam

The first exam covers reading/homeworks up through Week 4 on the schedule. The exam does not cover working with pictures, or anything beyond that point.

Exam0 Format

Exam0 Topics

Sample Exam0 questions This is a similar number and type of questions as what you can expect on the real exam. Here are the breakdowns on each type of question, how much they'll be worth, and how much time I hope you can finish each question in.

True/False: 10 questions, 1 pt each, 10 pts total, 1 minute per question, 10 minutes total.
List the variables: 3 questions, 3 pts each, 9 pts total, 3 minutes per question, 9 minutes total.
Fix syntax errors: 2 questions, 3 pts each, 6 pts total, 3 minutes per question, 6 minutes total.
Give code/fix logical errors: 4 questions, 4 pts each, 16 pts total, 5 minutes per question, 20 minutes total.

That adds up to 41 points and 45 minutes. Some will be faster than this and some will be slower.

  1. True or False. The following code has a syntax error.
    number = raw_input(Enter a number between 0 and 100)
  2. True or False. The following code has a syntax error.
    x = 2
    if x=="3": print("blah blah")
  3. True or False. The following code has a logical error.
    name = raw_input("What is your name?")
    if name.lower()=="jeff": print("Hello, Jeff.")
  4. True or False. The following code has a logical error.
    s="An important message..."
    for letter in s:
      if s=="m": print("Found letter m")
  5. True or False. 6 % 2 is equal to 3.
  6. True or False. ((3<=2)==False) is equal to True.
  7. True or False. True and not False is equal to False.
  8. True or False. "hello"[:2] is equal to "he".
  9. True or False. The code below prints the value [3,1].
    x = [5, 3, 1, 2]
    print(str(x[1:1+1]))
  10. True or False. The code below prints the value 100.
    x = 10
    y = 1
    while x >= 0:
      y = y + 1
      x -= .1
    print(str(y))
  11. For the following code, list all variables that are used, and put a trace of the all the values that those variables have while it is run (with your list so the final value is on the end).
    x=0
    for c in "why oh why?":
      if c>"m": x = x + 1
  12. List all the variable values...
    msg = "of"
    result = ""
    x=3
    while x > 0: 
      result = result + msg
      x -= 1
  13. List all the variable values...
    def fun(x,y,z):
      if x < y and x < z: return x
      elif y < x and y < z: return y
      else: return 0
    blah = fun(3, 2, 1)
    blah = fun(blah, -1, 3)
  14. The following is supposed to find the smallest number in a list. Fix all the syntax errors.
    def findSmallest(someList)
      smallest = someList[0
      for num = someList:
        if num < smallest: smallest = num
      return smallest
    findSmallest[5, 6, 7, 3, 0, 10, 20]
  15. The following is supposed to count how many coin flips until the given number of heads in a row. Fix all the syntax errors.
    import(random)
    def howMany(wantInARow):
      inARow=0
      count = 0
      while inARow < wantinarow
        count = count + 1
        if random.choice([h,t])="h": inARow = inARow + 1
        else: inARow = 0
      return count
    howMany(3)
  16. Complete the code to make a function that counts the number of digits (0-9) in a string.
    def countDigits(s):
      # put your code here, and return the count instead of returning nothing.
      return
    countDigits("Number is 124")
  17. Complete the code to get a user's first and last name using raw_input and say hello to them.
    def sayHello():
      # put your code here...
      # if they typed Jeff and Kinne, it should print "Hello Jeff Kinne"
      print("")
    sayHello()
  18. Here is a function that is supposed to do a grading scale. It has logical errors. Fix the code.
    def grade(num):
      if grade >= 60: return "D"
      elif grade >= 70: return "C"
      elif grade >= 80: return "B"
      elif grade >= 90: return "A"
      else: return "F"
  19. Here is a function that is supposed to replace all 'E' characters with '3' characters, all 'z' with '2', and all 'o' with '0'. There are logical errors. Fix them.
    def numbersLookLikeLetters(s):
      newS = ""
      for letter in s:
        if letter=='E': newS += '3'
        elif letter=='2': newS += 'z'
        elif letter=='o': newS += '0'
        else: newS = letter
      return s
    numbersLookLikeLetters('Every zebra glows.')