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
- Everything that was on Exam0. So for some of the questions, you
will not need any of the new material.
- Tuples: similarities/differences between tuples, lists, dictionaries,
strings. Tuples are printed and created using () instead of []. Access
them the same way as lists and strings, like t[2] or t[2:5].
- Files: be able to open and read from or write to a text file.
This includes basic files functions open, .read, .write, .readlines,
.close. Note that files can be used as the "list" variable in a
for loop, and then it reads the file one line at a time in the for loop.
- Dictionaries: similarities/differences with lists. Be able to create
a dictionary, add keys to it, and get keys out of it. Be able to
understand a for loop that gets all keys out of the dictionary one
at a time. Examples... d{'key1':'data1', 'key2':'data2'} ...
print(d['key1']) ... d['key3'] = 'a new value'. Be able to understand
dictionaries that have numbers or strings as the key values, and
that have lists or dictionaries at the data.
- URLs: opening url's is similar to reading a text file. Instead of
the open function, you have to import urllib2 and then use the
urllib2.open function. After that, things are the same (use .read
function to read the whole file, .close to close it).
- Strings: be able to deal with strings that are the result of reading
a file. If s is a string, then blah=s.split('\n') will make blah a
list that has the lines of s. If s is a string, then
blah=s.split(' ') will make blah a list that has all the words of s.
- Computer: the main components of a computer - CPU, memory, disk.
What is the difference between memory and disk? Memory is
fast, expensive (and therefore smallish), and temporary. Disk is
show, cheap (and therefore largeish), and permanent. These differences
are the whole reason programs and variables are in memory, but files are
on disk.
- Other: I may add things here if I forgot anything... Did I?
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.
- True or False. Strings, tuples, and dictionaries are
all immutable. That means once a variable is created, the value of
it cannot be changed.
- 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]))
- 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}))
- 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))
- True or False. print('wonderful'[1:3]) prints the
value 'on'.
- True or False. The following code has a syntax error.
x = (1, 2, 7)
x[0] = 3
- True or False. The following code has a logical error.
f = open('somefile.txt','r')
s = f.read
f.close()
print(s)
- True or False. The following code prints the value 4.
msg = 'Hello world. It is great.'
words = msg.split(' ')
print(str(len(words)))
- 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'])
- 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)
- 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:
#
- 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:
#
- 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:
#
- 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]))
- 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]
- 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.
- 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'.
- 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])
- 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
- On paper. No computers, phones, calculators, etc.
- You are allowed one piece of 8.5x11" paper with anything you want
on it (front and back) - written by your or printed by a computer. But
you should pretty much know everything on it anyway, if you are going
to finish the test in time.
- Some questions similar to homeworks, others different. See
sample exam questions below.
Exam0 Topics
- Everything we have done so far. But in more detail here are the
things you should know everything about - you should be able to understand
code given to you about these things as well as produce code that
does them...
- Arithmetic: +, -, /, *, %, including order of operations, difference
between float and int
- Variables: create/save with =, also created any time you have a
for loop, also created with functions that take input.
- User input/output: print function, raw_input
- Strings: creation with "" or '', concatenation with +, accessing
characters with [] and [:], being able to use string functions
properly (count, index, isdigit, upper, lower, strip), using for
loops with strings, strings are immutable, comparing with
comparison operators
- Characters: changing between number and character with ord and
chr functions.
- Converting between types: int, float, str functions
- Conditional: using if, elif, else with Boolean expressions/operators
- Boolean stuff: using values True, False, operators and, or, not,
order of operations for and, or, and not, comparison
operators (< <= > >= != ==).
- While loop: using while loop to ask user for input until they enter
it correctly, using while loop with a counter, the break statement.
- For loop: looping over strings, looping over lists.
- Functions: using functions that are defined for you, defining your
own functions, functions that do not take input, functions that take
one or more inputs, the return command/stateent.
- Lists: creating, getting things out with [] or [:], lists are
mutable, using the list functions (count, index, sort), creating
list with range function, for loops with range
- Random numbers: importing random module and using randint and
choice.
- Encryption: how is Caesar cipher supposed to work, looping through
a string to encode each letter.
- Binary search: best way to play the number guessing game.
- Tracing: be able to trace through a program line by line, keeping
track of the values of variables.
- Debugging: be able to find syntax errors in a program and correct them.
Be able to look for logical errors in a program.
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.
- True or False. The following code has a syntax error.
number = raw_input(Enter a number between 0 and 100)
- True or False. The following code has a syntax error.
x = 2
if x=="3": print("blah blah")
- True or False. The following code has a logical error.
name = raw_input("What is your name?")
if name.lower()=="jeff": print("Hello, Jeff.")
- 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")
- True or False. 6 % 2 is equal to 3.
- True or False. ((3<=2)==False) is equal to True.
- True or False. True and not False is equal to False.
- True or False. "hello"[:2] is equal to "he".
- True or False. The code below prints the value [3,1].
x = [5, 3, 1, 2]
print(str(x[1:1+1]))
- 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))
- 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
- List all the variable values...
msg = "of"
result = ""
x=3
while x > 0:
result = result + msg
x -= 1
- 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)
- 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]
- 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)
- 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")
- 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()
- 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"
- 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.')