Top 10 Java Programs for Interview with Output (2025 Edition)

✅ Java Program – Sort an Array

package Code;

import java.util.Arrays;

public class ArraySort {
    public static void main(String[] args) {
        int arr[] = {111, 22, 3133, 414, 55, 66, 99, 100, 200, 77};
        System.out.println("Original num: " + Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println("Sorted Array : " + Arrays.toString(arr));
    }
}

💡 Output:
Original num: [111, 22, 3133, 414, 55, 66, 99, 100, 200, 77]
Sorted Array : [22, 55, 66, 77, 99, 100, 111, 200, 414, 3133]


✅ Java Program – Character Count in String

package Code;

import java.util.HashMap;
import java.util.Map;

public class CharacterCount {
    public static void main(String[] args) {
        String str = "patnabiharamnorsaran";
        Map<Character, Integer> charCount = new HashMap<>();
        for (char c : str.toCharArray()) {
            charCount.put(c, charCount.getOrDefault(c, 0) + 1);
        }
        System.out.println(charCount);
    }
}

💡 Output:
{a=6, b=1, h=1, i=1, m=1, n=3, o=1, p=1, r=3, s=1, t=1}


✅ Java Program – Print Unique Words in a Sentence

package Code;

import java.util.Map;
import org.apache.commons.collections4.map.HashedMap;

public class GFG {
    public static void main(String[] args) {
        String str = "masum delhi patna bihar amnour bihar patna raza";
        String words[] = str.split(" ");
        Map<String, Integer> wordCount = new HashedMap<>();
        for (String word : words) {
            wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
        }
        for (String word : wordCount.keySet()) {
            if (wordCount.get(word) == 1) {
                System.out.println(word + " " + wordCount.get(word));
            }
        }
    }
}

💡 Output:
masum 1
delhi 1
amnour 1
raza 1


✅ Java Program – Non-Repeating and Repeating Characters

package Code;

public class NonRepeatingCharacter {
    public static void main(String[] args) {
        String str = "amnoursaranbihar";
        System.out.print("Non Repeating Character is : ");
        for (char c : str.toCharArray()) {
            if (str.indexOf(c) == str.lastIndexOf(c)) {
                System.out.print(c + " ");
            }
        }
        System.out.print("\nRepeating Character is = ");
        for (char d : str.toCharArray()) {
            if (str.indexOf(d) != str.lastIndexOf(d)) {
                System.out.print(d + " ");
            }
        }
    }
}

💡 Output:
Non Repeating Character is : m o u b
Repeating Character is = a n r s


✅ Java Program – Prime Numbers from 1 to 100

package Code;

public class PrimeChecker {
    public static void main(String[] args) {
        for (int num = 1; num <= 100; num++) {
            System.out.println(num + " is " + (isPrime(num) ? " prime num" : " non prime number"));
        }
    }
    private static boolean isPrime(int num) {
        if (num <= 1)
            return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
}

💡 Output (partial):
1 is non prime number
2 is prime num
3 is prime num
...
97 is prime num
100 is non prime number


✅ Java Program – Find Second Highest Number

package Code;

public class SecondHighestNumber {
    public static void main(String[] args) {
        int first = Integer.MIN_VALUE;
        int second = Integer.MIN_VALUE;
        int arr[] = {11, 22, 33, 44, 55, 66, 99, 100, 200, 77};
        for (int num : arr) {
            if (num > first) {
                second = first;
                first = num;
            } else if (num > second && num != first) {
                second = num;
            }
        }
        System.out.println(second);
    }
}

💡 Output:
100


✅ Java Program – Print Triangle Pattern

package Code;

public class Tringle {
    public static void main(String[] args) {
        int row = 8;
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= row - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

💡 Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *


✅ Java Program – Count Uppercase Letters

package Code;

public class UpperCaseCountInString {
    public static void main(String[] args) {
        String str = "PatnaDelhiKolkataBiharAmnourSARAn";
        int count = 0;
        System.out.print("Uppercase letters: ");
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c)) {
                System.out.print(c + " ");
                count++;
            }
        }
        System.out.println("\nTotal uppercase letters: " + count);
    }
}

💡 Output:
Uppercase letters: P D K B A S A R A
Total uppercase letters: 9


✅ Java Program – Count White Spaces and Words

package Code;

public class WhiteSpaceCount {
    public static void main(String[] args) {
        String str = "hell      aa ad ddsa ddd dd";
        int count = 0;
        for (int i = 0; i <= str.length() - 1; i++) {
            if (str.charAt(i) == ' ') {
                count++;
            }
        }
        System.out.println("Total white space count is : " + count);

        int wordCount = 0;
        for (int j = 0; j <= str.length() - 1; j++) {
            if (str.charAt(j) == ' ' && str.charAt(j + 1) != ' ') {
                wordCount++;
            }
        }
        System.out.println("Total word Count is : " + wordCount);
    }
}

💡 Output:
Total white space count is : 14
Total word Count is : 5


✅ Java Program – Remove Duplicate Words

package Code;

import java.util.HashSet;
import java.util.Set;

public class WordDublicate {
    public static void main(String[] args) {
        String Str = "hello ram delhi patna bihar delhi patna bihar";
        String[] words = Str.split(" ");
        Set<String> uniq = new HashSet<>();
        for (String word : words) {
            uniq.add(word.trim());
        }
        for (String word : uniq) {
            System.out.println(word);
        }
    }
}

💡 Output:
hello
ram
delhi
patna
bihar


Popular posts from this blog

Explore essential Java programs commonly asked in interviews, offering valuable coding insights and practice.

Here is the content refined for clarity and professionalism suitable for someone preparing for a QA Automation interview:

Comprehensive Selenium WebDriver Syntax for Effective Test Automation