Here You Can See Top 10 Java Coding Interview Questions And Answers. Just Check It In Your IDE And Do Practice Of This Programming.
1) Reverse a String in Java ?
public class Main {
public static void main(String[] args) {
String input = “BoxofLearn”;
StringBuilder reversed = new StringBuilder(input).reverse();
System.out.println(reversed);
}
} Output : olleH
2) Check Prime Number ?
public class Main {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(29));
}
} Output : true
3) Find the Factorial of a Number ?
public class Main {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n – 1);
}
public static void main(String[] args) {
System.out.println(factorial(5));
}
} Output : 120
4) Find the Largest Element in an Array ?
public class Main {
public static int findLargest(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) { if (arr[i] > max)
max = arr[i];
}
return max;
}
public static void main(String[] args) {
int[] arr = {3, 7, 1, 9, 2};
System.out.println(findLargest(arr));
}
} Output : 9
5) Find the Second Largest Element in an Array ?
public class Main {
public static int secondLargest(int[] arr) {
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int n : arr) {
if (n > first) {
second = first;
first = n;
} else if (n > second && n != first) {
second = n;
}
}
return second;
}
public static void main(String[] args) {
int[] arr = {3, 7, 1, 9, 2};
System.out.println(secondLargest(arr));
}
} Output : 7
6) Count Vowels in a String ?
public class Main {
public static int countVowels(String str) {
int count = 0;
for (char c : str.toLowerCase().toCharArray()) {
if (c == ‘a’ || c == ‘e’ || c == ‘i’ || c == ‘o’ || c == ‘u’)
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(countVowels(“Hello World”));
}
} Output : 3
7) Sum of Digits of a Number ?
public class Main {
public static int sumOfDigits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
public static void main(String[] args) {
System.out.println(sumOfDigits(1234));
}
} Output : 10
8) Find the Missing Number in an Array ?
public class Main {
public static int findMissing(int[] arr, int n) {
int sum = n * (n + 1) / 2;
for (int num : arr) sum -= num;
return sum;
}
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6};
System.out.println(findMissing(arr, 6));
}
} Output : 3
9) Find Duplicate Element in an Array ?
public class Main {
public static int findDuplicate(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int index = Math.abs(arr[i]);
if (arr[index] < 0)
return index;
arr[index] = -arr[index];
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 3, 4, 2, 2};
System.out.println(findDuplicate(arr));
}
} Output : 2
10) Merge Two Sorted Arrays
public class Main {
public static int[] mergeSortedArrays(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j])
result[k++] = arr1[i++];
else
result[k++] = arr2[j++];
}
while (i < arr1.length) result[k++] = arr1[i++];
while (j < arr2.length) result[k++] = arr2[j++];
return result;
}
public static void main(String[] args) {
int[] arr1 = {1, 3, 5};
int[] arr2 = {2, 4, 6};
int[] merged = mergeSortedArrays(arr1, arr2);
for (int n : merged)
System.out.print(n + ” “);
}
} Output : 1 2 3 4 5 6
This is Above Java Coding Interview Questions Most Asking In Interviews.
Top 10 Artificial Intelligence (AI) Interview Questions and Answers ?
What Is Graphic Design ?
1 thought on “Top 10 Java Coding Interview Questions”