
Preparing for a Java interview requires a solid understanding of core concepts and the ability to solve coding problems efficiently. In this article, we’ve compiled 25 Interview Java Coding Questions that are frequently asked by top tech companies. Each question includes a brief explanation and a code example to help you grasp the solution effectively. If you are looking for Java 8 Tutorial click here.
Java Coding Questions | Interview Java Coding Questions
1. Reverse a String
Problem: Write a Java program to reverse a given string.
Solution:
public class ReverseString {
public static void main(String[] args) {
String original = "Interview";
String reversed = new StringBuilder(original).reverse().toString();
System.out.println("Reversed string: " + reversed);
}
}
2. Check for Palindrome
Problem: Determine if a given string is a palindrome.
Solution:
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(reversed) ? "Palindrome" : "Not a palindrome");
}
}
3. Swap Two Numbers Without a Temporary Variable
Problem: Swap two numbers without using a third variable.
Solution:
public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a: " + a + ", b: " + b);
}
}
4. Fibonacci Series Using Recursion
Problem: Print Fibonacci series up to n terms using recursion.
Solution:
public class FibonacciRecursion {
public static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
public static void main(String[] args) {
int terms = 5;
for (int i = 0; i < terms; i++) {
System.out.print(fibonacci(i) + " ");
}
}
}
5. Check for Prime Number
Problem: Determine if a number is prime.
Solution:
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = num > 1;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(isPrime ? "Prime" : "Not prime");
}
}
6. Factorial Using Recursion
Problem: Calculate the factorial of a number using recursion.
Solution:
public class FactorialRecursion {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
int number = 5;
System.out.println("Factorial: " + factorial(number));
}
}
7. Reverse an Array
Problem: Reverse the elements of an array.
Solution:
import java.util.Arrays;
import java.util.Collections;
public class ReverseArray {
public static void main(String[] args) {
Integer[] array = {1, 2, 3, 4, 5};
Collections.reverse(Arrays.asList(array));
System.out.println(Arrays.toString(array));
}
}
8. Find Duplicate Elements in an Array
Problem: Identify duplicate elements in an array.
Solution:
import java.util.HashSet;
public class DuplicateElements {
public static void main(String[] args) {
int[] array = {1, 2, 3, 2, 4, 5, 1};
HashSet<Integer> set = new HashSet<>();
for (int num : array) {
if (!set.add(num)) {
System.out.println("Duplicate found: " + num);
}
}
}
}
9. Find the Largest Element in an Array
Problem: Find the largest number in an array.
Solution:
public class LargestElement {
public static void main(String[] args) {
int[] array = {10, 20, 30, 5, 15};
int max = array[0];
for (int num : array) {
if (num > max) max = num;
}
System.out.println("Largest element: " + max);
}
}
Java Coding Questions for Practice
10. Count Vowels in a String
Problem: Count the number of vowels in a string.
Solution:
public class VowelCount {
public static void main(String[] args) {
String str = "Interview";
int count = 0;
for (char c : str.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) count++;
}
System.out.println("Number of vowels: " + count);
}
}
11. Check Armstrong Number
Problem: Check whether a number is an Armstrong number.
Solution:
public class ArmstrongNumber {
public static void main(String[] args) {
int number = 153, sum = 0, temp = number;
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, 3);
temp /= 10;
}
System.out.println(number == sum ? "Armstrong" : "Not Armstrong");
}
}
12. Count Even and Odd Digits in a Number
public class CountEvenOdd {
public static void main(String[] args) {
int num = 123456;
int even = 0, odd = 0;
while (num != 0) {
if ((num % 10) % 2 == 0) even++;
else odd++;
num /= 10;
}
System.out.println("Even: " + even + ", Odd: " + odd);
}
}
13. Find the GCD of Two Numbers
public class GCD {
public static int findGCD(int a, int b) {
return b == 0 ? a : findGCD(b, a % b);
}
public static void main(String[] args) {
System.out.println("GCD: " + findGCD(20, 28));
}
}
14. Find the LCM of Two Numbers
public class LCM {
public static int findGCD(int a, int b) {
return b == 0 ? a : findGCD(b, a % b);
}
public static void main(String[] args) {
int a = 12, b = 15;
int lcm = (a * b) / findGCD(a, b);
System.out.println("LCM: " + lcm);
}
}
15. Print All Prime Numbers from 1 to N
public class PrimeUptoN {
public static void main(String[] args) {
int n = 50;
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.print(i + " ");
}
}
}
16. Find Missing Number in Array from 1 to N
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5, 6};
int n = 6;
int total = n * (n + 1) / 2;
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Missing Number: " + (total - sum));
}
}
17. Remove Duplicates from an Array
import java.util.*;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4};
Set<Integer> set = new LinkedHashSet<>();
for (int i : arr) set.add(i);
System.out.println("Unique: " + set);
}
}
18. Find the Second Largest Element in an Array
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {10, 20, 40, 35, 50};
int max = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > max) {
second = max;
max = num;
} else if (num > second && num < max) {
second = num;
}
}
System.out.println("Second Largest: " + second);
}
}
19. Linear Search in Array
public class LinearSearch {
public static void main(String[] args) {
int[] arr = {5, 3, 7, 1, 9};
int key = 7;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
System.out.println("Found at index: " + i);
return;
}
}
System.out.println("Not found");
}
}
Java coding test questions and answers
20. Binary Search in Sorted Array
public class BinarySearch {
public static int binarySearch(int[] arr, int key) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] < key) left = mid + 1;
else right = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
System.out.println("Found at: " + binarySearch(arr, 7));
}
}
21. Count Words in a Sentence
public class CountWords {
public static void main(String[] args) {
String sentence = "Java is a popular programming language";
String[] words = sentence.trim().split("\\s+");
System.out.println("Total words: " + words.length);
}
}
22. Sort Array Without Using Arrays.sort()
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 4, 2};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
}
}
}
for (int n : arr) System.out.print(n + " ");
}
}
23. Check if a Number is Even or Odd
public class EvenOdd {
public static void main(String[] args) {
int number = 11;
System.out.println(number % 2 == 0 ? "Even" : "Odd");
}
}
24. Find All Pairs with Given Sum
public class PairWithSum {
public static void main(String[] args) {
int[] arr = {2, 4, 3, 5, 6, -2};
int sum = 7;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == sum) {
System.out.println(arr[i] + " + " + arr[j] + " = " + sum);
}
}
}
}
}
25. Sort Characters in a String Alphabetically
import java.util.Arrays;
public class SortString {
public static void main(String[] args) {
String input = "java";
char[] chars = input.toCharArray();
Arrays.sort(chars);
System.out.println(new String(chars));
}
}
Conclusion:
Mastering these Interview Java Coding Questions will enhance your problem-solving skills and prepare you for technical interviews. Practice these problems regularly to build confidence and improve your coding proficiency. Download Java.
- How to Use Arrays in Java (with Code Examples for Beginners)
- Compilation in Java – How It Works with Execution Flow, Steps, and Example
- How to Use loops in Java: For, While, Do‑While Explained
- How to Use If Else Statement in Java (With Examples)
- How to Understand Object-Oriented Programming in Java Easily