Top 10 Java Coding Interview Questions

Learn the most IMP Java coding interview questions and answers. We wrote a simple logic for Java beginners. Just copy all the code and paste it into your IDE for better understanding.

1) How to 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 : nraeLfoxoB

2) How to check Prime Number in Java?

public class Main {
public static boolean checkPrime(int number) {
if (number <= 1) {
return false;
}

for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}

return true;
}

public static void main(String[] args) {
int input = 29;
System.out.println("Is " + input + " a prime number? " + checkPrime(input));
}
}
  Output : true

3) Find the factorial of a number ?

public class Main {
public static int calculateFactorial(int number) {
if (number == 0) {
return 1;
}
return number * calculateFactorial(number - 1);
}

public static void main(String[] args) {
int input = 5;
System.out.println("Factorial of " + input + " is: " + calculateFactorial(input));
}
}
 Output : 120

4) Find the largest element in an array ?

public class Main {
public static int getMaxValue(int[] numbers) {
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max)
max = numbers[i];
}
return max;
}

public static void main(String[] args) {
int[] data = {3, 7, 1, 9, 2};
System.out.println("Largest number: " + getMaxValue(data));
}
}
Output : 9

5) Find the second largest element in an array ?

public class Main {
public static int findSecondLargest(int[] numbers) {
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;

for (int num : numbers) {
if (num > max) {
secondMax = max;
max = num;
} else if (num > secondMax && num != max) {
secondMax = num;
}
}
return secondMax;
}

public static void main(String[] args) {
int[] data = {3, 7, 1, 9, 2};
System.out.println("Second largest number: " + findSecondLargest(data));
}
}
Second largest number: 7

6) Write a code that count vowels in a string ?

public class Main {
public static int getVowelCount(String input) {
int vowelCount = 0;
for (char ch : input.toLowerCase().toCharArray()) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
}
}
return vowelCount;
}

public static void main(String[] args) {
String text = "Hello World";
System.out.println("Number of vowels: " + getVowelCount(text));
}
}
Vowels number is : 3

7) Sum of digits of a number ?

public class Main {
public static int calculateDigitSum(int number) {
int total = 0;
while (number != 0) {
total += number % 10;
number /= 10;
}
return total;
}

public static void main(String[] args) {
int value = 1234;
System.out.println("Sum of digits: " + calculateDigitSum(value));
}
}
Sum of digits: 10

8) Find the missing number in an Array ?

public class Main {
public static int getMissingNumber(int[] data, int totalCount) {
int expectedSum = totalCount * (totalCount + 1) / 2;
for (int value : data) {
expectedSum -= value;
}
return expectedSum;
}

public static void main(String[] args) {
int[] numbers = {1, 2, 4, 5, 6};
System.out.println("Missing number is: " + getMissingNumber(numbers, 6));
}
}
Missing number is: 3

9) How to find duplicate element in an Array ?

public class Main {
public static int getDuplicate(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int index = Math.abs(nums[i]);
if (nums[index] < 0) {
return index;
}
nums[index] = -nums[index];
}
return -1; // No duplicate found
}

public static void main(String[] args) {
int[] data = {1, 3, 4, 2, 2};
System.out.println("Duplicate number is: " + getDuplicate(data));
}
}
Duplicate number is: 2

10) How to merge two sorted arrays in Java?

public class Main {
public static int[] mergeTwoSortedArrays(int[] first, int[] second) {
int[] mergedArray = new int[first.length + second.length];
int a = 0, b = 0, c = 0;

while (a < first.length && b < second.length) {
if (first[a] < second[b]) {
mergedArray[c++] = first[a++];
} else {
mergedArray[c++] = second[b++];
}
}

while (a < first.length) {
mergedArray[c++] = first[a++];
}

while (b < second.length) {
mergedArray[c++] = second[b++];
}

return mergedArray;
}

public static void main(String[] args) {
int[] arrayOne = {1, 3, 5};
int[] arrayTwo = {2, 4, 6};
int[] result = mergeTwoSortedArrays(arrayOne, arrayTwo);

System.out.print("Merged array: ");
for (int num : result) {
System.out.print(num + " ");
}
}
}
Output : 1 2 3 4 5 6

Note: All the above questions are essential for the interviews and exams.

Top 10 Artificial Intelligence (AI) Interview Questions and Answers ?

What Is Graphic Design ?

 

1 thought on “Top 10 Java Coding Interview Questions”

Leave a Comment