logo Practice-It logo

while loop mystery

Language/Type: Java method basics mod mystery while loops
Author: Kimberly Todd (on 2010/12/28)

For each call below to the following method, write the value that is returned:

public static int mystery(int x) {
    int a = 1;
    int c = 0;
    while (x > 0) {
        a = x % 2;
        if (a == 1) {
            c++;
        }
        x = x / 2;
    }
    return c;
}
mystery(2);
mystery(-1);
mystery(7);
mystery(18);
mystery(43);

You must log in before you can solve this problem.


Log In

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

Is there a problem? Contact a site administrator.