142k views
0 votes
What value does function mystery return when called with a value of 4?

a) 0.
b) 24.
c) 1.
d) 4.

1 Answer

3 votes

The function "mystery" returns the value 24 when called with the argument 4. This recursive function calculates the factorial of a number by multiplying it with the factorial of the preceding number. The correct answer is Option B.

The "mystery" function is a recursive function that calculates the factorial of a given number. Here's how it works:

int mystery(int number) {

if (number <= 1) {

return 1;

} else {

return number * mystery(number - 1);

}

}

When called with a value of 4:

mystery(4) = 4 * mystery(3)

= 4 * 3 * mystery(2)

= 4 * 3 * 2 * mystery(1)

= 4 * 3 * 2 * 1

= 24

So, the correct answer is:

b) 24.

The complete question is:

What value does function mystery return when called with a value of 4? int mystery (int number) { if (number <= 1) { return 1; } else { return number *mystery(number - 1); a) 0.

b) 24.

c) 1.

d) 4.

User Jan Palas
by
8.1k points