Note: Java Consider the following recursive method:             public static int Fun(int x) {                if(x == 0)               //line 1                    return 0              //line 2 else if( x == 1)         //line 3                   return 1;             //line 4                 else                     //line 5                   return (x*Fun(x-1));  //line 5      }   a) Is Fun(4) a valid call? If so, what is the value? If not, explain why?             Answer: b) Is Fun(-4) is a valid call? If so, what is the value? If not, explain why?

EBK JAVA PROGRAMMING
8th Edition
ISBN:9781305480537
Author:FARRELL
Publisher:FARRELL
Chapter3: Using Methods, Classes, And Objects
Section: Chapter Questions
Problem 1RQ
icon
Related questions
Question

Note: Java

  • Consider the following recursive method:

            public static int Fun(int x)

{

               if(x == 0)               //line 1 

                  return 0              //line 2

else if( x == 1)         //line 3

                  return 1;             //line 4 

               else                     //line 5

                  return (x*Fun(x-1));  //line 5

     }

 

a) Is Fun(4) a valid call? If so, what is the value? If not, explain why?

            Answer:

b) Is Fun(-4) is a valid call? If so, what is the value? If not, explain why?

Answer: 

                          (c)  Consider the following method.

      public static int res(int[] list, int first, int last)

     {

       if (first == last)

           return list[first];

       else

           return list[first] + res(list, first + 1, last);

     }

      Given the declaration

      int[] A = {2, 4, 6, 8, 10};

 

      What is the output of the following statement?

  • out.println(res(A, 0, 2));
  • out.println(res(A, 0, 4));

     Answer:

                          (d) Consider the following method,

            public static void Fun(int n) {

    if (n < 2) {

        System.out.println(n);

    } else {

        Fun(n / 2);

        Fun(n % 2);

    }

}

             What is the output of the following statement?

  • out.println(Fun(6));
  • out.println(Fun(13));

    Answer:

           

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT