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

import java.util.*; public class Main { public static int secondMostFrequent() { List lst = Arrays.asList(1, 2, 3, 3, 2, 2, 1, 1, 1); // Your code here return 0; } }

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

import java.util.*; public class Main { public static int findDifference() { List lst = Arrays.asList(8, 3, 1, 9, 7, 2); // Your code here return 0; } }

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

import java.util.*; public class Main { public static void reverseArray() { int[] arr = {1, 2, 3, 4, 5}; // Your code here } }

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

import java.util.*; public class Main { public static boolean isSorted() { List lst = Arrays.asList(1, 2, 3, 4, 5); // Your code here return false; } }

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

import java.util.*; public class Main { public static String findLongestWord() { List lst = Arrays.asList("apple", "banana", "cherry", "kiwi"); // Your code here return ""; } }

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

import java.util.*; public class Main { public static int sumOfEvenNumbers() { int[] arr = {1, 2, 3, 4, 5, 6}; // Your code here return 0; } }

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

import java.util.*; public class Main { public static List rotateLeft() { List lst = Arrays.asList(1, 2, 3, 4, 5); int k = 2; // Your code here return lst; } }

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

import java.util.*; public class Main { public static List mergeWithoutDuplicates() { List lst1 = Arrays.asList(1, 2, 3); List lst2 = Arrays.asList(3, 4, 5); // Your code here return lst1; } }

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

import java.util.*; public class Main { public static boolean areIdentical() { int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; // Your code here return false; } }

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

import java.util.*; public class Main { public static int firstRepeated() { List lst = Arrays.asList(1, 2, 3, 2, 1); // Your code here return 0; } }

Output

Click Run Button to view compiled output

ad vertical

1 of 3