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?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.