30 Java Basic Exercises for Intermediate with Solutions
Master intermediate Java skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Java, setting a solid foundation for advanced challenges. Start your journey to Java mastery today!
Learning Objectives:
By the end of these exercises, you will gain proficiency in object-oriented concepts, error handling, and working with Java collections like ArrayLists and HashMaps.
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Find the second most frequent element in a list.
Required Input:
[1, 2, 3, 3, 2, 2, 1, 1, 1]
Expected Output:
2
Code In Java
Output
Click Run Button to view compiled output
2. Find the difference between the largest and smallest elements in a list.
Required Input:
[8, 3, 1, 9, 7, 2]
Expected Output:
8
Code In Java
Output
Click Run Button to view compiled output
3. Reverse an array in place.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
[5, 4, 3, 2, 1]
Code In Java
Output
Click Run Button to view compiled output
4. Check if a list is sorted in ascending order.
Required Input:
[1, 2, 3, 4, 5]
Expected Output:
true
Code In Java
Output
Click Run Button to view compiled output
5. Find the longest word in a list of strings.
Required Input:
["apple", "banana", "cherry", "kiwi"]
Expected Output:
banana
Code In Java
Output
Click Run Button to view compiled output
6. Find the sum of all even numbers in an array.
Required Input:
[1, 2, 3, 4, 5, 6]
Expected Output:
12
Code In Java
Output
Click Run Button to view compiled output
7. Rotate a list left by k positions.
Required Input:
[1, 2, 3, 4, 5], k = 2
Expected Output:
[3, 4, 5, 1, 2]
Code In Java
Output
Click Run Button to view compiled output
8. Merge two lists without duplicates.
Required Input:
[1, 2, 3], [3, 4, 5]
Expected Output:
[1, 2, 3, 4, 5]
Code In Java
Output
Click Run Button to view compiled output
9. Check if two arrays are identical (same elements in the same order).
Required Input:
[1, 2, 3], [1, 2, 3]
Expected Output:
true
Code In Java
Output
Click Run Button to view compiled output
10. Find the first repeated element in a list.
Required Input:
[1, 2, 3, 2, 1]
Expected Output:
2
Code In Java
Output
Click Run Button to view compiled output