Top 10 java interview programs questions and answers

Write a program of Reverse a string in java 

import java.util.Scanner;
public class ReverseString
{
Public static void main(String[ ] args)
{
String reverse=” “;

System.out.print(“Enter your string=”);

//Scanner is used to take input from user
Scanner sc=new Scanner(System.in);
String name=sc.nextLine( );

Int len=name.length( );

for(int i=len-1;i>=0;i--)
{
reverse=reverse+name.charAt(i);
}
System.out.print(“Reverse string is given=” +reverse);
}
}

Output:

Enter your string= suman

Reverse string is given= namus

Write a program of Binary search in java.

import java.util.Scanner;
public class BinarySearchProgram
{
Public static void main(String[ ] args)
{
int first,last,number,array[ ],searchElement,middle;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number of elements :”);
number=sc.nextInt( );
array=new Int[number];

System.out.println(“Enter” +number “ +numbers”);

for(int i=0;i<number;i++)
{
array[i]=sc.nextInt( );
}

System.out.println(“Enter number as you want to search!”);
searchElement=sc.nextInt( );


first=0;
last=number-1;
middle=(first+last)/2;

while(first<=last)
{
if(array[middle]<searchElement)
{
first=middle+1;
}
else if(array[middle]==searchElement)
{
System.out.println(“Element found at the location=”+(middle+1));
}

else
last=middle-1;

middle=(first+last)/2;

if(first>last)
{
System.out.println(“Element is not found in the given array”);
}
}
}

Output:

Enter the number of elements :

4

Enter 4 numbers

10

4

7

23

Enter number as you want to search!

7

Element found at the location=3

Write  a program of Factorial number using Recursion function in java.

Note:

The factorial of a negative number does not exist.please keep in mind.

public class FactorialNumber
{
Public static void main(String[ ] args)
{
int number=4;
long factorial=multiplyFactorial(number);
System.out.println(“Factorial of “+number +”=”+factorial);

}
Public static long  multiplyFactorial(int num)
{
if(num>=1)
{
return num*multiplyFactorial(num-1);
}
else
{
return 1;
}
}
}

Output:

Factorial of 4=4*3*2*1=24


Write a program of  Fibonacci series using recursion.

Fibonacci series is the series of sum of previous two numbers.

Example

0 1 1 2 3 5 8 ………..

public class  FibonacciSeries
{
Public static void main(String[] args)
{
static in n1=0,n2=1,n3=0;
static void fibonacciSeries(int count)
{
if(count>0)
{
n3=n1+n2;
n1=n2;
n2=n3;
System.out.println(“ “+n3);
fibonacciSeries(count-1);
}
}
Public static void main(String[ ] args)
{
int count=6;
System.out.println(n1+” “+n2); //here printing the 0 and 1
fibonacciSeries((count-2));
}
}

Output:

0 1 1 2 3 5 

Thanks and Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *