Top Java Programming Interview Questions for Freshers in 2025

Top Java Programming Interview Questions for Freshers in 2025. Java remains one of the most sought-after programming languages, making it essential for freshers to prepare thoroughly for technical interviews. Here are some of the most commonly asked Java programming interview questions for 2025.

  1. Java program to Find Odd or Even number
import java.util.Scanner; 

 public class OddEven {
          public static void main(String[] args) {
                   Scanner scanner = new Scanner(System.in); 
                   System.out.print("Enter any number: ");
                   int number = scanner.nextInt();
                 if (number % 2 == 0) {
                      System.out.println(number + " is even.");
                            }
                else {
                    System.out.println(number + " is odd.");
                              }
                          }
                    }

2. Java program to find Fibonacci series up to a given number range?

import java.util.Scanner;

public class PrimeNumber { 

     public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("enter number of terms");
            int number = 6;
            int first = 0, second = 1, next;
            System.out.println("Fibonacci series is ");
            for ( int i = 0; i<=number; i++)
 {
   System.out.println(first + "");
   next = second+first;
   first = second;
   second = next;
  }
}

Output: 0 1 1 2 3 5 8

3. Swap two numbers without using a third variable.

import java.util.Scanner;

     public class SwapNumbers {
               public static void main(String[] args) {

                    Scanner scanner = new Scanner(System.in);
                    System.out.print("Enter the first number: ");
                     int a = 5,
                     System.out.print("Enter the second number: ");
                     int b = 10;
                     System.out.println("Before swapping: a = " + a + ", b = " + b);

                     a = a + b;
                     b = a - b;
                     a = a - b;
                    System.out.println("After swapping: a = " + a + ", b = " + b);
                                   }
                              }


Output: After Swapping: a = 10 , b = 5

4. Java program to Reverse Number

import java.util.Scanner;
            public class ReverseNumber {
                     public static void main(String[] args) {
                              int no, rev=0,r,a;
                             Scanner scanner = new Scanner(System.in);
                             System.out.println("Enter any number : ");
                             no = scanner.nextInt();
                             a = no;
                             while(no>0)
             {
                     r = no%10;
                     rev = rev*10+r;
                     no=no/10;
         }
                      System.out.println("Reverse : " +rev);
          }
      }


Input: 15786
Output: 68751
Join the Telegram Channel for more updates about jobs and interviews: Join Now!

Top Java Programming Interview Questions

5. Java program to find Palindrome number

import java.util.Scanner;

             public class Main {
                          public static void main(String[] args) {

                                    Scanner scanner = new Scanner(System.in);
                                    System.out.print("Enter a number: ");
                                    int number = scanner.nextInt();

                             if (isPalindrome(number)) {
                                   System.out.println(number + " is a palindrome.");
                                     } else {
                                           System.out.println(number + " is not a palindrome.");
                                                       }
                                                }

                               public static boolean isPalindrome(int num) {
                                               int originalNumber = num;
                                               int reversedNumber = 0;
                                               while (num != 0) {
                                               int digit = num % 10;
                                               reversedNumber = reversedNumber * 10 + digit;
                                              num = num/10;
                              }
                             return originalNumber == reversedNumber;
                        }
                     }

Enter a number: 1001

1001 is a palindrome.

6. Java program to reverse a string

import java.util.Scanner;
         public class Test {
                            public static void main(String[] args) {
                                                Scanner scanner = new Scanner(System.in);
                                                System.out.print("Enter a string: ");
                                                String input = scanner.nextLine();
                                                char ch;
                                                String nstr = "";

                                        for (int i = 0; i < input.length(); i++) {
                                             ch = input.charAt(i);
                                             nstr = ch + nstr;        
                                               }
                           System.out.println("Reversed String is : " + nstr);

7. Java program to reverse each word of a given string

public static void main(String[] args) {
         reverseEachWordOfString("Java is good programming langauges");
      }

static void reverseEachWordOfString(String inputString)
{
         String[] words = inputString.split(" ");

        String reverseString = "";
        for (int i = 0; i < words.length; i++) {
                            String word = words[i];
                            String nstr = "";
                            char ch;
                            for (int j = 0; j < word.length(); j++) {
                            ch = word.charAt(j);
                            nstr = ch + nstr;
                           }
                           reverseString = reverseString + nstr + " ";
                        }
                                 System.out.println(inputString);
                                 System.out.println(reverseString);
                }


Input: Java is good programming langauges

Output: avaJ si doog gnimmargorp seguagnal

8. Java program to find duplicate characters in a string

import java.util.HashMap;
import java.util.Set;

    public class Main {

               public static void main(String[] args) {
                                   duplicateCharacterCount("Learn Java Programming");
                               }

 static void duplicateCharacterCount(String inputString) {

                  HashMap<Character, Integer> charCountMap = new HashMap<>();
                  char[] strArray = inputString.toCharArray();
                  for (char c : strArray) {
                         if (charCountMap.containsKey(c)) {
                             charCountMap.put(c, charCountMap.get(c) + 1);
                         } else {
                              charCountMap.put(c, 1);
                        }
                    }

                    Set<Character> charsInString = charCountMap.keySet();
                    System.out.println("Duplicate Characters in : " + inputString);

                    for (Character ch : charsInString) {
                            if (charCountMap.get(ch) > 1) {
                            System.out.println(ch + " : " + charCountMap.get(ch));
                            }
                     }
                 }
             }

Duplicate Characters in : Learn Java Programming

a : 4

g : 2

m : 2

n : 2

r : 3

9. Java program to count the number of words in a string

public class Main {
           public static void main(String[] args) {
System.out.println("Enter the String");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int count = 1;

 for (int i = 0; i < s.length() - 1; i++) {
       if ((s.charAt(i) == ' ') && (s.charAt(i + 1) != ' ')) {
               count++;
           }
        }

        System.out.println("Number of words in a string: " +count); }
}


Enter the String: Welcome to Java World

Number of words in a string: 4

10. Java program to find if a string is Palindrome

A palindrome string is the same string written reverse or forward. To check for a palindrome, reverse the input string and see if the result matches the input. The example code below describes how to use the String charAt(int index) technique for verifying palindrome strings:

import java.util.Scanner;

public class Main {
       public static void main(String[] args) {
                       String str = "madam";
                       System.out.println(isPalindrome(str));
      }

 static boolean isPalindrome(String str) {
           int start = 0;
           int end = str.length() - 1;

           while (start < end) {
                       if (str.charAt(start) != str.charAt(end)) {
                           return false;
                       }
                       start++;
                       end--;
                 }
                 return true;
           }
 }

11. Java program to swap two strings without using 3rd variable

import java.util.Scanner;

public class Main {
     public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter first string: ");
                String str1 = scanner.nextLine();
                System.out.print("Enter second string: ");
                String str2 = scanner.nextLine();

               System.out.println("Before swapping: str1 = " + str1 + ",
    str2 = " + str2);

 // Swapping without using a third variable
 str1 = str1 + str2; // Concatenate str1 and str2 and store in str1
 str2 = str1.substring(0, str1.length() - str2.length()); // Extract the initial part (original str1) from the concatenated
string
 str1 = str1.substring(str2.length()); // Extract the remaining part (original str2) from the concatenated string

            System.out.println("After swapping: str1 = " + str1 + ",
str2 = " + str2);
     }
}

Enter first string: Hello

Enter second string: World

Before swapping: str1 = Hello, str2 = World

After swapping: str1 = World, str2 = Hello

12. Sort an array without using the in-built method

public class Main {
        public static void main(String[] args) {
                    int[] array = {5, 2, 9, 1, 6};

       selectionSort(array);

     System.out.println("Sorted array:");
         for (int num : array) {
             System.out.print(num + " ");
            }
       }

 public static void selectionSort(int[] array) {
                   int n = array.length;
                   for (int i = 0; i < n - 1; i++) {
                   int minIndex = i;

                     for (int j = i + 1; j < n; j++) {
                     if (array[j] < array[minIndex]) {
                     minIndex = j;
                  }
              }

             // Swap array[i] and array[minIndex]
            int temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = temp;
          }
      }
 }

Output:

Sorted array: 1 2 5 6 9

Download the full Java Interview Questions PDF: Click Here!

Tennr is hiring freshers for the Implementation Engineer: Apply Now!

IBM is hiring freshers for Software Engineer in 2025: Apply Now!

Mastering Sora AI: Text to Video Generator by OpenAI: Read More!

Conclusion

For freshers, mastering these Java concepts can significantly boost confidence in interviews. Ensure you understand both theoretical concepts and practical implementations by coding regularly. Stay updated with new Java releases and frameworks to remain competitive in the job market. Good luck!

1 thought on “Top Java Programming Interview Questions for Freshers in 2025”

Leave a Comment