Introduction to Computer Science

Quiz 1 Study Guide

This page contains a list of things you should know before taking Quiz 1.

Types and Values

The four types of values we discussed in the first two weeks of class are: int, str, float, and list. You should know which values have which types, be able to write expressions to produce each type, and be able to tell which type each expression will produce.

Exercises

What are the types for each of the following expressions? To what value does each expression evaluate?

Variables

Variables in Python and other procedural languages are mutable, meaning they can change value over time. This is unlike e.g. variables in arithmetic algebra. Know the syntax for variable assignment and the effect it has on a program.

Exercises

Which of the following are legal variable names? For each of the following which is not a legal variable name, why can’t you use it as a variable?

What will each of the following pieces of code print? Try to figure this out without using the Python interpreter.

Loops

In class, we have discussed the for loop. This loop runs an indented block of code many times, setting a variable to each element of a list as it does so. Know how loops run; be able to predict what will happen when a loop is executed and understand the influence this has on variables in the program. Know the term accumulator and know how to use an accumulator with a loop to build up an answer.

Exercises

What will each of the following pieces of code print? Try to figure this out without using the Python interpreter.

Debugging

By this point, you have written several small programs and undoubtedly encountered mistakes that you have fixed. Know how to find mistakes in small programs. Given a description of what a program should do and what it actually does, identify how it can be fixed. (Hint: there may be more than one thing wrong with a program.)

Exercises

#1

Program

name = "Gertrude"
print "Hello" + name

Desired Output

Hello, Gertrude.

Actual Output

HelloGertrude

#2

Program

for n in range(5):
  print n

Desired Output

1
2
3
4
5

Actual Output

0
1
2
3
4

#3

Program

s = ""
for num in range(4):
  s = s + str(num)
for row in range(4):
  print s

Desired Output

1 2 3 4
1 2 3 4
1 2 3 4

Actual Output

0123
0123
0123
0123

#4

Program

total = 1
for n in range(1,5):
  print n
  total = n
print "Total: %d" % total

Desired Output

1
2
3
4
5
Total: 15

Actual Output

1
2
3
4
Total: 4