Core Java - Interview Questions and Answers for 'Recursion]' | Search Interview Question - javasearch.buggybread.com # Search Interview Questions More than 3000 questions in repository. There are more than 900 unanswered questions. Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text Submit Question

Core Java - Interview Questions and Answers for 'Recursion]' - 7 question(s) found - Order By Newest Q1. Write a Program to print factorial of a number using recursion Core Java
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);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve recursion Asked in 25 Companies Q2. Write a program to calculate factorial of a number using recursion Core Java
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);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve factorial  calculate factorial  code  coding  recursion Asked in 1 Companies basic Q3. Write an Algorithm for Graph Traversal ? The Graph has a loop. Algorithm
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 {
System.out.println(element);
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve graph traversal algorithm  graph traversal algorithm using recursion Asked in 1 Companies intermediate Q4. Write a program to print sum of numbers between the start and end number For example - Passing start number as 2 and end number as 10, it should print 2+3+4+5+6+7+8+9+10 = 54 Core Java
Ans.
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);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve recursion Q5. Write a program using Recursion to print multiplication of numbers between the start and end number For example - Passing start number as 2 and end number as 10, it should print 2*3*4*5*6*7*8*9*10 = 3628800 Core Java
Ans.
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);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve recursion Q6. Which of the two - iteration or recursion - is slower ? Core Java
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. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve iteration  recursion  iteration vs recursion Q7. Write a Program to print fibonacci series using recursion. Core Java
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;
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve fibonacci  recursion

## Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.