9:40 Quiz 2

One-page version suitable for printing.

Statistics:

mean     21.667 (650.000/30)
stddev   3.618
median   21.000
midrange 18.000-24.000

# avg
1 6.63 / 10
2 7.8  / 10
3 7.23 / 10

Question 2-1

Suppose a user executes the following run() method.

public static void run() {
    String str = "arboreteum";
    int j = 0;
    for(int i = 0; i < str.length(); i++) {
        if(str.charAt(i) == 'e') {
            IO.print(str.substring(j, i));
            j = i + 1;
        }
    }
    IO.println(str.substring(j));
}
Show all the values taken on by the variables of the program.
i
j
What does the method print?

Solution

Question 2-2

Say we've defined the following method.

public static int f(int n) {
    IO.println(n);
    return n / 2;
}
Also, say our program executes the line
IO.println(1 + f(1 + f(16)));
What is printed in executing this line?

Solution

Question 2-3

What does the following method print?

import csbsju.cs160.*;

public class Q3 {
    public static void run() {
        Account a = new Account();
        a.deposit(4.0);
        Account b = new Account();
        b.deposit(8.0);
        Account c = b;
        b = a;
        b.deposit(2.0);
        IO.println(a.getBalance());
        IO.println(b.getBalance());
        IO.println(c.getBalance());
    }
}

Solution