Common Java interviewing questions

 

In this article, we are going to discuss the top 10 very important programming interviewing questions which are almost all interviews are asking by the interviewer.

1. Check Number is Prime number or not.

These are very important interviewing questions that are asking at both fresher level and experience level.

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.

This is programming question is a very common question for every interview,  using this equation interviewer is checking your logical concept.

Step 1:- First we need to check number is greater than 0 or not in the while loop.

Step 2:- If the number is greater than 0 then we need to find a mode of that number.

Step 3:- Then we need to divide the number by 10.

Step 4:- After that, we need to store that number(r) in a global variable and we need to multiple r to 10 and add that mode of number, this process we need to do until the number will become zero.


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 numbers.

If we want to remove a duplicate number without any other operation than, we need to simply set that list of numbers into a set, so the set will internally useing the sorting technique.


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.

Find palindrome number is the same as finding reverse numbers, which I already explain in the reverse programming question.


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.

This programming question asking for checking your array concept. They are checking how we think to solve this type of question. 


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.

Finding Armstrong’s number is the same as the reverse number concept but at the last we need to multiple mode three times when we are adding.


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]); } } } } }

 

Read more topics related to java

 

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.