115k views
5 votes
For the question below, refer to the following recursive factorial method.

public int factorial(int x)
{
if (x > 1)
return x * factorial (x - 1);
else
return 1;
}

What is returned if factorial(3) is called?

a) 0
b) 1
c) 3
d) 6
e) 9

User Mirek
by
5.3k points

1 Answer

3 votes

Answer:

(d) 6

Step-by-step explanation:

When we call factorial(3) first it will check if it greater than 1 since it is greater then return 3*factorial(2) will be called on factorial(2) it will again check that 2>1 since it is greater then return 2*factorial(1) will be called.In factorial(1) we have is not greater than 1 so it will return 1.return 2*factorial(1) will return 2 and 2 that is returned by factorial(2) will be multiplied with 3.So it will return 6.

User Reversed Engineer
by
4.7k points