What is output of below Program?

public class RecursionDemo
{
public static void main(String args[])
{
main(new String[5]);
}
}

1.Compile Error 2.Runs continuously forever 3.Exception 4.None

Solution:
The program compiles, and during execution, StackOverFlow error occurs, because whenever a method is invoked, an entry is created on stack, hence continuously a method invokes itself, and entries keep on getting added to stack.A method calling itself is called as Recursion. Due to this Stack get filled up, and StackOverFlowError exception occurs. Since this is Run time exception, this need not be handled with try catch blocks.

You may also like to read: Exception handling

Leave a Reply