
Java remains one of the most widely used programming languages in the world, and mastering its basic coding problems is essential for students, job seekers, and coding enthusiasts. In this blog, we’ve compiled 25 of the most asked Basic Java Coding Questions that often appear in interviews, exams, and online coding assessments.
These questions focus on core logic, data types, control structures, loops, arrays, and strings. Let’s dive in. If you want to learn about Java 8 Features click the link.
Java coding interview questions for freshers
1. Hello World Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Check Even or Odd
public class EvenOdd {
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
3. Find Largest of Three Numbers
public class LargestOfThree {
public static void main(String[] args) {
int a = 10, b = 20, c = 5;
int max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
System.out.println("Largest: " + max);
}
}
4. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "Java";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
5. Check Palindrome
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(rev) ? "Palindrome" : "Not Palindrome");
}
}
6. Swap Two Numbers
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);
}
}
7. Print Fibonacci Series
public class FibonacciSeries {
public static void main(String[] args) {
int a = 0, b = 1, n = 10;
for (int i = 0; i < n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}
8. Find Factorial (Using Recursion)
public class Factorial {
static int fact(int n) {
return (n == 1) ? 1 : n * fact(n - 1);
}
public static void main(String[] args) {
System.out.println(fact(5));
}
}
9. Count Characters in String
import java.util.HashMap;
public class CharCount {
public static void main(String[] args) {
String str = "hello";
HashMap<Character, Integer> countMap = new HashMap<>();
for (char c : str.toCharArray())
countMap.put(c, countMap.getOrDefault(c, 0) + 1);
System.out.println(countMap);
}
}
Java Coding Interview Questions and Answers
10. Print Prime Numbers from 1 to 50
public class PrimeNumbers {
public static void main(String[] args) {
for (int i = 2; i <= 50; i++) {
boolean prime = true;
for (int j = 2; j <= i/2; j++)
if (i % j == 0) prime = false;
if (prime) System.out.print(i + " ");
}
}
}
11. Find GCD of Two Numbers
public class GCD {
public static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
System.out.println(findGCD(12, 15));
}
}
12. Check Armstrong Number
public class Armstrong {
public static void main(String[] args) {
int num = 153, temp = num, sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
System.out.println(sum == num ? "Armstrong" : "Not Armstrong");
}
}
13. Reverse an Integer
public class ReverseInteger {
public static void main(String[] args) {
int num = 1234, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
System.out.println("Reversed: " + rev);
}
}
14. Count Vowels and Consonants
public class VowelConsonant {
public static void main(String[] args) {
String str = "hello world";
int vowels = 0, consonants = 0;
str = str.toLowerCase();
for (char c : str.toCharArray()) {
if (Character.isLetter(c)) {
if ("aeiou".indexOf(c) != -1) vowels++;
else consonants++;
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
}
15. Linear Search
public class LinearSearch {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7};
int target = 5;
for (int i = 0; i < arr.length; i++)
if (arr[i] == target)
System.out.println("Found at index: " + i);
}
}
16. Binary Search
import java.util.Arrays;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
int target = 5;
int index = Arrays.binarySearch(arr, target);
System.out.println("Index: " + index);
}
}
17. Insertion Sort
public class InsertionSort {
public static void main(String[] args) {
int[] arr = {4, 3, 2, 1};
for (int i = 1; i < arr.length; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key)
arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
for (int n : arr) System.out.print(n + " ");
}
}
18. Remove Duplicates from Array
import java.util.HashSet;
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 4, 4};
HashSet<Integer> set = new HashSet<>();
for (int num : arr) set.add(num);
System.out.println(set);
}
}
19. Sum of Digits
public class SumDigits {
public static void main(String[] args) {
int num = 1234, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum: " + sum);
}
}
20. Count Words in a Sentence
public class WordCount {
public static void main(String[] args) {
String sentence = "Java is fun to learn";
String[] words = sentence.split(" ");
System.out.println("Words: " + words.length);
}
}
Java Coding Interview Questions for Experienced
21. Find Second Largest Number
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 8};
int max = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int n : arr) {
if (n > max) {
second = max;
max = n;
} else if (n > second && n != max) {
second = n;
}
}
System.out.println("Second Largest: " + second);
}
}
22. Reverse Words in a Sentence
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Java is awesome";
String[] words = sentence.split(" ");
for (int i = words.length - 1; i >= 0; i--)
System.out.print(words[i] + " ");
}
}
23. Check Leap Year
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
System.out.println(isLeap ? "Leap Year" : "Not Leap Year");
}
}
24. Multiply Two Matrices
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{2, 0}, {1, 2}};
int[][] result = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
result[i][j] += a[i][k] * b[k][j];
for (int[] row : result) {
for (int val : row)
System.out.print(val + " ");
System.out.println();
}
}
}
25. Find Missing Number in Array
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 5};
int n = 5;
int sum = n * (n + 1) / 2;
for (int num : arr) sum -= num;
System.out.println("Missing Number: " + sum);
}
}
Conclusion
Practicing these questions will help you build a strong foundation in Java. These programs are commonly seen in coding interviews, university assignments, and online tests. As you get more comfortable, try modifying and combining them for better understanding and to build problem-solving skills. 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