Skip to main content

20 Java Coding Questions with Answers (With Code Examples)

 🔹 Introduction

Java is one of the most widely used programming languages for building robust, scalable applications. Whether you're preparing for a coding interview, brushing up on fundamentals, or practicing for competitive exams, solving coding problems is the best way to improve your logic and confidence.

In this guide, we've compiled 50 essential Java coding questions with answers—ranging from simple to intermediate levels. Each problem includes a clear Java code example to help you understand the logic and syntax step-by-step. This collection covers topics like loops, arrays, strings, OOP, searching, sorting, and more—making it perfect for beginners and intermediate learners alike.

Let’s get started and level up your Java programming skills!





🔹 1. Swap two numbers

java
int a = 5, b = 10; int temp = a; a = b; b = temp; System.out.println("a = " + a + ", b = " + b);

🔹 2. Check even or odd

java
int num = 7; System.out.println((num % 2 == 0) ? "Even" : "Odd");

🔹 3. Prime number check

java
int n = 7; boolean isPrime = true; for(int i=2; i<=n/2; i++) { if(n % i == 0) { isPrime = false; break; } } System.out.println(isPrime ? "Prime" : "Not Prime");

🔹 4. Print Fibonacci series

java
int n = 10, 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; }

🔹 5. Factorial of number

java
int num = 5, fact = 1; for(int i = 1; i <= num; i++) { fact *= i; } System.out.println("Factorial: " + fact);

🔹 6. Reverse a number

java
int n = 1234, rev = 0; while(n != 0) { rev = rev * 10 + n % 10; n /= 10; } System.out.println("Reversed: " + rev);

🔹 7. Palindrome number

java
int n = 121, original = n, rev = 0; while(n != 0) { rev = rev * 10 + n % 10; n /= 10; } System.out.println(original == rev ? "Palindrome" : "Not Palindrome");

🔹 8. Armstrong number (3-digit)

java
int n = 153, sum = 0, temp = n; while(temp != 0) { int d = temp % 10; sum += d * d * d; temp /= 10; } System.out.println(sum == n ? "Armstrong" : "Not Armstrong");

🔹 9. Count digits

java
int n = 12345, count = 0; while(n != 0) { count++; n /= 10; } System.out.println("Digits: " + count);

🔹 10. Find max of three numbers

java
int a = 10, b = 25, c = 15; int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); System.out.println("Max: " + max);

🔹 11. Print reverse of string

java
String s = "Java"; String rev = ""; for(int i = s.length()-1; i >= 0; i--) { rev += s.charAt(i); } System.out.println("Reversed: " + rev);

🔹 12. Check string palindrome

java
String s = "madam"; String rev = new StringBuilder(s).reverse().toString(); System.out.println(s.equals(rev) ? "Palindrome" : "Not Palindrome");

🔹 13. Count vowels and consonants

java
String s = "Hello World".toLowerCase(); int vowels = 0, consonants = 0; for(char c : s.toCharArray()) { if(Character.isLetter(c)) { if("aeiou".indexOf(c) != -1) vowels++; else consonants++; } } System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);

🔹 14. Reverse array

java
int[] arr = {1, 2, 3, 4, 5}; for(int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i] + " "); }

🔹 15. Sum of array elements

java
int[] arr = {1, 2, 3, 4, 5}; int sum = 0; for(int num : arr) sum += num; System.out.println("Sum: " + sum);

🔹 16. Find max in array

java
int[] arr = {10, 25, 15, 30}; int max = arr[0]; for(int num : arr) { if(num > max) max = num; } System.out.println("Max: " + max);

🔹 17. Linear Search

java
int[] arr = {5, 3, 7, 2}; int key = 7, pos = -1; for(int i = 0; i < arr.length; i++) { if(arr[i] == key) { pos = i; break; } } System.out.println(pos != -1 ? "Found at " + pos : "Not Found");

🔹 18. Binary Search (Sorted Array)

java
int[] arr = {2, 4, 6, 8, 10}; int key = 6, low = 0, high = arr.length - 1; while(low <= high) { int mid = (low + high) / 2; if(arr[mid] == key) { System.out.println("Found at " + mid); break; } else if(arr[mid] < key) low = mid + 1; else high = mid - 1; }

🔹 19. Bubble sort

java
int[] arr = {5, 1, 4, 2, 8}; 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; } } } System.out.println(Arrays.toString(arr));

🔹 20. Reverse each word in a string



java
String s = "Java is fun"; String[] words = s.split(" "); for(String word : words) { System.out.print(new StringBuilder(word).reverse() + " "); }


🔹 Expected Outputs

Here's what you can expect when you run each of the programs:



Whether you're preparing for interviews, exams, or just learning Java for fun, practicing these questions will give you a strong foundation. The code samples use concise logic and standard Java syntax, making them easy to understand and modify for further learning.

These 20 Java coding exercises are essential building blocks for mastering Java programming. From swapping numbers and checking primes to string manipulations and sorting algorithms, each problem strengthens your logical thinking, syntax clarity, and problem-solving approach.

🔹 Conclusion    



Q.NoDescriptionOutput Example
1Swap two numbersa = 10, b = 5
2Check even or oddOdd
3Prime number checkPrime
4Print Fibonacci series (first 10)0 1 1 2 3 5 8 13 21 34
5Factorial of number (5)Factorial: 120
6Reverse a number (1234)Reversed: 4321
7Palindrome number (121)Palindrome
8Armstrong number (153)Armstrong
9Count digits (12345)Digits: 5
10Find max of three numbersMax: 25
11Reverse of string "Java"Reversed: avaJ
12Check string palindrome ("madam")Palindrome
13Count vowels/consonants in "Hello"Vowels: 3, Consonants: 7
14Reverse array {1,2,3,4,5}5 4 3 2 1
15Sum of array elementsSum: 15
16Find max in arrayMax: 30
17Linear search key = 7Found at 2
18Binary search key = 6Found at 2
19Bubble sort on [5,1,4,2,8][1, 2, 4, 5, 8]
20Reverse each word in "Java is fun"avaJ si nuf