Top 15 Java Programmes for Interview
It's very essential to crack MCQ test along with the programming test in Java interview . Interviewer usually asked basic logical program. They are checking your coding skill but in term of logical. They asked basic program. Let's started without wasting time find below programmes which are mostly asked Interview Programmes in Java interview.
1. Prime Number : Write a Program to find the given number is prime or not .
This is very frequently program asked in interview room. Before writing a program for that you should know definition of the prime number. A Number which is only divisible by 1 and its self is called as prime number. e.g. 3, 5,7, 11 etc.
1. Program to check Given Number is Prime or Not.
import java.util.Scanner;
public class javaprogram
{
public static void main(String args[])
{
int i;
boolean flag = true;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Number : ");
int num = scan.nextInt();
// Reduce time complexity using==>; for(i=2; i<= num /2; i++)
for(i=2; i< num ; i++)
{
if (num % i == 0)
{
flag = false;
break;
}
}
if(flag)
{
System.out.print("This "+num+" is a Prime Number");
}
else
{
System.out.print("This "+num+" is not a Prime Number");
}
}
}
public class javaprogram
{
public static void main(String args[])
{
int i;
boolean flag = true;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Number : ");
int num = scan.nextInt();
// Reduce time complexity using==>; for(i=2; i<= num /2; i++)
for(i=2; i< num ; i++)
{
{
flag = false;
break;
}
}
if(flag)
{
System.out.print("This "+num+" is a Prime Number");
}
else
{
System.out.print("This "+num+" is not a Prime Number");
}
}
}
2. Reverse String : Write a Program to print given String in reverse.
This is another program commonly asked in java interview.
// Program for Reverse String.
import java.util.Scanner;
public class javaprogram
{
public static void main(String[] args)
{
System.out.println("Enter string to reverse:");
Scanner read = new Scanner(System.in);
String str = read.nextLine();
String reverse = "";
for(int i = str.length() - 1; i >= 0; i--)
{
reverse = reverse + str.charAt(i);
}
System.out.println("Reversed string is:");
System.out.println(reverse);
}
}
public class javaprogram
{
public static void main(String[] args)
{
System.out.println("Enter string to reverse:");
Scanner read = new Scanner(System.in);
String str = read.nextLine();
String reverse = "";
for(int i = str.length() - 1; i >= 0; i--)
{
reverse = reverse + str.charAt(i);
}
System.out.println("Reversed string is:");
System.out.println(reverse);
}
}
3. Reverse String : Without any library and indexing function.
When interviewer asked for reverse String you can easily write a program using Library method or indexing method but on same time he asked for same programme without use of any indexing function. There are so many way of reverse the String but we can see one of them and simplest to understand.
Here we can swap the first char(element) to last one, then 2nd char to 2nd last one, and so on up to the middle of the string.
Here we can swap the first char(element) to last one, then 2nd char to 2nd last one, and so on up to the middle of the string.
// Program for Reverse String without using function.
public class RevString
{
public static void main(String args[])
{
Scanner scn=new Scanner(System.in);
System.out.println("Enter the String");
String str=scn.nextLine();
char []rev=str.toCharArray();
int j=str.length()-1;
for(int i=0;i<=(rev.length)/2;i++)
{
char temp=rev[i];
rev[i]=rev[j];
rev[j]=temp;
j--;
}
System.out.println(new String(rev));
}
}
{
public static void main(String args[])
{
Scanner scn=new Scanner(System.in);
System.out.println("Enter the String");
String str=scn.nextLine();
char []rev=str.toCharArray();
int j=str.length()-1;
for(int i=0;i<=(rev.length)/2;i++)
{
char temp=rev[i];
rev[i]=rev[j];
rev[j]=temp;
j--;
}
System.out.println(new String(rev));
}
}
4. Bubble Sort : Write a Program to bubble sort.
Bubble sort is simple algorithm among the other algorithm of sorting the elements. In this sort algorithm first element is compare to the second element and if first element is greater than second then swap between these two element take place, loop the same step to next element util the condition is true. Repeated the above step to nth number elements or time.
// Program for Bubble Sort.
public class BubbleSort
{
public static void main(String[] args)
{
int arr[] = { 1, 5, 2, -2, -4, -1, 8, 0, -5, 9, 4,11 };
System.out.println("Elements Before Bubble Sort : ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Elements After Bubble Sort : ");
for (int i = 0; i< arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
static void bubbleSort(int[] arr)
{
int temp = 0;
int n = arr.length;
for (int i = 0; i< n; i++)
{
for (int j = 0; j < (n - i)-1; j++)
{
if (arr[j]> arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
{
public static void main(String[] args)
{
int arr[] = { 1, 5, 2, -2, -4, -1, 8, 0, -5, 9, 4,11 };
System.out.println("Elements Before Bubble Sort : ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Elements After Bubble Sort : ");
for (int i = 0; i< arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
static void bubbleSort(int[] arr)
{
int temp = 0;
int n = arr.length;
for (int i = 0; i< n; i++)
{
for (int j = 0; j < (n - i)-1; j++)
{
if (arr[j]> arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
5. Unique Elements from array : Write a Program to print unique or distinct elements from an array.
Program in java interview.
// Program for Unique Elements from an array
public class UniqueArray
{
public static void main(String[] args)
{
int uniarr[]=new int[] {1,2,3,22,3,4,5,23,3,5,7,4,5,6,9};
for(int i=0;i {
boolean flag=false;
for(int j=0;j {
if(i!=j &&uniarr[i]==uniarr[j])
{
flag=true;
break;
}
}
if(!flag)
{
System.out.print(uniarr[i]+" ");
}
}
}
}
{
public static void main(String[] args)
{
int uniarr[]=new int[] {1,2,3,22,3,4,5,23,3,5,7,4,5,6,9};
for(int i=0;i
boolean flag=false;
for(int j=0;j
if(i!=j &&uniarr[i]==uniarr[j])
{
flag=true;
break;
}
}
if(!flag)
{
System.out.print(uniarr[i]+" ");
}
}
}
}
6. Duplicate elements from list : Write a Program to find duplicate elements from list .
Program in java interview.
// Program for Duplicate elements from list.
import java.util.ArrayList;
import java.util.HashSet;
public class UniqueArray
{
public static void main(String[] args)
{
ArrayList ls=new ArrayList<>():
ls.add(1);
ls.add(2);
ls.add(3);
ls.add(2);
ls.add(4);
ls.add(5);
ls.add(4);
ls.add(9);
HashSet lset=new HashSet();
for(Integer i:ls)
{
if(!lset.add(i))
System.out.println(i.toString());
}
}
}
import java.util.HashSet;
public class UniqueArray
{
public static void main(String[] args)
{
ArrayList
ls.add(1);
ls.add(2);
ls.add(3);
ls.add(2);
ls.add(4);
ls.add(5);
ls.add(4);
ls.add(9);
HashSet lset=new HashSet();
for(Integer i:ls)
{
if(!lset.add(i))
System.out.println(i.toString());
}
}
}