Review for Quiz 0

One-page version suitable for printing.

Question 0-1

Using the following method, what are the values of the following expressions?

public static int f(int x) {
    int y = 4 * x;
    if(y < 10) return y;
    y++;
    return y / 6;
}
a.15 - 1 - 3 * 2
b.15 - 12 % 5
c.f(1)
d.f(f(4))

Solution

Question 0-2

Consider the following method to compute the greatest common denominator of two numbers.

public static int gcd(int m, int n) {
    int r;
    while(m != 0) {
        r = m % n;
        m = n;
        n = r;
    }
    return m;
}
Say we call gcd(42, 54). Show how the values of r, m, and n change as the computer continues through the method.
r
m 42
n 54

Solution

Question 0-3

Fill in run() so that the method will print ``Hello'' to the screen 2,001 times.

import csbsju.cs160.*;

public class PrintHello {
    public static void run() {





    }
}

Solution

Question 0-4

Say we've defined the following method.

public static int f(int x) {
    IO.print(x);
    return x * 2;
}
If our program executes the line IO.println(f(f(1)));, what does the program print?

Solution