Midterm Preparation

< CS101

This page has information about the midterm exam and some sample problems to work. The exam will be in Cubberly Auditorium Thu May 2nd at regular class time (by the clock tower). The exam will be 60 minutes (we can start a little late to allow for stragglers).

The exam will have 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. Like the homeworks, problems may include some starter code for you.

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.setReeed(4); or omit one parenthesis or comma or something, we'll still give full credit, so long as the key ideas of the solution are there and we can see what you meant.

This is closed everything exam: no notes, no computer, no cell phone, no calculator. As a result, I do not need to make the questions very obscure; just variations on the themes from the homework problems. Just bring yourself and a pen and that's it. 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 reference sheet below listing the major phrases of syntax we have used, so you don't need to memorize the superficial 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 rote 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 all the homework problems and all the lecture code examples I have run (all available online of course). You should be able to type in the code for any of our lecture or homework problems 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 earler examples. To study, select and delete the code in one of the boxes, and then see if you can type in a reasonable solution just from reading the problem statement.

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

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


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

Functions:
-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)
-image.setSameSize(other_image)
 

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? (including units in your answer) : _________

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.

  // your code here









Problem 2

1. 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().

image = new SimpleImage("10-20-30.png");
for (pixel: image) {
  // your code here









Problem 3

2. 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. You may omit the final print().

image = new SimpleImage("a.jpg");
back = new SimpleImage("b.jpg");

for (pixel: image) {
  // your code here










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().

image = new SimpleImage("flowers.jpg");















Solutions:

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

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


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

3.
for (pixel: image) {
  if (pixel.getRed() > 100) {
    pixel2 = back.getPixel(pixel.getX(), pixel.getY());
    pixel.setRed(pixel2.getRed());
    pixel.setGreen(pixel2.getGreen());
    pixel.setBlue(pixel2.getBlue());
  }
}


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