Pages

Saturday, 1 October 2022

What is Recursion in Java? Program to print first five characters using recursive method in Java?

 


SDET Interview Question and Answers.  

Java Interview Question SET-01

Java INterview Question Set-02 ARRAY

Java INterview Questiona Set-03 MAP

Java INterview Question Set-04

Java is a very important topic for both developers and testers. It's very difficult to remember all the Java basics, so it's always wise to do a revision before the day of the interview. 

What is Recursion in Java?


Recursion works in Java the same way it works everywhere else. Recursion is, fundamentally, the process of solving a problem by reducing it to a smaller version of itself.

Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.


In Java, a method that calls itself is known as a recursive method. This process is also known as recursion. A physical world example would be to place two parallel mirrors facing each other. Any object in between them will be reflected recursively.


Appium Interview Questions and Answers

Selenium Interview Questions and answers

GIT Interview Questions and Answers


Why do we need Recursion?

Recursion is made for solving problems that can be broken down into smaller, repetitive problems. It is especially good for working on things that have many possible branches and are too complex for an iterative approach . One good example of this would be searching through a file system


  1. import java.util.Scanner;
  2. public class recursion
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n = 5;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter any number:");
  9.         n = s.nextInt();
  10.         recursion obj = new recursion();
  11.         System.out.print("recursion numbers till "+n+" :");
  12.         obj. recursion(n,1);
  13.  
  14.  
  15.     }
  16.     int recursion(int y, int i)
  17.     {
  18.         if(i <= y)
  19.         {
  20.             System.out.print(i+" ");
  21.             return(recursion(y,++i));
  22.         }
  23.         return 1;
  24.     }
  25. }

 You can also find factorials using Recursion, very important for interview preparation:

Jenkins Interview Questions and Answers

TYPES of Recursion

There are several different recursion types and terms. These include:

  • Direct recursion: This is typified by the factorial implementation where the methods call itself.
  • Mutual recursion: This happens where one method, say method A, calls another method B, which then calls method A. This involves two or more methods that eventually create a circular call sequence.
  • Multi-recursion: Multiple recursive calls are made in the method.
  • Head recursion: The recursive call is made at the beginning of the method.
  • Tail recursion: The recursive call is the last statement.

Factorial of a Number Using Recursion

class Factorial {

    static int factorial( int n ) {
        if (n != 0)  // termination condition
            return n * factorial(n-1); // recursive call
        else
            return 1;
    }

    public static void main(String[] args) {
        int number = 4, result;
        result = factorial(number);
        System.out.println(number + " factorial = " + result);
    }
}

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

✍️AUTHORLinkedIn Profile

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

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employee and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33


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

SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python


 

No comments:

Post a Comment