# Can Computers Think?Introduction to Computer Science

## CSC 106Union CollegeWinter 2010

### Preparing for the (midterm) exam

#### What to do to prepare

• Review codelab exercises, homework exercises, in-class exercises.
• Concentrate on the points you had trouble with.
• Make sure really understand the solution.
• Just "memorizing" the right answer is not enought because it will most likely not help you solve other similar problems.
• If you don't feel that you could solve other similar problems, review your notes and read the relevant parts of the book.
• (Side note on reading the book: The book is not light reading that you can do while listening to music, watching TV or doing other things. You need to concentrate and really think through the examples that it presents to you.)
• If you still feel unsure about some concepts or problems, come to see me to go through your questions.

#### Important concepts to know

• expressions, statements
• variables
• conditional statements (if-statements)
• functions (calling a function, parameters, return values)
• conditional statements (if-statements)
• loops

#### Types of questions you can expect on the midterm

##### Understanding basic Python syntax

For each of the following code snippets, say whether they are a valid Python expression, a valid Python statement or not valid. For expressions, also say what value they refer to. For statements explain what they do. For code snippets that are not valid explain why they are not valid.

1. cat"
2. 4 + 6
3. math.sqrt(9)
4. print random.randint(1,50)
##### Being able to trace what a Python program does step by step

a) What do you see on the screen, if we run the following program? How does the value that the variable 'count' refers to change throughout the execution of the code?

```count = 0
while count <= 20:
if count%2==0:
print count
count = count + 1
```

b) The following program is supposed to calculate the average of all numbers from 1 to 10. Instead we get an error message when we run it.

```count = 1
while count <= 10:
total = total + count
count = count + 1
avg = float(total) / 10
```

Explain what causes this error. Then make a suggestion how it could be fixed.

##### Devising strategies for solving a problem computationally

Assume you are given a string and are asked to create a new string that looks the same as the first except that an underscore has been inserted between all letters. For example, dog yields d_o_g, elephant yields e_l_e_p_h_a_n_t and a yields a.

Write down in English a step-by-step strategy for solving this problem.