For each call below to the following method,write the value that is returned: pu
ID: 3611642 • Letter: F
Question
For each call below to the following method,write the value that is returned:public static int mystery(intx)
{
int x = 1;
int y = 0;
while (x > 0)
{
x = x %2;
if (x ==1)
{
y++;
}
x = x /2;
}
return c;
}
MethodCall Value Returned
mystery(2); __________
mystery(-1); __________
mystery(7); __________
Explanation / Answer
public static int mystery(int x) { //int x = 1; //commented this lineas the function is already getting X as a parameter... //if u dont comment the above line, the program gives a compilationerror int y = 0; while (x > 0) { x = x %2; if (x ==1) { y++; } x = x /2; } return y; //u are returningC..which is not defined..im assuming it as y or X and giving outputfor both } OUTPUT if y is returned by the function: System.out.println(mystery(2)); gives 0 System.out.println(mystery(-1)); gives 0 System.out.println(mystery(7)); gives 1 OUTPUT if x is returned by function Mystery() System.out.println(mystery(2)); gives 0 System.out.println(mystery(-1)); gives -1 System.out.println(mystery(7)); gives 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.