Top 11 Java Coding Interview Questions for Freshers (With Code & Explanations)
Top 11 Java Coding Interview Questions for Freshers (With Code & Explanations)

Top 11 Java Coding Interview Questions for Freshers (With Code & Explanations)

Crack your next Java interview with these handpicked beginner-level coding problems — complete with code, logic, and tips to impress recruiters.

📄 Introduction

Are you preparing for your first Java developer interview?


This list features the top 11 coding questions frequently asked by leading companies like Google, Amazon, Microsoft, and Infosys.

These problems are gathered based on real interview experiences, candidate feedback, and extensive research across popular coding platforms like LeetCode, HackerRank, and GeeksforGeeks.

Each question comes with clear explanations and Java code examples to help you ace your coding rounds with confidence.

Question 1 – Master the Art of Reversing Strings in Java

Reverse a given string without using built-in functions

Java
public class ReverseString {
    public static void main(String[] args) {
        String str = "hello";
        String reversed = "";

        for (int i = str.length() - 1; i >= 0; i--) {
            reversed += str.charAt(i);
        }

        System.out.println("Reversed string: " + reversed);
    }
}

Question 2 – Is It Prime? Java’s Clever Way to Find Out – Check for Prime Number

Check if a number is prime or not.

Java
public class PrimeCheck {
    public static void main(String[] args) {
        int num = 7;
        boolean isPrime = true;

        if (num <= 1) {
            isPrime = false;
        } else {
            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }

        System.out.println(num + " is prime? " + isPrime);
    }
}

Question 3 – Crack Fibonacci Like a Pro (Java Edition) | Fibonacci Series

Print first n Fibonacci numbers.

Java
public class Fibonacci {
    public static void main(String[] args) {
        int n = 10;
        int a = 0, b = 1;

        System.out.print("Fibonacci Series: " + a + " " + b);

        for (int i = 2; i < n; i++) {
            int next = a + b;
            System.out.print(" " + next);
            a = b;
            b = next;
        }
    }
}

Question 4 Palindrome Check (String) : Palindrome Check in Java – Simple Yet Powerful

Check if a string is a palindrome.

Java
public class PalindromeCheck {
    public static void main(String[] args) {
        String str = "madam";
        String reversed = "";

        for (int i = str.length() - 1; i >= 0; i--) {
            reversed += str.charAt(i);
        }

        if (str.equals(reversed)) {
            System.out.println(str + " is a palindrome.");
        } else {
            System.out.println(str + " is not a palindrome.");
        }
    }
}

Question 5 Factorial (Using Recursion) : Factorial in Java – Recursion vs Iteration Showdown

Java
public class Factorial {
    public static int factorial(int n) {
        if (n == 0)
            return 1;
        return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        int num = 5;
        System.out.println("Factorial of " + num + " is: " + factorial(num));
    }
}

Question 6 – Find the King and Pauper: Min and Max in Java Arrays

Find Largest and Smallest in Array :

Java
public class MinMaxArray {
    public static void main(String[] args) {
        int[] arr = {12, 3, 19, 5, 7};

        int min = arr[0];
        int max = arr[0];

        for (int num : arr) {
            if (num < min) min = num;
            if (num > max) max = num;
        }

        System.out.println("Minimum: " + min);
        System.out.println("Maximum: " + max);
    }
}

Question 7 : Java Hack: Remove Duplicates from Arrays Like a Boss

Remove Duplicates from Array

Java
import java.util.*;

public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 4, 4, 5};

        Set<Integer> set = new LinkedHashSet<>();
        for (int num : arr) {
            set.add(num);
        }

        System.out.println("Array without duplicates: " + set);
    }
}

Question 8. Swap Two Numbers Without Using Third Variable

Swap two numbers without using a temporary variable.

Java
public class SwapNumbers {
    public static void main(String[] args) {
        int a = 5, b = 10;

        System.out.println("Before swap: a = " + a + ", b = " + b);

        a = a + b;
        b = a - b;
        a = a - b;

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

Question 9. Count Occurrence of a Character in a String

Counting Characters in Strings – The Java Way

Java
public class CountCharacter {
    public static void main(String[] args) {
        String str = "java programming";
        char ch = 'a';
        int count = 0;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ch) {
                count++;
            }
        }

        System.out.println("Character '" + ch + "' occurs " + count + " times.");
    }
}

Question 10 : Sort an Array (Bubble Sort)

Bubble Sort Demystified: Java’s Most Asked Sorting Algorithm

Java
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 1, 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]) {
                    // swap
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

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

Question 10 : Find Frequency of Elements in an Array

Frequency Finder: Count Element Occurrences in Java Arrays

Java
import java.util.*;

public class ElementFrequency {
    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 1, 4, 2};
        Map<Integer, Integer> freqMap = new HashMap<>();

        for (int num : arr) {
            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
        }

        System.out.println("Frequencies:");
        for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
            System.out.println(entry.getKey() + " -> " + entry.getValue());
        }
    }
}

This article is part of our Interview Prep series.


#JavaInterview #JavaCoding #CodingInterview #JavaDeveloper #ProgrammingQuestions #InterviewPrep #JavaForBeginners #FreshersJob #TechInterview #CodeInterview #LearnJava #DSA #SoftwareEngineering #CodingChallenge #JobInterviewTips

image Simply Creative Minds