🔹 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
🔹 2. Check even or odd
🔹 3. Prime number check
🔹 4. Print Fibonacci series
🔹 5. Factorial of number
🔹 6. Reverse a number
🔹 7. Palindrome number
🔹 8. Armstrong number (3-digit)
🔹 9. Count digits
🔹 10. Find max of three numbers
🔹 11. Print reverse of string
🔹 12. Check string palindrome
🔹 13. Count vowels and consonants
🔹 14. Reverse array
🔹 15. Sum of array elements
🔹 16. Find max in array
🔹 17. Linear Search
🔹 18. Binary Search (Sorted Array)
🔹 19. Bubble sort
🔹 20. Reverse each word in a string
🔹 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.No | Description | Output Example |
---|---|---|
1 | Swap two numbers | a = 10, b = 5 |
2 | Check even or odd | Odd |
3 | Prime number check | Prime |
4 | Print Fibonacci series (first 10) | 0 1 1 2 3 5 8 13 21 34 |
5 | Factorial of number (5) | Factorial: 120 |
6 | Reverse a number (1234) | Reversed: 4321 |
7 | Palindrome number (121) | Palindrome |
8 | Armstrong number (153) | Armstrong |
9 | Count digits (12345) | Digits: 5 |
10 | Find max of three numbers | Max: 25 |
11 | Reverse of string "Java" | Reversed: avaJ |
12 | Check string palindrome ("madam") | Palindrome |
13 | Count vowels/consonants in "Hello" | Vowels: 3, Consonants: 7 |
14 | Reverse array {1,2,3,4,5} | 5 4 3 2 1 |
15 | Sum of array elements | Sum: 15 |
16 | Find max in array | Max: 30 |
17 | Linear search key = 7 | Found at 2 |
18 | Binary search key = 6 | Found at 2 |
19 | Bubble sort on [5,1,4,2,8] | [1, 2, 4, 5, 8] |
20 | Reverse each word in "Java is fun" | avaJ si nuf |