Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The output of the given Java program will be 1.

Here's the step by step explanation:

  1. The main method in the Output class creates an object of the recursion class and calls the fact method with the argument 1.

  2. The fact method in the recursion class checks if the input number n is equal to 1. If it is, it returns 1.

  3. Since the input number is 1, the method returns 1 without going into the recursion.

  4. The print statement in the main method prints the returned value, which is 1.

So, 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 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

the correct answerWhat is Recursion in Java?

Select the correct answerWhat will be the output of the following Java code?  class Codetantra  {    final public int output(int x, int y) { return 1; }   }   class Program extends Codetantra   {     public int output(int x, int y) { return 2; }   }    public class result   {    public static void main(String args[])     {       Program object = new Program();       System.out.print("y is " + y.output(0, 1));      }   }Optionsy is : 0y is : 1Compilation ErrorAn exception is thrown at runtime

Select the correct answerWhat will be the output of the following Java program?class java   {    public static void main(String args[])    {      int var1 = 3;       int var2 = 8;      if ((var2 = 2) == var1)        System.out.print(var2);      else         System.out.print(++var2);    }   }Options4213

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);

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.