Top 15 Java Interview Programs for Freshers

In this post, we have collected the most frequently asked Java interview programs for freshers. Please go through all the questions. These questions check your problem-solving and logical thinking skills. Let's dive deep into the topic:

Read Also: Top 30 Java Interview Questions for Freshers

Q1.Write a FizzBuzz program in Java?

FizzBuzz program has the following rules:
a. When the number is divisible by 3 then the program displays the "Fizz" keyword.
b. When the number is divisible by 5 then the program displays the "Buzz" keyword.
c. When the number is divisible by both 3 and 5 then it displays the "FizzBuzz" keyword.
d. Else print the number as it is.

You can find the solution here.

Q2 Write a program to check whether a given number is odd or even?

This is one of the most common questions for freshers.
a. If a given number is divisible by 2 then the number is even.
b. If a given number is not divisible by 2 and the remainder is 1 then the number is odd.

 public class EvenOdd{
    public static void evenOddValue(int value) {
    if(value % 2 == 0){
        System.out.println("The given value is even: " + value);
    }
    else{
        System.out.println("The given value is odd: " + value);
    }
}
    public static void main(String [] args) {
        int num = 25;
        EvenOdd obj = new EvenOdd();
        obj.evenOddValue(num);
     }
}

Output:
The given value is odd: 25

Q3 Write a program to verify if a given number is prime or not in Java?

If a given number is divisible by the other number (less than or equal to the square root of the number). Then the number is not prime. The following program helps to verify if a number is prime or not.

 public class PrimeNumberProgram{
    public static boolean primeNumber(int value) {
        if(value <= 1){
                return false;
        }
        for(int i = 2; i <= Math.sqrt(value); i++){
            if(value % i == 0){
                return false;
            }
        }
            return true;
}
    public static void main(String [] args){
        int num = 13;
        PrimeNumberProgram obj = new PrimeNumberProgram();
        if(obj.primeNumber(num)){
            System.out.println(" The given number is a prime number: "  + num);  
       } else {  
           System.out.println(" The given number is not a prime number: " + num);  
        }
    }
}

Output:
The given number is a prime number: 13

Q4 Write a program to find the factorial of a given number using Java?

Please find the factorial program below:

 public class FactorialProgram {
    public static void factorialNumber(int value) {
        int factorial = 1;
        for(int a = 1; a <= value; a++) {
            factorial= factorial*a;
        }
        System.out.println("The given number is:" + value + "\nThe factorial number is: " + factorial);   
    }
    public static void main(String [] args){
        FactorialProgram obj = new FactorialProgram();
        int num = 4;
        obj.factorialNumber(num);
    }
}

Output:
The given number is:4
The factorial number is: 24

Q5 Write a program to print the Fibonacci series of a given number in Java?

 public class FibonacciNumberProgram{
    public static void fibonacciNumber(int value){
        int number1 = 0, number2 = 1, number3;
        System.out.print(number1 + ", " + number2);
        for(int i =2; i < value; ++i) {
            number3 = number1 + number2;
            System.out.print(", " +number3);
            number1 = number2;
            number2 = number3;
        }
    }
    public static void main(String [] args){
        FibonacciNumberProgram obj = new FibonacciNumberProgram();
        int num = 8;
        obj.fibonacciNumber(num);
    }
}

Output:
0, 1, 1, 2, 3, 5, 8, 13

Q6 Write a program to verify a given number is palindrome or not using Java?

 public class PalindromeNumberProgram{
    public static void checkPalindrome(int value) {
        int number, addition = 0, temporary;
        temporary = value;
        while( value > 0 ) {
            number = value % 10;
            addition = (addition*10) + number;
            value = value / 10;
        }
        if(temporary == addition){
            System.out.println("the given number is palindrome.");
        }else{
            System.out.println("the given number is not palindrome.");
        }
    }
    public static void main(String [] args) {
        PalindromeNumberProgram obj = new PalindromeNumberProgram();
        int num = 8543458;
        obj.checkPalindrome(num);
        int num1 = 843458;
        obj.checkPalindrome(num1);
    }
}

Output:
the given number is palindrome.
the given number is not palindrome.


Q7 Write a reverse a string program in Java?

This question must be on your to-do list before appearing for the interview. I have shared the different ways to reverse a string in Java here.

Q8 Write a program to find duplicate characters in a given string?

 import java.util.HashSet;
public class DuplicateCharactersProgram {
    public static void findDuplicateMethod(String string) {
        char[] duplicate = string.toCharArray();
        HashSet<Character> set = new HashSet<Character>();
        System.out.println("The given string is: " + string);
        System.out.println("The duplicate characters are:");
        for (int a = 0; a < string.length(); a++) {
            for (int b = a + 1; b < string.length(); b++) {
                if (duplicate[a] == duplicate[b]) {
                   set.add( duplicate[b] );
                   break;
                }
            }
        }
        System.out.println(set);
    }
    public static void main(String [] args) {
        DuplicateCharactersProgram obj = new DuplicateCharactersProgram();
        String str = "AliveIsAwesome";
        obj.findDuplicateMethod(str);
    }
}

Output:
The given string is: AliveIsAwesome
The duplicate characters are:
[A, s, e]


Q9 Write a program to verify if a given string is palindrome or not using Java?

The answer to this question can be found here.

Q10 Write a program to verify a given number is Armstrong or not using Java?

If the sum of the cubes of the digits is equal to the original number then the original number is called as Armstrong number.
 public class ArmstrongNumberProgram {
    public static void armstrongValues(int num){
        int temporary, value, count=0;
        temporary = num;
        while(num > 0){
             value = num % 10;
             num = num / 10;
             count = count + (value*value*value);
        }
        if(temporary == count)  
            System.out.println("The given number is an armstrong: " +temporary+ " = " + count);   
        else  
            System.out.println("The given number is not armstrong: " +temporary+ " != " + count); 
    }
    public static void main(String [] args) {
        ArmstrongNumberProgram obj = new ArmstrongNumberProgram();
        int num =407;
        obj.armstrongValues(num);
        int num1 =408;
        obj.armstrongValues(num1);
    }
}

Output:
The given number is an armstrong: 407 = 407
The given number is not armstrong: 408 != 576

Q11 Write a program to calculate the number of words in a given string in Java?

 public class CountNumberOfWordsProgram  {
    public static void countWordsMethod(String string)   {
       String[] number = string.trim().split(" ");
       System.out.println("the total number of words: " +number.length);
       for(int a = 0; a < number.length; a++){
          System.out.println(number[a]);
       }
    }
    public static void main(String [] args){
        CountNumberOfWordsProgram obj = new CountNumberOfWordsProgram();
        String words = "Alive is Awesome";
        obj.countWordsMethod(words);
    }
}

Output:
the total number of words: 3
Alive
is
Awesome


Q12 Write a program for a downward star pyramid pattern in Java?

star pattern

You can find the answer and practice similar questions here.

Q13 Write a program to find missing numbers in an array using Java?

This question is answered here.

Q14 Write a program to print the below pattern in Java?

number pattern
You can find the number pattern programs for practice here.

Q15 Write a program to print the below character pattern in Java?


alphabet pattern
You can find the alphabet pattern programs for practice here.

That's all for today. Please mention in the comments if you have faced any other java interview programs as fresher that are not included in the above list.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry