Java Exercises for Beginners with Solutions: 100+ Practice Problems with Answers

Robert Jul 13, 2026

Embarking on your Java learning journey? Welcome! Practicing Java with exercises is an excellent way to grasp concepts and build a solid foundation. In this guide, we've curated a list of beginner-friendly Java exercises, complete with solutions to help you learn and grow. Let's dive right in!

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀

Whether you're new to programming or transitioning from another language, consistent practice is key to mastering Java. This article caters to both beginners and those refreshing their memory with fundamental exercises and solutions.

30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners

Java Basics: Data Types and Operators

The bedrock of Java programming lies in understanding data types and operators. Let's explore some exercises that delve into these basics.

Java Homework Help – Step-by-Step Coding Solutions & Guides
Java Homework Help – Step-by-Step Coding Solutions & Guides

Start by brushing up on your knowledge of Java's primitive data types: int, float, double, boolean, char, and String. Remember, only int, double, float, and boolean are used for numerical calculations; char and String, for text manipulation.

Exercise 1: Temperature Converter

a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '

Create a simple program that converts Fahrenheit to Celsius. The formula is: C = (F - 32) * 5/9.

Here's a simple solution using double for better precision:

import java.util.Scanner; public class TempConverter { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter temperature in Fahrenheit: "); double fahrenheit = input.nextDouble(); double celsius = (fahrenheit - 32) * 5/9; System.out.println(fahrenheit + " degrees Fahrenheit is equivalent to " + celsius + " degrees Celsius."); } }

Exercise 2: Arithmetic Operations

Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course

Write a program that takes two integers as input and performs basic arithmetic operations (addition, subtraction, multiplication, and division).

Here's a straightforward solution using int for the inputs and double for the results (to account for potential decimal outputs):

import java.util.Scanner; public class Arithmetic { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first number: "); int num1 = input.nextInt(); System.out.print("Enter second number: "); int num2 = input.nextInt(); System.out.println("num1 + num2 = " + (num1 + num2)); System.out.println("num1 - num2 = " + (num1 - num2)); System.out.println("num1 * num2 = " + (num1 * num2)); System.out.println("num1 / num2 = " + (double)num1 / num2); } }

Control Structures: Looping and Branching

Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026

Now that you've conquered the basics, it's time to explore Java's control structures. Let's practice with some looping and branching exercises.

Familiarize yourself with Java's loop types: for, while, and do-while, and understand when to use each. Additionally, grasp the power of branching with if, else if, and switch statements.

the info sheet shows how to learn in 30 days
the info sheet shows how to learn in 30 days
Java Practice Problems & Solutions for Beginners ☕
Java Practice Problems & Solutions for Beginners ☕
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Java: 100 Practical Projects & Exercises for All Levels! 👨‍💻
Master Java: 100 Practical Projects & Exercises for All Levels! 👨‍💻
Java Program Structure Explained ☕ | Beginner-Friendly Guide
Java Program Structure Explained ☕ | Beginner-Friendly Guide
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Echo Answered! Outstanding Python, Java, JavaScript Pros: Call Heard – Order for Resonant Results!
Echo Answered! Outstanding Python, Java, JavaScript Pros: Call Heard – Order for Resonant Results!
Java Learning Roadmap 2026 ☕🚀
Java Learning Roadmap 2026 ☕🚀
Steps to becoming a java pro
Steps to becoming a java pro
String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
java array operations
java array operations
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
30 Days of Java – Day 24 | Time Complexity Explained for Beginners (Big-O)
30 Days of Java – Day 24 | Time Complexity Explained for Beginners (Big-O)
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
Java Project Ideas For Practice 🔥🚀
Java Project Ideas For Practice 🔥🚀

Exercise 3: Guess the Number

Create a simple guessing game. The computer picks a random number, and the user has to guess it. Provide hints like ' Higher' or 'Lower' until the user guesses correctly.

Here's a solution using Scanner, Random, and a while loop:

import java.util.Random; import java.util.Scanner; public class GuessTheNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand = new Random(); int secretNumber = rand.nextInt(100) + 1; int guess; do { System.out.print("Guess a number between 1 and 100: "); guess = input.nextInt(); if (guess < secretNumber) System.out.println("Too low! Try again."); else if (guess > secretNumber) System.out.println("Too high! Try again."); } while (guess != secretNumber); System.out.println("Congratulations! You guessed the number!"); } }

Exercise 4: Calculate Factorial

Write a program that calculates the factorial of a given number using a for loop.

Here's a simple solution using a for loop and an int to store the result:

import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a non-negative integer: "); int number = input.nextInt(); int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; } System.out.println("The factorial of " + number + " is " + factorial); } }

Keep practicing and exploringJava! As you tackle more exercises, you'll build a strong foundation in Java programming. Happy coding!