In this post we are discussing about Commonly asked programs in java interview. We all know that mostly programs are asked in java interview to judge your working experience. Below are few Commonly asked programs in java interview.
List Programs in Java Interview :
1. Permutaion of word
for ex ABC will be abc , acb,bac,bca,cab,cba
Program:
public class Permutation {
public static void main(String[] args) {
String s=”abc”;
List<String>combinations=new ArrayList<String>();
combinations=permutations(s);
System.out.println(combinations);
}
private static List<String> permutations(String s) {
// TODO Auto-generated method stub
List<String>combinations=new ArrayList<String>();
System.out.println(s.length());
if(s.length()==1){
combinations.add(s);
}
else{
System.out.println(“Else part”);
for(int i=0;i<s.length();i++){
List<String>temp=permutations(s.substring(0, i)+s.substring(i+1));
for (String string : temp) {
combinations.add(s.charAt(i)+string);
System.out.println(“s.charAt(i)+string : “+s.charAt(i)+string);
}
}
}
return combinations;
}
}
2. Reverse Array without additional array
public class ReverseWithoutAdditionalArray {
static void reverseArray(int inputArray[])
{ System.out.println(“Array Before Reverse : “+Arrays.toString(inputArray));
int temp;
for (int i = 0; i < inputArray.length/2; i++)
{
temp = inputArray[i];
inputArray[i] = inputArray[inputArray.length-1-i];
inputArray[inputArray.length-1-i] = temp;
}
System.out.println(“Array After Reverse : “+Arrays.toString(inputArray));
System.out.println(“=========================================”);
}
public static void main(String[] args)
{
reverseArray(new int[]{4, 5, 11, 9, 10})
}
}
3. Separate zeros from arrays
public class SeparateZero {
public static void main(String[] args) {
int arr[] = {1,2,3,0,5,0,7,8,0};
int newArr[]=new int[arr.length];
int count=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i] != 0)
{
newArr[count]=arr[i];
count++;
}
}
while(count < arr.length)
{
newArr[count]=0;
count++;
}
System.out.println(Arrays.toString(newArr));
}
}
4. Find unique elements from arrays
Unique elements
for(int i=0;i<arr.length-1;i++)
{ int count =0;
for(int j=1;j<=i;j++)
{
if(arr[i] == arr[j])
{
count++;
}
}
if(count != 1)
{System.out.println(“Duplicate”);
System.out.println(arr[i]);
}
else
{
System.out.println(“Unique”);
System.out.println(arr[i]);
}
}
5. Add or remove elements from array
int[] my_array = { 25, 14, 56 ,34,12,76};
// Insert an element in 3rd position of the array (index->2, value->5)
//Adding value
int Index_position = 2;
int newValue = 5;
System.out.println(“Original Array : ” + Arrays.toString(my_array));
for (int i = my_array.length-1; i > Index_position; i–) {
my_array[i] = my_array[i-1];
}
my_array[Index_position] = newValue;
System.out.println(“New Array: ” + Arrays.toString(my_array));
//Removing value from array
int [] arr= {5,6,7,8};
int [] newArr=new int[arr.length-1];
int j=6;
int k=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i] != j)
{
newArr[k]=arr[i];
k++;
}
}
System.out.println(Arrays.toString(newArr));
6. Find missing number
public static void main(String[] args) {
int num[] = { 1, 2, 3, 4, 5, 6, 7, 8, 10 };
int length = num.length;
int nums = length + 1;
int sumno = sumOfNnumbers(nums);
int toatal = sumOfTotalNumber(num);
int missing = toatal – sumno;
System.out.println(“Missing number ” + missing * (-1));
}
static int sumOfNnumbers(int n) {
int sum = 0;
for (int i = 0; i <= n; i++) {
sum = sum + i;
}
return sum;
}
static int sumOfTotalNumber(int arr[]) {
int sums = 0;
for (int i = 0; i < arr.length; i++) {
sums = sums + arr[i];
}
return sums;
}
}
7. Find Max number from array
for(int i=0;i<arr.length-1;i++)
{
if(arr[i] > max )
{
max=arr[i];
}
}
System.out.println(“Maximum elements :”+max);
for(int i=0;i<arr.length-1;i++)
{
if(arr[i] < min )
{
min=arr[i];
}
}
String:
package in.com;
/*
* with predefined function
* char[] ArrayS1 = s1.toLowerCase().toCharArray();
char[] ArrayS2 = s2.toLowerCase().toCharArray();
Arrays.sort(ArrayS1);
Arrays.sort(ArrayS2);
status = Arrays.equals(ArrayS1, ArrayS2);
*
*/
public class AnagromString {
public static void main(String[] args) {
String str1=”Shubham chivhane”;
String Str2=”chibham Shumhaneqw”;
String s1=str1.replaceAll(“\\s”, ” “);
String s2=Str2.replaceAll(“\\s”, ” “);
if(s1.length() == s2.length())
{
char a1[]=s1.toLowerCase().toCharArray();
char a2[]=s2.toLowerCase().toCharArray();
for(int i=0;i<a1.length;i++)
{
for(int j=i+1;j<a1.length;j++)
{
char tmp;
if(a1[i] > a1[j])
{
tmp=a1[i];
a1[i]=a1[j];
a1[j]=tmp;
}
}
}
for(int i=0;i<a2.length;i++)
{
for(int j=i+1;j<a2.length;j++)
{
char tmp;
if(a2[i] > a2[j])
{
tmp=a2[i];
a2[i]=a2[j];
a2[j]=tmp;
}
}
}
if(a1.equals(a2)) {
System.out.println(“Anagrom”);
}
else
{
System.out.println(“Not Anagrom”);
}
}
else
{
System.out.println(“Not Anagrom”);
}
}
}
8. Find Number of words in string
public class NumberOfwords {
public static void main(String[] args) {
String str=”aa bb ccc dd ee”;
String [] word=str.split(” “);
System.out.println(“No of word “+ word.length);
}
}————————————————
9. Find Number of perticular words in string
public class NumberOfwords2 {
public static void main(String[] args) {
String str=”abcf ssd sds “;
char str1=’s’;
int count=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)== str1)
{
count++;
}
}
System.out.println(count);
}
10. Reverse String and Array
public class ReverseStringAndArray {
public static void main(String[] args) {
int arr[] = {10,20,30,40,50};
int newArr[]= new int[arr.length];
System.out.println(“before reverse Integer Array: “+Arrays.toString(newArr));
int j=0;
for(int i = arr.length -1;i>=0;i–)
{
//System.out.println(arr[i]);
newArr[j] = arr[i];
// System.out.println(newArr[i]);
j++;
}
System.out.println(“reverse Integer Array: “+Arrays.toString(newArr));
String str=”SHUBHAM”;
char a[]=new char[str.length()];
int k=0;
for(int i=str.length()-1;i>=0;i–)
{
a[k]=str.charAt(i);
k++;
}
System.out.println(“String before reverse: “+str);
System.out.println(“reverse string: “+String. valueOf(a));
}
}
11. find word and their count
public class Test {
public static void main(String[] args) {
String str=”Bread butter and bread”;
String[] words=str.split(” “);
HashMap<String, Integer> hm = new HashMap<>();
for(String word : words)
{
if(hm.containsKey(word))
{
hm.put(word.toLowerCase(), hm.get(word)+1);
}
else
{
hm.put(word.toLowerCase(), 1);
}
}
for(Entry<String, Integer> entry : hm.entrySet())
{
System.out.println(entry.getKey() +” “+entry.getValue());
//if we want duplicate key then we can use it
// if(entry.getValue() > 1)
// {
// System.out.println(“duplicate key only”);
// System.out.println(entry.getKey() +” “+entry.getValue());
// }
}
}
}
Related Posts:
TOP 200+ JAVA Interview Questions And Answers
TOP 50+ Cassandra Interview Questions And Answers
TOP 50+ DBMS Interview Questions – 2022
Mostly Asked PHP Interview Questions And Answers
[UPDATED 2022] CouchDB Interview Questions And Answers
Mostly Asked Active Directory Interview Questions
[UPDATED] Django Interview Questions And Answers
Angular Interview Questions and Answers
Oracle Tuxedo Interview Questions and Answers
Top Answers to OBIEE Interview Questions And Answers
Top Data Warehouse Interview Questions And Answers
Top Interview Questions And Answers of ETL ( Etract, Trasform , Load)