Knowee
Questions
Features
Study Tools

Select the correct answerWhat will be the output of the following Java program? class recursion { int func (int n) { int result; if (n == 1) return 1; result = func (n - 1); return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.func(5)); } }Options 12010None of the mentioned

Question

Select the correct answerWhat will be the output of the following Java program? class recursion { int func (int n) { int result; if (n == 1) return 1; result = func (n - 1); return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.func(5)); } }Options 12010None of the mentioned

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is 1.

Here's the step by step explanation:

  1. The function func is a recursive function, which means it calls itself.

  2. The base case for this recursive function is when n equals 1, it returns 1.

  3. If n is not 1, the function calls itself with n - 1.

  4. This process continues until n equals 1.

  5. In the main method, the function func is called with n equals 5.

  6. The function will call itself with n equals 4, then 3, then 2, and finally 1.

  7. When n equals 1, the function returns 1.

  8. Therefore, the output of the program is 1.

This problem has been solved

Similar Questions

Select the correct answerWhat will be the output of the following Java program? class recursion { int fact(int n) { int result; if (n == 1) return 1; result = fact(n - 1) * n; return result; } } class Output { public static void main(String args[]) { recursion obj = new recursion() ; System.out.print(obj.fact(1)); } }Options 120301Runtime Error

Select the correct answer What will be the output of the following Java code?class evaluate {  public static void main(String args[])     {   int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};   int n = 8;      n = arr[arr[n] / 4];   System.out.println(arr[n] / 4);    } }Options1230

the correct answerWhat is Recursion in Java?

What will be the output of the following code ?public static void print(int n){ if(n < 0){ return; } if(n == 0){ System.out.println(n); return; } print(n--); System.out.print(n+" ");}public static void main(String[] args) { int num = 3; print(num);

e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.