Studying for the Midterm

< CS101

This page has information about the midterm exam and some sample problems to work. The exam will be in Hewlett 200 Thu April 30 at regular class time. The exam will be 1 hour, so we can start a little late to allow for stragglers.

The exam will have a few short answer questions from the lecture, but most of the points will focus on questions where you write code to solve something, very much like the homeworks. Indeed, when creating the exam, I will start with the homework and lecture problems for inspiration.

Writing code on a piece of paper is different from typing code into the computer, and our grading criteria take that into account. We do not grade off for trivial syntax errors that you would easily catch when running on the computer. So if you write poxel.setReed(4); or omit one parenthesis or comma or something, you'll still get full credit, so long as the key ideas of the solution are there and we can see what you meant. You can omit the if-test "* 1.1" factors we had for real code, since those make no sense on paper.

This is closed everything exam: no notes, no computer, no cell phone, no calculator. Just bring yourself and a pen or pencil and that's it. As a result, I do not need to make the questions very obscure; just variations on the themes from the homework problems. If you do not want to do the arithmetic for an answer, you can write an answer as an expression like "5.5 * 100 / 2" and we will do the arithmetic for you when grading. That said, the arithmetic will tend to be easy enough that you can do it in your head anyway.

The exam will contain the "cheat sheet" below listing the major phrases of syntax we have used, so you don't need to memorize those details. The points on the exam will come from using the syntax to solve little problems, just like the homework. If a question requires a little boilerplate code at the start, often the question will include that code already done, and the points will be from writing the rest of the required code (as on the homework).

How To Study

My advice to study for the exam is that you be comfortable with writing code for all the homework problems and all the lecture code examples we have run (all on the course page). You should be able to scroll down to a lecture example or homework problem, and type in a pretty good solution just from reading the description, without needing to refer to any example code. This is a higher bar than the homeworks, where you very likely scrolled back to remind yourself from earlier examples.

Code Reference

Code reference "cheat sheet" to be printed with exam:

// Basic image for-loop example
image = new SimpleImage("something.jpg");
for (pixel: image) {

// If-Statement example:
if (100 > 50) {
  // body lines

-pixel.setRed(val)  // likewise for green and blue
-pixel.getRed()     // likewise for green and blue
-pixel.getX(), pixel.getY()
-image.getWidth(), image.getHeight()
-image.getPixel(x, y)

The lecture example and homework problems are an excellent source of practice problems. Here are some sample problems that cover some of the techniques we have seen.

Practice Short Answer

Number of bits in a byte: _______

Sally has 2.8 gigabytes. Bobby has 500 megabytes. Added together they have about how much in GB? : _________

Machine code is a language implemented by what piece of hardware:_____

Problem 1

Write code that prints the following with your name.

My name is Alice
Alice is in this class

Rather than mentioning your name directly, assign a variable to hold your name (i.e. name = ... ; ), then use that name variable within the print statements to insert your name.

Problem 2

This is a 10-20-30 puzzle. Write code to solve this puzzle -- for each pixel, multiply red by 20, green by 10, and blue by 30. You may omit the final print().

Problem 3

This is a bluescreen problem. Write code that for each pixel, in "image", if the red value is greater than 100, copy over the analogous pixel from the "back" image. Note that we are not using the more sophisticated "avg" technique here. You may omit the final print().

Problem 4

Write code to make the following changes to an image, such as "flowers.jpg". For each pixel, if the green value is more than double the blue value, double the blue value, and set the red value to 100. The standard line to load the image is provided. You may omit the final print().


Short answer: 8 bits in a byte.
2.8 GB + 500 MB is about 3.3 GB.
Machine code is implemented by the CPU.

x = "Alice";
print("My name is", x);
print(x, "is in this class");

for (pixel: image) {
  pixel.setRed(pixel.getRed() * 20);
  pixel.setGreen(pixel.getGreen() * 10);
  pixel.setBlue(pixel.getBlue() * 30);

for (pixel: image) {
  if (pixel.getRed() > 100) {
    pixel2 = back.getPixel(pixel.getX(), pixel.getY());

for (pixel: image) {
  if (pixel.getGreen() > pixel.getBlue() * 2) {
    pixel.setBlue(pixel.getBlue() * 2);