Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Recursion' - 11 question(s) found - Order By Newest | ||||
| ||||
Ans. Function call allocates a stackframe in stack. Every stackframe will use some memory to store local variables, parameters and to remember return address. Without terminating condition stackframes will keep consuming memory from stack and eventually program will result in stackoverflow error. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class BuggyBread { public static void main(String args[]) { System.out.println(factorial(5)); } private static int factorial(int number){ if(number == 1){ return 1; } else { return number * factorial(number - 1); } } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. public class Factorial { public static void main(String[] args){ int x = 5; System.out.println(calculateFactorial(x)); } private static int calculateFactorial(int x){ if(x==1){ return 1; } return x * calculateFactorial(x-1); } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Please not that all such questions can be easily answered through recursion. Simple recursive implementation could be traverse(root); void traverse(Element element){ if(element.hasNext()){ traverse(element.next()); } else { System.out.println(element); } } but this algo / code lead to endless loop if there is a loop in graph traversal. So you can keep a collection to keep track of which elements have laready been traversed static List<Elements> listOfAlreadyTraversedElements = new ArrayList<Elements>(); main(){ traverse(root); } void traverse(Element element){ if(element.hasNext()){ traverse(element.next()); } else { listOfAlreadyTraversedElements.add(element); System.out.println(element); } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. public class BuggyBread { public static void main(String args[]) { System.out.println(sum(2,10)); } private static int sum(int start,int end){ if(start > end){ return 0; } else { return start + sum(start+1,end); } } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class BuggyBread { public static void main(String args[]) { System.out.println(multiply(2,5)); } private static int multiply(int start,int end){ if(start > end){ return 1; } else { return start * multiply(start+1,end); } } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class BuggyBread { public static void main(String args[]) { displayNumbersBetween(2,10); } private static void displayNumbersBetween(int start,int end){ if(start > end){ return ; } else { System.out.println(start); displayNumbersBetween(start+1,end); } } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class BuggyBread { public static void main(String args[]) { displayNumbersBetween(5,7); } private static void displayNumbersBetween(int start,int end){ if(start > end){ return ; } else { for(int x=1;x<=start;x++){ System.out.print("*"); } System.out.println(""); displayNumbersBetween(start+1,end); } } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. It would result in endless function calls and hence eventually would result in stackoverflow exception. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Any function call is slower than the code in the same method as Java has to maintain stack of meta and function call information. So recursion is slower. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public class FibonacciUsingRecursion { public static void main(String[] args) throws IOException{ getNextFibanocci(1,1); } static int getNextFibanocci(int a,int b){ if(a+b >= 100){ return 0; } else { System.out.println(a+b); getNextFibanocci(b,a+b); } return 0; } } | ||||
![]() | ||||
![]() ![]() ![]() | ||||