logo CodeStepByStep logo

MysteryGhost

Language/Type: Java parameters return
Author: Mehran Sahami (on 2017/10/03)

For the following console program, trace through its execution by hand to write the five lines of output that are produced when it runs:

/*
 * File: Mystery.java
 * ------------------
 * This program doesn't do anything useful and exists only to test
 * your understanding of method calls and parameter passing.
 */
public class Mystery {
    public static void main(String[] args) {
        ghost(13);
    }

    private static void ghost(int x) {
        int y = 0;
        for (int i = 1; i < x; i *= 2) {
            y = witch(y, skeleton(x, i));
        }
        println("ghost: x = " + x + ", y = " + y);
    }

    private static int witch(int x, int y) {
        x = 10 * x + y;
        println("witch: x = " + x + ", y = " + y);
        return x;
    }

    private static int skeleton(int x, int y) {
        return x / y % 2;
    }
}
line 1
line 2
line 3
line 4
line 5

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.