Home CPSC 220

Midterm Review

Overview

The midterm exam will cover everything we've done thus far. The test will include a few multiple choice questions, but will mostly be short answer.

It mostly will focus on two main areas: reading and understating code, and writing code.

There will also be conceptual questions as well.


Reading Code

Understanding how code will operate by reading it is an important part of being a programmer. It's needed to debug your programs, as well as communicate with other computer scientists. The midterm will include questions where you read over a program or code snippet and describe what will happen when it executes.


Reading Example 1

What will the following program print?

public class R1 {

    public static void main(String args[]) {
        for (int i = 1; i < 8; i += 2) {
            System.out.println(i);
        }
    }
}


Reading Example 2

What will he following program print?

public class R2 {
    public static void main(String args[]) {
            System.out.println(1/2);
    }
}

Reading Example 3

What does the following program print?

public class R3 {
    public static void main(String args[]) {

        char c = 'b';

        switch (c) {
            case 'B':
                System.out.print("1");
            case 'b':
                System.out.print("2");
            case 'c':
                System.out.print("3");
                break;
            case 'D':
                System.out.print("4");
            default:
                System.out.print("5");
                break;
        }
    }
}

Reading Example 4

What does the following program print?

public class R4 {

    static double x = 5;

    public static void function(double x) {
        System.out.println(x);
    }

    public static void main(String args[]) {
        System.out.println(x);

        function(10);

        if (x > 0) {
            double x = 15;
            System.out.println(x);
        }
        System.out.println(x);
    }
}

Reading Example 5

What does the following program print?

public class R5 {

    public static void doSomething(int a, int b []) {
        a++;
        for (int i = 0; i < b.length; i++) {
            b[i]++;
        }
    }

    public static void main(String args[]) {
        int number = 1;
        int numbers [] = new int [] {2, 3};
        doSomething(number, numbers);
        System.out.printf("%d, %d, %d\n", number, numbers[0], numbers[1]);
    }
}

Writing Code

Of course writing code is an important part of programming as well. For tests in this course, the main idea in a programming question is the most important thing, and details like missing semi-colons typically do not matter.


Example: Estimating a Series

An infinite series is a sequence of numbers with a fixed ratio between values. One of the most famous is the following:

$\frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \frac{1}{16} + \frac{1}{32} + \ldots$

How could we write a program that reads in a number $N$ from the user and prints the sum of the first $N$ terms in this series?





Example: Median

Finding the median of an array is trickier than finding the mean. How can we do this?





Example: Mode

Finding the mode of an array is also a bit trickier than the mead or median, but is also made easier by having the array sorted. How can we do this?

We can assume there is only one mode.




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