Question 6-1

[10 pts] Suppose we have the class defined as below.

class C {
    static int x = 0;
    int y;

    C() {
        y = 0;
    }
    void incrX() {
        x++;
        IO.println(x);
    }
    void incrY() {
        y++;
        IO.println(y);
    }
    void incrZ() {
        int z = 0;
        z++;
        IO.println(z);
    }
}
public class Example {
    public static void run() {
        C a = new C();
        C b = new C();
        a.incrX();
        a.incrX();
        b.incrX();
        a.incrY();
        a.incrY();
        b.incrY();
        a.incrZ();
        a.incrZ();
        b.incrZ();
    }
}
What would the computer print when it executes the Example class's run() method?

Solution

1
2
3
1
2
1
1
1
1

Back to 8:00 Quiz 6