logo Practice-It logo

whileLoopMystery2

Language/Type: Java mystery while loops
Author: Leslie Ferguson (on 2020/04/08)

Given the following method:

public static int mystery2(int x) {
    int a = 1;
    int c = 0;
    while (x > 0) {
        a = x % 2;
        if (a == 1) {
            c++;
        }
        x = x / 2;
    }
    return c;
}

Write the output of each of the following calls.

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.