Skip to content

Top Java Programming Interview Questions.

interviewQuestion

In This tutorial, I have tried to provide commonly asked Java programming interview questions.

1)Reverse a string using all possible ways?

1)By Using String Buffer

public class ReverseString {

    public static void main(String[] args) {

        StringReverse("automationqahub");

    }
    public static void StringReverse(String str) {

        StringBuffer bf = new StringBuffer(str);
        System.out.println(bf.reverse());


    }

}

2)Converting string to a character array.

public class ReverseString {

    public static void main(String[] args) {

        StringReverse("automationqahub");

    }
    public static void StringReverse(String str) {
        char ar[] = str.toCharArray();
        for (int i = str.length() - 1; i >= 0; i--) {
            System.out.print(ar[i]);
        }

    }

}

3)By Using Recursion

public class ReverseString {

    public static void main(String[] args) {

        StringReverseusingRecursion("automationqahub");
    }
    public static void StringReverseusingRecursion(String str) {
        if ((str == null) || (str.length() <= 1))
            System.out.println(str);
        else {
            System.out.print(str.charAt(str.length() - 1));
            StringReverseusingRecursion(str.substring(0, str.length() - 1));
        }

    }

}

2) WAP to swap 2 numbers without using 3rd variable(Don’t use ‘+’ and ‘-‘ Operator).

1)Using Arithmetic Operators:

public class SwapNumber {

    public static void main(String[] args) {

        SwapNumbers(5, 7);

    }
    public static void SwapNumbers(int a, int b)

    {
        a = a * b;
        b = a / b;
        a = a / b;
        System.out.print("a:" + a + "b:" + b);
    }

}

2)Using Bitwise Operators

public class SwapNumber {

    public static void main(String[] args) {

        SwapNumbers(5, 7);

    }
    public static void SwapNumbers(int a, int b)

    {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.print("a:" + a + "b:" + b);
    }

}

3)WAP to find duplicate elements in an array with and without using the collection.

1)Using Collection

import java.util.HashSet;
import java.util.Set;

public class DuplicateElement {

	public static void main(String[] args) {
		
		int [] numarray= {5,2,4,0,1,2,5,7};
		FindDuplicateElements(numarray);

	}
	public static void FindDuplicateElements(int [] array)
  
	{    
		     
	      Set<Integer> dupnumber=new HashSet<>();
	      for(Integer number : array)
	      {
	    	  if(!dupnumber.add(number))
	    	  {
	    		  System.out.println("Duplicate elem is:"+number);
	    	  }
	      }
		
	}   

}

2)Without using the collection

 public static void dup()    
{
	 int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};   
	 System.out.println("Duplicate elements in given array: ");
         for(int i = 0; i < arr.length; i++) {
          for (int j = i + 1; j < arr.length; j++) {
             if (arr[i] == arr[j])
                 System.out.println(arr[j]);
         }
     }
}  

4)Write a program to count the frequency of elements in a string.


public class FrequencyCount {

    public static void main(String[] args) {
        Findrepeter();
    }


    static void Findrepeter() {
        String str = "aabrakaadaabratest";
        String[] strSplit = str.split("");
        ArrayList < String > strList = new ArrayList < String > (
            Arrays.asList(strSplit));

        Set < String > distinct = new HashSet < > (strList);
        for (String s: distinct) {
            System.out.println(s + ": " + Collections.frequency(strList, s));
        }

    }

}

5) Write a program to remove duplicate elements from an array.



public class RemoveDuplicate {

	public static void main(String[] args) {
		
		int [] numarray= {5,2,4,0,1,1,2,5,7};

		removeDuplicates(numarray);
		 public static void removeDuplicates(int[] a)
	    {
	        LinkedHashSet<Integer> set
	            = new LinkedHashSet<Integer>();
	  
	        
	        for (int i = 0; i < a.length; i++)
	            set.add(a[i]);
	  
	       
	        System.out.print(set);
	    }
	}
	 
}	

6) How to print the Fibonacci series using recursion?

public class ReverseString {

    public static void main(String[] str) {
        int n = 10;
        for (int i = 1; i <= n; i++) {
            System.out.println(fibonacciusingrecursion(i));

        }
    }
    static int fibonacciusingrecursion(int n) {
        if (n == 1)
            return 1;
        else if (n == 0)
            return 0;
        else
            return (fibonacciusingrecursion(n - 1) + fibonacciusingrecursion(n - 2));
    }
}

7)How to print the date in a specific format?

public static void main() {


    String pattern = "MM-dd-yyyy";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

    String date = simpleDateFormat.format(new Date());
    System.out.println(date);

}

8)Write a program to sort ArrayList in descending order.

public class ArrayListDescendingSort {
    public static void main(String args[]) {

        ArrayList < String > arraylist = new ArrayList < String > ();
        arraylist.add("India");
        arraylist.add("Nepal");
        arraylist.add("Newzeland");
        arraylist.add("Bhutan");


        System.out.println("ArrayList Before Sorting:");
        for (String str: arraylist) {
            System.out.println(str);
        }


        Collections.sort(arraylist, Collections.reverseOrder());


        System.out.println("ArrayList in descending order:");
        for (String str: arraylist) {
            System.out.println(str);
        }
    }
}

9)How to sort HashSet in java?

public static void sortSet() {

    HashSet < String > set = new HashSet < String > ();
    set.add("test");
    set.add("practice");
    set.add("contribute");
    set.add("ide");
    set.add("java");

    System.out.println("Original HashSet: " +
        set);

    TreeSet < String > treeSet = new TreeSet < String > (set);

    // Print the sorted elements of the HashSet
    System.out.println("HashSet elements " +
        "in sorted order " +
        "using TreeSet: " +
        treeSet);
}

10)How to left rotate array by one position?

public class RotateArray {

    public static void main(String[] args) {

        int[] numarray = {
            7,
            5,
            1,
            0,
            9
        };

        LeftRotate(numarray);
        public static void LeftRotate(int[] array) {
            int temp = array[0];
            for (int i = 1; i < array.length; i++) {
                array[i - 1] = array[i];
            }
            array[array.length - 1] = temp;
            for (int i: array) {
                System.out.print(i);
            }

        }
    }

For More coding questions visit here.