What will be the output of the following Java program?

class recursion 
    {
        int func (int n) 
        {
            int result;
            result = func (n - 1);
            return result;
        }
    } 
    class Output 
    {
        public static void main(String args[]) 
        {
            recursion obj = new recursion() ;
            System.out.print(obj.func(12));
        }
    }

a) 0
b) 1
c) Compilation Error
d) Runtime Error

1 thought on “What will be the output of the following Java program?”

  1. d
    Explanation: Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in Stack Overflow.
    Output:
    $ javac Output.javac
    $ java Output
    Exception in thread “main” java.lang.StackOverflowError

Leave a Comment