Ask Question
20 December, 17:30

Analyze the following recursive method and indicate which of the following will be true.

public static long factorial (int n)

{ return n * factorial (n - 1); }

a) The method runs infinitely and causes a StackOverflowError.

b) Invoking factorial (2) returns 2.

c) Invoking factorial (1) returns 1.

d) Invoking factorial (0) returns 0.

+4
Answers (1)
  1. 20 December, 19:45
    0
    a) The method runs infinitely and causes a StackOverflowError.

    / / Return the factorial for a specified index

    public static long factorial (int n)

    {

    if (n = = 0)

    return 1;

    else

    return n * factorial (n - 1);

    }

    b) Invoking factorial (2) returns 2.

    / / Return the factorial (2)

    public static long factorial (int 2)

    {

    if (n = = 2)

    return 2;

    else

    return 2 * factorial (3 - 1);

    }

    c) Invoking factorial (1) returns 1.

    / / Return the factorial (1)

    public static long factorial (int 1)

    {

    if (n = = 1)

    return 1;

    else

    return 1 * factorial (1 - 1);

    }

    d) Invoking factorial (0) returns 0.

    / / Return the factorial (0)

    public static long factorial (int 0)

    {

    if (n = = 0)

    return 0;

    else

    return 0 * factorial (0 - 1);

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Analyze the following recursive method and indicate which of the following will be true. public static long factorial (int n) { return n * ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers