__As soon as you write __* Selenium Java* in your resume, interviewer is bound to give you a sheet of paper & ask write a

*. Though*

**Java program***Selenium*doesn’t require extensive Java knowledge but still there is a set of basic

*Java programs*that you should be able to write on-the-spot. This article covers five of the most popular

*questions frequently asked in the technical interview. The questions are easy, but don’t forget to mark these java-program in your to do list before attending any technical interview for*

**Write a Java program***Selenium automation testing*.

__Recursion in programming__

__Recursion in programming__

* Recursion *is a programming technique that allows the programmer to express operations in terms of themselves, i.e. a function that calls itself is known as a recursive function. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to “repeat the process”.

**Write a Java program to calculate Factorial of a number**

In mathematics, the *factorial* of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5 * 4 * 3 * 2 * 1 = 120

The value of 0! is 1, according to the convention for an empty product.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package Java; import java.util.Scanner; public class Factorial { public static int Factorial1(int a){ return (a==0 || a==1) ? 1 : (a*Factorial1(a-1)); } public static void main(String args[]){ int n; Scanner in = new Scanner(System.in); System.out.println("Enter a number: "); n = in.nextInt(); System.out.println("Factorial of "+n+" is: "+Factorial1(n)); in.close(); } } |

Use of Recursion:

1 |
return (a==0 || a==1) ? 1 : (a*Factorial1(a-1)); |

**Write a Java program to print the Fibonacci series**

In mathematics, the *Fibonacci numbers* are the numbers in the following integer sequence, called the *Fibonacci sequence*, and characterized by the fact that every number after the first two is the sum of the two preceding ones:

*1, 1, 2, 3, 5, 8, 13, 21, 34, 55…*

By definition, the first two numbers in the *Fibonacci sequence* are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package Java; import java.util.Scanner; public class Fibonacci { public static int Fib(int a){ return (a<=1) ? a : Fib(a-1)+Fib(a-2); } public static void main(String args[]){ int n; Scanner in = new Scanner(System.in); System.out.println("Enter a number: "); n = in.nextInt(); for(int i=0;i<=n-1;i++) System.out.print(Fib(i)+" "); in.close(); } } |

Use of Recursion:

1 |
return (a<=1) ? a : Fib(a-1)+Fib(a-2); |

**Write a Java program to calculate power ‘n’ of a number**

This is simple,

*2 power 5 = 2 * 2 * 2 * 2 * 2 = 32*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package Java; import java.util.Scanner; public class PowerN { public static int Power(int x,int n){ if(n==0) return 1; else if(n==1) return x; else{ return x*Power(x,n-1); } } public static void main(String args[]){ int x,n; Scanner in = new Scanner(System.in); System.out.println("Enter a number: "); x = in.nextInt(); System.out.println("Enter the power: "); n = in.nextInt(); System.out.println("Number "+x+" to the power "+n+" is: "+Power(x,n)); in.close(); } } |

Use of Recursion:

1 |
return x*Power(x,n-1); |

**Write a Java program to Reverse a String**

Although there are many ways to get the solution but we are sharing 2 different ways to *reverse a string*. First we will understand the question,

*Input: Deepanshu Agarwal | Output: lawragA uhsnapeeD*

*Input: Software Testing Studio | Output: oidutS gnitseT erawtfoS*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package Java; import java.util.Scanner; public class ReverseString { public static String Reverse1(String a){ String reverse = ""; int len = a.length(); for (int i = len - 1 ; i >= 0 ; i-- ) reverse += a.charAt(i); return reverse; } public static StringBuffer Reverse2(String a){ StringBuffer sb = new StringBuffer(a); return sb.reverse(); } public static void main(String args[]){ String original; Scanner in = new Scanner(System.in); System.out.println("Enter a string to reverse: "); original = in.nextLine(); System.out.println("Reverse of entered string is: "+Reverse1(original)); System.out.println("Reverse of entered string is: "+Reverse2(original)); in.close(); } } |

This java program reverses a string entered by the user. We use,

**charAt**method to extract characters from the string and append them in reverse order to reverse the entered string**StringBuffer**class method

**Write a Java program to calculate ‘Digit Sum’**

You’re given an integer N. Write a Java program to calculate the sum of all the digits of N. In mathematics, the *digit sum* of a given integer is the sum of all its digits (e.g. the *digit sum* of 84001 is calculated as 8+4+0+0+1 = 13).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package Java; import java.util.Scanner; public class SumofDigits { public static int sum(int n){ return n==0 ? 0 : n%10+sum(n/10); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number: "); int num = sc.nextInt(); sc.close(); System.out.println("Sum of digits of "+num + " is: " + sum(num)); } } |

Hope these five programs will help you kick-start the Java-program journey. All the Best 🙂

Hi will share string programs in java for interview

How many objects will be created If two Strings are Created This way

String S1 = “beyond_corner”;

String S2 = “beyond_corner”;

only one object is created, because s1 and s2 are string literals and having the same content object reference will be shared by them in the string pool.