Top 10 most common Java Interview Programming Questions

In almost all interview these programming questions are asking by interviewer.

 

1. Check Number is Prime number or not.

package practice.Number;

public class PrimeNumber {
	public static void main(String Ars[]) {
		int i, m = 0, flag = 0;
		int n = 5;
		m = n / 2;
		if (n == 0 || n == 1) {
			System.out.println(n + " " + "Not prime number");
		} else {
			for (i = 2; i <= m; i++) {
				if (n % i == 0) {
					System.out.println(n + " " + "Not prime number");
					flag = 1;
					break;
				}
			}
			if (flag == 0) {
				System.out.println(n + " " + "is prime number");
			}
		}
	}
}

 

2. Reverse number in java.


package practice.Number; public class ReverceNumber { public static void main(String[] args) { int n = 123; int r = 0; while (n > 0) { int m = n % 10; n = n / 10; r = r * 10 + m; } System.out.println("Reverce number of number is " + r); } }

 

3. Remove Duplicates number.


package practice.Number; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class RemovedDuplicates { public static void main(String[] args) { List<Integer> al= new ArrayList<Integer>(); al.add(1); al.add(2); al.add(3); al.add(3); al.add(4); al.add(5); System.out.println(al); Set<Integer> hs= new HashSet<Integer>(al); System.out.println(hs); } }

 

4. Check Palindrome Number.


package practice.Number; public class PalindromeNumber { public static void main(String Arg[]) { int n = 121; int temp = n; int sum = 0; while (n > 0) { int m = n % 10; n = n / 10; sum = (sum * 10 )+ m; } if (sum == temp) { System.out.println(sum + " number is palindrome"); } else { System.out.println(sum + " number is not palindrome"); } } }

 

5. Missing Number from Array.


package practice.Number; public class MissingNumber { static int getMissingNumber(int num[], int n) { int i, total; total = (n + 1) * (n + 2) / 2; for (i = 0; i < n; i++) { total -= num[i]; } return total; } public static void main(String[] args) { int num[] = {1, 2, 3, 4, 5, 6, 8, 9 }; int miss = getMissingNumber(num, num.length); System.out.println(miss); } }

 

6.Fibonacci Series.

Fibonacci series without Recursion.


package practice.Number; public class FibonacciSeries { public static void main(String Arg[]) { int n=10, n1=0, n2=1,n3; System.out.print(n1+" "+n2); for(int i=2;i<n;i++) { n3=n1+n2; System.out.print(" "+ n3); n1=n2; n2=n3; } } }

 

Fibonacci series with Recursion.


package practice.Number; public class FibonacciSeriesRecursion { static int i, n1=0,n2=1,n3; static void printFibonaicc(int n) { if(n>0) { n3= n1+n2; n1=n2; n2=n3; System.out.print(" "+n3); printFibonaicc(n-1); } } public static void main(String Ags[] ) { int n=10; System.out.print(n1+" "+n2); printFibonaicc(n-2); } }

 

7. Check Armstrong Number.


package practice.Number; public class Armstrongnumber { public static void main(String[] args) { int n = 153; int s = 0, temp = 153; while (n > 0) { int m = n % 10; n = n / 10; s = s + (m * m * m); } if (temp == s) { System.out.println("Number is Armstrong"); } else { System.out.println("Number is not Armstrong"); } } }

 

8. Factorial Number.


package practice.Number; public class Factorial { public static void main(String[] args) { int n=5; int f = 1; while(n>0) { f=(f*n); n--; } System.out.println("Factorial of n : "+f); } }

 

9. Square Root of Number.


package practice.Number; public class SquareRoot { public static void main(String[] args) { Double num = 4.0; int n = 4; System.out.println("SquareRoot of number is : " + Math.sqrt(num)); System.out.println("SquareRoot of number is : " + Math.pow(num, 2)); System.out.println("SquareRoot of number is : " + Math.pow(num, 3)); for (int i = 1; i <= n; i++) { int reminder = n % i; if (reminder == 0 && (n / i) == i) {//if reminder is 0 and number divided by i is same i than number is square root System.out.println("The Square root of " + n + " is = " + i); } } } }

 

10. Print duplicate element.


package practice.Number; public class DuplicateElement { public static void main(String Arg[]) { int nums[] = { 1, 2, 3, 4, 5, 5, 6, 7 }; int l = nums.length; for (int i = 0; i < l; i++) { for (int j = i + 1; j < l; j++) { if (nums[i] == nums[j]) { System.out.println(nums[i]); } } } } }

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

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