Java - Interview Questions and Answers for 'Recursion' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Recursion' - 12 question(s) found - Order By Newest

Very frequently asked in HCL Tech ( Based of 4 inputs )
  Q1. Write a Program to reverse a string iteratively and recursivelyCore Java

Ans. Using String method -

new StringBuffer(str).reverse().toString();

Iterative -

Strategy - Loop through each character of a String from last to first and append the character to StringBuilder / StringBuffer

public static String getReverseString(String str){
StringBuffer strBuffer = new StringBuffer(str.length);
for(int counter=str.length -1 ; counter>=0;counter--){
strBuffer.append(str.charAt(counter));
}
return strBuffer;
}

Recursive -

Strategy - Call the method with substring starting from 2nd character recursively till we have just 1 character.

public static String getReverseString(String str){
if(str.length <= 1){
return str;
}
return (getReverseString(str.subString(1)) + str.charAt(0);
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     string  StringBuffer  recursion  for loop  stringbuffer.append  java.lang.String  java.lang.StringBuffer  String Manipulation     HCL Technologies  Ocado  BlackRock  Oracle  FINRA  Arezzo Sky  Zalando  Boeing        frequent


 Q2. What will happen if we don't have termination statement in recursion ?
Manoj
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.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion   break statement  break  stackoverflow exception  stackoverflow  memory management  memory  exceptions


 Q3. Write a program to calculate factorial of a number using recursionCore 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     Mahindra Comviva      basic


 Q4. Write an Algorithm for Graph Traversal ? The Graph has a loop.Algorithm2016-11-30 15:42:32

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

 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     Overstock.com      intermediate


 Q5. Write a Program to print factorial of a number using recursionCore Java2017-01-29 17:47:20

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

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion     PrimeFocus Technologies  Aavanor System  Mobicip  CMSS Private Limited  Intrepid Pursuits  Indus Valley Partners  Sunquest Information Systems  Linchpin  Riverbed Technology  Knoldus Software  Sopra Steria  Sunquest Information Systems  OpenBet  DMX Technologies  Whitesnow Software Consultancy  NextSphere  Clearwater Analytics  Obelisk Infosystems  iNautix Technologies  Andesa Services  NextSphere  MobileIron  Wolfram Research  Spillman Technologies


 Q6. 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 Java2017-01-29 17:52:10

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

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion


 Q7. 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 Java2017-01-29 17:54:47

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

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion


 Q8. Write a Program to display numbers between 2 numbers using recursionCore Java2017-01-29 18:08:13

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

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion  code  coding


 Q9. Write a Program to print asterix like following using Recursion

**
***
****
*****
******
*******
********
*********
**********

Program should set the start and end index and then display this accordingly. For example the above pattern is for displayNumbersBetween(2,10) and following is for displayNumbersBetween(5,7)

*****
******
*******
Core Java2017-01-29 18:12:47

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

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion  code  coding



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. What will happen if we don't have termination statement in recursion ?Core Java2017-04-18 20:13:15

Ans. It would result in endless function calls and hence eventually would result in stackoverflow exception.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     recursion  stackoverflowexception      Basic        frequent


 Q11. Which of the two - iteration or recursion - is slower ?Core Java2017-08-10 17:03:28

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


 Q12. Write a Program to print fibonacci series using recursion.Core Java2017-08-24 14:41:45

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




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: