30 Java Basic Exercises for Beginners with Solutions

Master beginner 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 intermediate challenges. Start your journey to Java mastery today!

Learning Objectives:

By the end of these exercises, you will have a solid understanding of Java basics, including variables, loops, conditionals, and methods.

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 sum of two numbers

Required Input:

4, 5

Expected Output:

9

Code In Java

public class Main { public static int sumTwoNumbers() { int a = 4; int b = 5; // Your code here return 0; } }

Output

Click Run Button to view compiled output

2. Determine if a number is even or odd

Required Input:

7

Expected Output:

Odd

Code In Java

public class Main { public static String checkEvenOdd() { int n = 7; // Your code here return ""; } }

Output

Click Run Button to view compiled output

3. Find the largest of three numbers

Required Input:

3, 9, 2

Expected Output:

9

Code In Java

public class Main { public static int largestOfThree() { int a = 3; int b = 9; int c = 2; // Your code here return 0; } }

Output

Click Run Button to view compiled output

4. Reverse a string

Required Input:

hello

Expected Output:

olleh

Code In Java

public class Main { public static String reverseString() { String s = "hello"; // Your code here return ""; } }

Output

Click Run Button to view compiled output

5. Check if a string is a palindrome

Required Input:

madam

Expected Output:

true

Code In Java

public class Main { public static boolean isPalindrome() { String s = "madam"; // Your code here return false; } }

Output

Click Run Button to view compiled output

6. Calculate the factorial of a number

Required Input:

5

Expected Output:

120

Code In Java

public class Main { public static int factorial() { int n = 5; // Your code here return 0; } }

Output

Click Run Button to view compiled output

7. Find the sum of all numbers in a list

Required Input:

[1, 2, 3, 4, 5]

Expected Output:

15

Code In Java

public class Main { public static int sumOfList() { int[] lst = {1, 2, 3, 4, 5}; // Your code here return 0; } }

Output

Click Run Button to view compiled output

8. Count the number of vowels in a string

Required Input:

education

Expected Output:

5

Code In Java

public class Main { public static int countVowels() { String s = "education"; // Your code here return 0; } }

Output

Click Run Button to view compiled output

9. Generate the Fibonacci sequence up to n terms

Required Input:

5

Expected Output:

0 1 1 2 3 

Code In Java

public class Main { public static int[] fibonacci() { int n = 5; // Your code here return new int[0]; } }

Output

Click Run Button to view compiled output

10. Find the second largest number in a list

Required Input:

[4, 3, 1, 2]

Expected Output:

3

Code In Java

public class Main { public static int secondLargest() { int[] lst = {4, 3, 1, 2}; // Your code here return 0; } }

Output

Click Run Button to view compiled output

ad vertical

1 of 3