
Java remains one of the most popular programming languages for software development, enterprise applications, Android development, and backend systems.
If you are preparing for campus placements, internships, coding tests, or technical interviews, practicing Java interview programs can significantly improve your problem-solving skills and coding confidence.
In this guide, you will learn some of the most commonly asked Java interview programs along with explanations, code examples, outputs, and interview tips.
1. Java Program to Check Even or Odd Number
Checking whether a number is even or odd is one of the most basic Java interview questions. It helps interviewers evaluate your understanding of conditional statements and the modulus operator.
Java Code
public class EvenOdd {
public static void main(String[] args) {
int number = 12;
if(number % 2 == 0)
System.out.println("Even Number");
else
System.out.println("Odd Number");
}
}
Output
Even Number
Explanation
If a number is divisible by 2 without leaving a remainder, it is considered an even number. Otherwise, it is odd.
Interview Tip
Interviewers may ask:
- How would you check even and odd numbers without using the modulus operator?
- Can this logic work with negative numbers?
2. Java Program to Find the Largest of Three Numbers
This question tests logical thinking and conditional statements.
Java Code
public class LargestNumber {
public static void main(String[] args) {
int a = 15;
int b = 25;
int c = 10;
if(a >= b && a >= c)
System.out.println("Largest Number = " + a);
else if(b >= a && b >= c)
System.out.println("Largest Number = " + b);
else
System.out.println("Largest Number = " + c);
}
}
Output
Largest Number = 25
Explanation
The program compares three numbers and displays the largest value.
Interview Tip
The interviewer may ask you to:
- Find the largest number using nested if statements.
- Find the largest number using arrays.
3. Java Program to Check a Prime Number
Prime number questions are among the most frequently asked Java interview programs.
Java Code
public class PrimeNumber {
public static void main(String[] args) {
int number = 29;
boolean isPrime = true;
for(int i = 2; i <= number / 2; i++) {
if(number % i == 0) {
isPrime = false;
break;
}
}
if(isPrime)
System.out.println("Prime Number");
else
System.out.println("Not Prime Number");
}
}
Output
Prime Number
Explanation
A prime number has only two factors:
- 1
- Itself
Examples:
2, 3, 5, 7, 11, 13
Interview Tip
A common follow-up question:
How can you optimize this program using square root logic?
4. Java Program to Calculate Factorial
Factorial is one of the most important beginner-level Java interview questions.
Java Code
public class Factorial {
public static void main(String[] args) {
int number = 5;
int factorial = 1;
for(int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial = " + factorial);
}
}
Output
Factorial = 120
Explanation
Factorial of 5:
5 × 4 × 3 × 2 × 1 = 120
Interview Tip
You may be asked to:
- Write factorial using recursion.
- Compare recursion and loops.
5. Java Program to Print Fibonacci Series
Fibonacci Series is a popular interview question because it tests loops and variable manipulation.
Java Code
public class Fibonacci {
public static void main(String[] args) {
int n = 10;
int first = 0;
int second = 1;
System.out.print(first + " " + second + " ");
for(int i = 2; i < n; i++) {
int next = first + second;
System.out.print(next + " ");
first = second;
second = next;
}
}
}
Output
0 1 1 2 3 5 8 13 21 34
Explanation
Each number is the sum of the previous two numbers.
Interview Tip
Interviewers may ask:
- Write Fibonacci using recursion.
- Find the nth Fibonacci number.
6. Java Program to Reverse a Number
This question is extremely common in coding interviews.
Java Code
public class ReverseNumber {
public static void main(String[] args) {
int number = 12345;
int reverse = 0;
while(number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
System.out.println(reverse);
}
}
Output
54321
Explanation
The program extracts digits one by one and rebuilds the number in reverse order.
Interview Tip
A common follow-up:
Can you reverse a number without converting it into a string?
7. Java Program to Check Palindrome Number
Palindrome numbers read the same from both directions.
Examples
12113311221
Java Code
public class PalindromeNumber {
public static void main(String[] args) {
int number = 121;
int original = number;
int reverse = 0;
while(number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
if(original == reverse)
System.out.println("Palindrome Number");
else
System.out.println("Not Palindrome Number");
}
}
Output
Palindrome Number
Explanation
The program reverses the number and compares it with the original value.
Interview Tip
Interviewers often ask:
- Palindrome using String.
- Palindrome without using extra variables.
8. Java Program to Check Armstrong Number
Armstrong Number is a very popular Java interview question.
Example
153
Calculation:
1³ + 5³ + 3³ = 153
Java Code
public class ArmstrongNumber {
public static void main(String[] args) {
int number = 153;
int original = number;
int sum = 0;
while(number != 0) {
int digit = number % 10;
sum += digit * digit * digit;
number /= 10;
}
if(sum == original)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong Number");
}
}
Output
Armstrong Number
Explanation
An Armstrong Number is a number that equals the sum of the cubes of its digits.
Interview Tip
Advanced interviewers may ask:
- Armstrong Number for n digits.
- Armstrong Number in a range.
9. Java Program to Find Sum of Digits
This question tests loops and arithmetic operators.
Java Code
public class SumOfDigits {
public static void main(String[] args) {
int number = 1234;
int sum = 0;
while(number != 0) {
sum += number % 10;
number /= 10;
}
System.out.println("Sum = " + sum);
}
}
Output
Sum = 10
Explanation
The program extracts digits one by one and adds them together.
Interview Tip
A common follow-up question:
Find the sum of digits using recursion.
10. Java Program to Count Digits in a Number
Java Code
public class CountDigits {
public static void main(String[] args) {
int number = 123456;
int count = 0;
while(number != 0) {
count++;
number /= 10;
}
System.out.println("Digits = " + count);
}
}
Output
Digits = 6
Explanation
Each loop iteration removes one digit until the number becomes zero.
11. Java Program to Reverse a String
This is one of the most frequently asked Java interview programs.
Java Code
public class ReverseString {
public static void main(String[] args) {
String str = "Java";
for(int i = str.length()-1; i >= 0; i--) {
System.out.print(str.charAt(i));
}
}
}
Output
avaJ
Explanation
The program traverses the string from the last character to the first.
Interview Tip
Interviewers may ask:
- Reverse without using loops.
- Reverse using StringBuilder.
12. Java Program to Check Palindrome String
Java Code
public class PalindromeString {
public static void main(String[] args) {
String str = "madam";
String reverse = "";
for(int i = str.length()-1; i >= 0; i--) {
reverse += str.charAt(i);
}
if(str.equals(reverse))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
Output
Palindrome
Explanation
A palindrome string reads the same from both directions.
Examples:
madamlevelradar
13. Java Program to Count Vowels in a String
Java Code
public class CountVowels {
public static void main(String[] args) {
String text = "Programming";
int count = 0;
text = text.toLowerCase();
for(int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
count++;
}
System.out.println("Vowels = " + count);
}
}
Output
Vowels = 3
Explanation
The program checks each character and counts vowels.
Interview Tip
A common variation:
Count vowels and consonants separately.
14. Java Program to Count Words in a String
Java Code
public class CountWords {
public static void main(String[] args) {
String sentence = "Java is a powerful language";
String words[] = sentence.split(" ");
System.out.println("Total Words = " + words.length);
}
}
Output
Total Words = 5
Explanation
The split() method separates words using spaces.
15. Java Program to Find Duplicate Characters
Java Code
public class DuplicateCharacters {
public static void main(String[] args) {
String str = "programming";
char chars[] = str.toCharArray();
for(int i = 0; i < chars.length; i++) {
for(int j = i + 1; j < chars.length; j++) {
if(chars[i] == chars[j]) {
System.out.println(chars[j]);
break;
}
}
}
}
}
Output
rgm
Explanation
The program compares each character with remaining characters.
Interview Tip
Advanced version:
Print duplicate characters without repetition.
16. Java Program to Check Anagram
Two words are anagrams if they contain the same characters in different order.
Examples:
listensilent
earthheart
Java Code
import java.util.Arrays;
public class Anagram {
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
char a[] = str1.toCharArray();
char b[] = str2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
if(Arrays.equals(a, b))
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}
Output
Anagram
Explanation
The program sorts both strings and compares them.
Interview Tip
Interviewers often ask:
- Check anagram without sorting.
- Ignore spaces and case sensitivity.
17. Java Program to Find the Largest Element in an Array
Finding the largest element is one of the most common array interview questions.
Java Code
public class LargestElement {
public static void main(String[] args) {
int arr[] = {12, 45, 78, 23, 90, 34};
int largest = arr[0];
for(int i = 1; i < arr.length; i++) {
if(arr[i] > largest) {
largest = arr[i];
}
}
System.out.println("Largest Element = " + largest);
}
}
Output
Largest Element = 90
Interview Tip
A common variation:
- Find the second largest element.
- Find the largest element without sorting.
18. Java Program to Reverse an Array
Java Code
public class ReverseArray {
public static void main(String[] args) {
int arr[] = {10,20,30,40,50};
for(int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
Output
50 40 30 20 10
Explanation
The program starts from the last index and prints elements in reverse order.
19. Java Program for Linear Search
Linear Search checks each element one by one.
Java Code
public class LinearSearch {
public static void main(String[] args) {
int arr[] = {10,20,30,40,50};
int search = 30;
boolean found = false;
for(int value : arr) {
if(value == search) {
found = true;
break;
}
}
if(found)
System.out.println("Element Found");
else
System.out.println("Element Not Found");
}
}
Output
Element Found
20. Java Program for Binary Search
Binary Search is one of the most important searching algorithms.
Java Code
import java.util.Arrays;
public class BinarySearch {
public static void main(String[] args) {
int arr[] = {10,20,30,40,50};
int result = Arrays.binarySearch(arr, 40);
System.out.println("Index = " + result);
}
}
Output
Index = 3
Interview Tip
Interviewers often ask:
- Difference between Linear Search and Binary Search.
- Time complexity of Binary Search.
21. Java Program for Bubble Sort
Bubble Sort is one of the most frequently asked sorting algorithms.
Java Code
public class BubbleSort {
public static void main(String[] args) {
int arr[] = {5,3,8,4,2};
for(int i = 0; i < arr.length - 1; i++) {
for(int j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for(int num : arr)
System.out.print(num + " ");
}
}
Output
2 3 4 5 8
22. Java Program for Method Overloading
Method Overloading allows multiple methods with the same name but different parameters.
Java Code
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator obj = new Calculator();
System.out.println(obj.add(10,20));
System.out.println(obj.add(10,20,30));
}
}
Output
3060
Interview Tip
This is a common Object-Oriented Programming interview question.
23. Java Program for Method Overriding
Method Overriding allows a subclass to provide a specific implementation of a parent class method.
Java Code
class Animal {
void sound() {
System.out.println("Animal Sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog Barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output
Dog Barks
24. Java Program for Inheritance
Inheritance allows one class to acquire properties and methods of another class.
Java Code
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Output
EatingBarking
Interview Tip
Interviewers frequently ask:
- Types of inheritance in Java.
- Why Java does not support multiple inheritance using classes.
25. Java Program for Exception Handling
Exception handling is one of the most important Java interview topics.
Java Code
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch(Exception e) {
System.out.println("Exception Handled");
}
}
}
Output
Exception Handled
Explanation
The program catches runtime exceptions and prevents abnormal termination.
Most Important Java Interview Programs for Freshers
If you have limited preparation time, focus on these programs first:
- Prime Number
- Factorial
- Fibonacci Series
- Armstrong Number
- Reverse Number
- Reverse String
- Palindrome String
- Count Vowels
- Anagram
- Largest Element in Array
- Reverse Array
- Linear Search
- Binary Search
- Bubble Sort
- Method Overloading
- Method Overriding
- Inheritance
- Exception Handling
These are among the most commonly asked Java interview programs during placements and technical interviews.
Frequently Asked Questions
Which Java programs are most asked in interviews?
Prime Number, Fibonacci Series, Armstrong Number, Reverse String, Binary Search, Bubble Sort, and Inheritance are among the most commonly asked Java interview programs.
How many Java programs should freshers practice?
Freshers should practice at least 25–50 important Java programs before attending coding interviews.
Are Java interview programs important for placements?
Yes. Most companies include programming questions in coding rounds and technical interviews.
Is Java still a good career choice in 2026?
Yes. Java remains one of the most widely used programming languages for enterprise software, backend development, Android applications, and financial systems.
Final Thoughts
Java interviews are not about memorizing code. Recruiters want to see how you solve problems and explain your approach.
Start with these 25 Java interview programs and understand the logic behind each solution. Once you become comfortable with these questions, move toward advanced topics such as Collections Framework, Multithreading, JDBC, and Spring Boot.
Consistent practice is the key to becoming a confident Java developer.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.