9.9k views
2 votes
Which of the following recursive methods will result in a stack overflow when main() calls fact(10)?

a. public static int fact(int n) {
if (n == 100)
return 1;
else
return nfact(n-1);
}
b. public static int fact(int n) {
if (n != 100)
return 1;
else
return nfact(n-1);
}
c. public static int fact(int n) {
if (n < 100)
return 1;
else
return nfact(n-1);
}
d. public static int fact(int n){
if (n <= 100)
return 1;
else
return nfact(n-1);
}

User Jameeka
by
8.1k points

1 Answer

1 vote

Final answer:

The only recursive method that will result in a stack overflow is option a.

Step-by-step explanation:

The only recursive method that will result in a stack overflow when main() calls fact(10) is option a.

This is because option a has a stopping condition of n == 100, which will never be met when calling fact(10) from main(). This will cause the recursive calls to continue indefinitely, leading to a stack overflow.

On the other hand, options b, c, and d have stopping conditions that will eventually be met when fact() is called with any positive integer value, preventing a stack overflow.

User GoldenJoe
by
8.2k points