Home CPSC 110

Midterm Review

General Computer Science


Python Basics

Below is a list of topics that are fundamental to programming, the sort of atomic building blocks that all programs are made of:

Some questions will test your understanding by either asking you to interpret code. For these, you will need to go through the program line by line as if you are the Python interpreter and follow the program to see what it will do.

Other questions will ask you to write small programs to solve problems. Most points will be on developing the actual algorithm. If you mess up the syntax, it won't count against you, so long as the algorithm is clear.


Interpreting Examples

What does the following code output:


# comment
number = 100
number = number / 2
number = number + 1
print(number)

How about this program, if the user enters "Orange" then "Hammer"?


a = input("Enter a word: ")
b = input("Enter a word: ")

thing = b + " then "
thing2 = thing + a
print(thing2)

Writing Examples

Write a short Python program to read in a person's height. It should ask for how many feet and how many inches (for example, you might put in 5 feet and 6 inches). It should then print their height just in inches (in this example 66).





Write a short Python program to read in a person's first name and last name. Then print out how many characters are in their first name, last name, and how many are in their full name.






Control Structures

Below is a list of the control structures we have covered:

There will also be questions covering these that ask you to either interpret, or write code.


Interpreting Examples

What does the following code print?


x = 7
y = 2

if y != 2:
    print("Apple")
elif x == 7:
    print("Banana")
else:
    print("Pear")

How about this program?


number = 7
times = 0

while number > 0:
    number = number - 2
    times = times + 1
print(times)

Writing Examples

Write a program that reads in a string and a number from the user and prints out the string that many times. For example, if the user enters "Hello" and 5, it should print "Hello" 5 times and then stop.





Write a program that asks the user what year they are going to be graduating and tells them whether they are a "Goat" (those with even graduation years) or a "Devil" (those with odd).





The Collatz conjecture says that if you take any positive number and repeat a certain process you will eventually hit 1. The process is that if the number is even, you divide it in half. If it's odd, you multiply by 3 and add 1.

For example, if we start with 10, divide by 2 to get 5. Then we multiply by 3 and add 1 which gives 16. Then 8, 4, 2, and finally 1. The conjecture has never been proven, but it works for all numbers we have tried it on.

Write a program to get a number from the user and print the numbers we hit up until 1.





(This one is just a bit harder than anything I'd put on the midterm, but if you can do this you're well prepared!)

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.