💻 Java Interview Questions and Answers (Part 4 – Practical Coding & Programs)
Coding questions are the heart of any Java interview.
Even if you understand all the theory, you must know how to write small working programs that show your problem-solving skills.
Let’s go through 30+ most asked Java coding questions with easy explanations and outputs 👇
🔹 1. Write a program to print “Hello World” in Java.
Answer:
class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
✅ Output: Hello World
👉 This is the simplest Java program that every beginner starts with.
🔹 2. Write a program to find the sum of two numbers.
class Sum {
public static void main(String[] args) {
int a = 10, b = 20;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
✅ Output: Sum: 30
🔹 3. Write a program to check if a number is even or odd.
class EvenOdd {
public static void main(String[] args) {
int num = 5;
if(num % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
✅ Output: Odd
🔹 4. Program to find the largest of three numbers.
class Largest {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
if(a >= b && a >= c)
System.out.println(a + " is largest");
else if(b >= a && b >= c)
System.out.println(b + " is largest");
else
System.out.println(c + " is largest");
}
}
✅ Output: 25 is largest
🔹 5. Program to check if a number is prime.
class Prime {
public static void main(String[] args) {
int num = 7;
boolean flag = true;
for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
flag = false;
break;
}
}
if(flag)
System.out.println(num + " is prime");
else
System.out.println(num + " is not prime");
}
}
✅ Output: 7 is prime
🔹 6. Program to reverse a number.
class ReverseNumber {
public static void main(String[] args) {
int num = 1234, rev = 0;
while(num != 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
System.out.println("Reversed: " + rev);
}
}
✅ Output: Reversed: 4321
🔹 7. Program to check if a number is palindrome.
class Palindrome {
public static void main(String[] args) {
int num = 121, original = num, rev = 0;
while(num != 0) {
rev = rev * 10 + num % 10;
num = num / 10;
}
if(original == rev)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
✅ Output: Palindrome
🔹 8. Program to find factorial of a number.
class Factorial {
public static void main(String[] args) {
int num = 5;
int fact = 1;
for(int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}
✅ Output: Factorial: 120
🔹 9. Program to find Fibonacci series up to n terms.
class Fibonacci {
public static void main(String[] args) {
int n = 7, a = 0, b = 1;
System.out.print(a + " " + b);
for(int i = 2; i < n; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}
✅ Output: 0 1 1 2 3 5 8
🔹 10. Program to find sum of digits of a number.
class SumOfDigits {
public static void main(String[] args) {
int num = 123, sum = 0;
while(num > 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum);
}
}
✅ Output: Sum of digits: 6
🔹 11. Program to swap two numbers without using third variable.
class Swap {
public static void main(String[] args) {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a: " + a + ", b: " + b);
}
}
✅ Output: a: 20, b: 10
🔹 12. Program to check if a string is palindrome.
class StringPalindrome {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
if(str.equals(rev))
System.out.println("Palindrome String");
else
System.out.println("Not Palindrome");
}
}
✅ Output: Palindrome String
🔹 13. Program to count vowels and consonants in a string.
class CountVowels {
public static void main(String[] args) {
String str = "Hello Java";
int vowels = 0, consonants = 0;
str = str.toLowerCase();
for(char c : str.toCharArray()) {
if("aeiou".indexOf(c) != -1)
vowels++;
else if(Character.isLetter(c))
consonants++;
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}
✅ Output:
Vowels: 4
Consonants: 4
🔹 14. Program to reverse a string.
class ReverseString {
public static void main(String[] args) {
String str = "Java";
String rev = "";
for(int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println("Reversed: " + rev);
}
}
✅ Output: Reversed: avaJ
🔹 15. Program to find largest element in an array.
class LargestArray {
public static void main(String[] args) {
int arr[] = {10, 25, 3, 50, 2};
int max = arr[0];
for(int n : arr) {
if(n > max)
max = n;
}
System.out.println("Largest: " + max);
}
}
✅ Output: Largest: 50
🔹 16. Program to sort an array in ascending order.
import java.util.Arrays;
class SortArray {
public static void main(String[] args) {
int arr[] = {5, 1, 4, 2, 3};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
✅ Output: [1, 2, 3, 4, 5]
🔹 17. Program to find second largest element in an array.
import java.util.Arrays;
class SecondLargest {
public static void main(String[] args) {
int arr[] = {12, 35, 1, 10, 34, 1};
Arrays.sort(arr);
System.out.println("Second Largest: " + arr[arr.length - 2]);
}
}
✅ Output: Second Largest: 34
🔹 18. Program to check Armstrong number.
class Armstrong {
public static void main(String[] args) {
int num = 153, temp = num, sum = 0;
while(num != 0) {
int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
if(sum == temp)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong");
}
}
✅ Output: Armstrong Number
🔹 19. Program to find duplicate elements in an array.
class Duplicate {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 2, 4, 1};
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] == arr[j])
System.out.println("Duplicate: " + arr[j]);
}
}
}
}
✅ Output:
Duplicate: 2
Duplicate: 1
🔹 20. Program to count words in a string.
class CountWords {
public static void main(String[] args) {
String str = "Java is fun to learn";
String[] words = str.split(" ");
System.out.println("Word Count: " + words.length);
}
}
✅ Output: Word Count: 5
🔹 21. Program to find factorial using recursion.
class FactorialRec {
static int fact(int n) {
if(n == 1)
return 1;
else
return n * fact(n - 1);
}
public static void main(String[] args) {
System.out.println("Factorial: " + fact(5));
}
}
✅ Output: Factorial: 120
🔹 22. Program to check leap year.
class LeapYear {
public static void main(String[] args) {
int year = 2024;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
System.out.println("Leap Year");
else
System.out.println("Not Leap Year");
}
}
✅ Output: Leap Year
🔹 23. Program to count number of digits in a number.
class CountDigits {
public static void main(String[] args) {
int num = 12345, count = 0;
while(num != 0) {
num = num / 10;
count++;
}
System.out.println("Digits: " + count);
}
}
✅ Output: Digits: 5
🔹 24. Program to find smallest element in an array.
class Smallest {
public static void main(String[] args) {
int arr[] = {10, 2, 8, 3};
int min = arr[0];
for(int n : arr) {
if(n < min)
min = n;
}
System.out.println("Smallest: " + min);
}
}
✅ Output: Smallest: 2
🔹 25. Program to print multiplication table.
class Table {
public static void main(String[] args) {
int num = 5;
for(int i = 1; i <= 10; i++)
System.out.println(num + " x " + i + " = " + (num * i));
}
}
✅ Output:
5 x 1 = 5
5 x 2 = 10
…
5 x 10 = 50
🔹 26. Program to check if a character is vowel or consonant.
class VowelCheck {
public static void main(String[] args) {
char c = 'e';
if("aeiouAEIOU".indexOf(c) != -1)
System.out.println("Vowel");
else
System.out.println("Consonant");
}
}
✅ Output: Vowel
🔹 27. Program to convert string to character array.
class ToCharArray {
public static void main(String[] args) {
String str = "Java";
char[] arr = str.toCharArray();
for(char c : arr)
System.out.print(c + " ");
}
}
✅ Output: J a v a
🔹 28. Program to find sum of elements in an array.
class SumArray {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
for(int n : arr)
sum += n;
System.out.println("Sum: " + sum);
}
}
✅ Output: Sum: 15
🔹 29. Program to check if a number is perfect.
class PerfectNumber {
public static void main(String[] args) {
int num = 28, sum = 0;
for(int i = 1; i < num; i++) {
if(num % i == 0)
sum += i;
}
if(sum == num)
System.out.println("Perfect Number");
else
System.out.println("Not Perfect");
}
}
✅ Output: Perfect Number
🔹 30. Program to find power of a number.
class Power {
public static void main(String[] args) {
int base = 2, exp = 3, result = 1;
for(int i = 1; i <= exp; i++) {
result *= base;
}
System.out.println("Power: " + result);
}
}
✅ Output: Power: 8
🟢 Final Tips for Coding Rounds
- Always write clean and readable code.
- Try to explain your logic step-by-step to the interviewer.
- Use
forandwhileloops confidently. - Learn small string and array tricks — they appear often!
- Practice on platforms like LeetCode, HackerRank, or CodeStudio.
Hey guys, just tried out fortunegodsvn. Seeing some decent action there! Definitely worth checking out if you’re looking for something new to play. Link is fortunegodsvn