Review for Quiz 0

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

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

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





    }
}

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?

Solutions

Solution 0-1

a.8
b.13
c.4
d.8

Solution 0-2

r    42 12  6  0
m 42 54 42 12  6
n 54 42 12  6  0

Solution 0-3

import csbsju.cs160.*;

public class PrintHello {
    public static void run() {
        int i = 0;
        while(i < 2001) {
            IO.println("Hello");
            i++;
        }
    }
}

Solution 0-4

124