Java Programming Exercises: Beginner to Advanced Practice
Table of Contents
Java is one of the most consistently in-demand languages in the UK tech market. Developers who can write clean Java code are wanted in enterprise software, fintech, e-commerce back-ends, and web application development — the kinds of systems that underpin the digital products ProfileTree builds for clients across Northern Ireland and beyond.
The trouble is that reading about Java and being able to write it are two very different things. This guide is structured around practical Java programming exercises that move you from first principles through to the kind of object-oriented design you would encounter in a real project.
Work through these in order if you are starting out. If you are more experienced, use the intermediate and advanced sections as a refresher or gap-fill. Before diving in, if you are still deciding whether Java is the right language to learn, our guide to identifying the right programming language covers the trade-offs clearly.
Basic Java Exercises
The beginner section covers the language’s building blocks. These exercises are not trivial. Working through them carefully, without copying answers, builds the muscle memory that makes more complex work possible later. If you are entirely new to programming, our introduction to what a programming language actually is is a useful starting point before tackling the exercises below.
Hello World and Output
Every Java journey starts with printing to the console. The goal is not just to make the program run; it is to understand the structure you are working within: the class declaration, the main method signature, and the System.out.println call. Once you are comfortable with that structure, extend the exercise by printing multiple lines and experimenting with string concatenation.
java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Variables and Data Types
Write a program that declares variables of different types — int, double, String, and boolean — assigns them values, and prints them. Then write a second version that takes user input using the Scanner class and stores it in the appropriate type. This exercise catches a surprising number of learners out: Java is strongly typed, and the compiler is unforgiving about mismatches.
Arithmetic Operators
Build a simple calculator that takes two numbers and an operator as inputs and returns the result. Include division, and handle the case where the divisor is zero. This introduces conditional logic alongside arithmetic and is a useful pattern for any developer working on business logic later.
Control Flow: If-Else and Switch
Write exercises that classify numbers as positive, negative, or zero, determine whether a year is a leap year, and convert a numerical score into a letter grade. These exercises teach you to think in conditions, which is fundamental to writing application logic, whether you are working in Java, Python, or any other language. If you are curious about how Java compares to other languages used in web projects, our overview of programming languages for e-commerce websites gives useful context.
Loops: For, While, and Do-While
Loop exercises are where most beginners start to feel fluent. Good starting tasks include printing a multiplication table, summing numbers from 1 to n, and printing Fibonacci numbers up to a given limit. Once you can write a clean loop without thinking about the syntax, move on.
Intermediate Java Exercises
These exercises assume you are comfortable with the basics. They introduce data structures, more complex logic, and the object-oriented concepts that underpin Java. This is where most of the work happens for developers aiming at junior roles.
Arrays and ArrayLists
Arrays are the simplest data structure in Java and the one you will encounter most often in interview questions. Start with exercises like finding the largest and smallest values in an array, reversing an array in place, and removing duplicates. Then move to ArrayList, which is more flexible and closer to what you would use in production code. Sorting a list of names alphabetically, filtering a list by a condition, and merging two sorted arrays are all good practice tasks.
String Manipulation
String exercises are a staple of Java technical interviews. Practise reversing a string without using the built-in reverse method, checking if a string is a palindrome, counting vowels, and replacing substrings. Spend time with StringBuilder rather than String concatenation in loops — this is a common inefficiency that experienced code reviewers notice immediately.
Methods and Scope
Write programs that are deliberately split into multiple methods. A good exercise: build a number guessing game where one method generates the random number, another checks the guess, and a third handles the loop. This is less about the logic and more about learning to organise code. Clean method separation is something that distinguishes a junior developer who has done real project work from one who has only completed syntax exercises.
Object-Oriented Programming: Classes and Objects
OOP exercises are where Java programming really begins. Write a simple BankAccount class with fields for the account holder’s name, balance, and account number. Add methods for deposit, withdrawal, and balance inquiry. Then write a second class that creates multiple BankAccount objects and simulates a series of transactions. This exercise covers encapsulation, constructors, and method calls in a realistic context — the kind of scenario that appears regularly in technical interviews for entry and junior-level roles.
Inheritance and Polymorphism
Extend the BankAccount exercise by creating a SavingsAccount subclass that overrides the withdrawal method to check a minimum balance condition. Add a CurrentAccount subclass with an overdraft limit. Then write a method that accepts a BankAccount object and calls the withdrawal method — this is polymorphism in practice.
Understanding this deeply matters because it underlies how frameworks like Spring Boot structure their codebases. Developers who have worked through our guide to socket programming in Java will recognise how the same class-and-method patterns appear in networked application code.
Exception Handling
Wrap your withdrawal method in a try-catch block and throw a custom InsufficientFundsException when the balance would go negative. Exception handling is not a niche topic: it is something any competent Java developer needs to use correctly. Practise distinguishing checked from unchecked exceptions and understand when to throw versus when to catch.
Advanced Java Exercises

Advanced exercises are less about learning new syntax and more about applying what you know to solve harder problems. They mirror the kind of thinking you would need for a technical interview at a UK software company or when contributing to a professional codebase.
Data Structures: Stacks, Queues, and HashMaps
Implement a stack using an ArrayList and use it to check whether a string of brackets is balanced — a genuinely common interview question. Implement a queue and use it to simulate a basic job scheduling system. Then work with HashMaps: count the frequency of each word in a paragraph, group a list of products by category, and build a simple in-memory lookup table. The Streams API is worth introducing at this stage to filter and map collections in fewer lines of code.
Algorithm Design
Binary search, bubble sort, and merge sort are the standard algorithms to implement from scratch. Do not skip this because libraries handle it for you in production. Understanding how binary search achieves O(log n) performance helps you make better decisions about data structure choices in real applications, and interviewers test this directly. Once you have implemented the standard set, move to dynamic programming basics: the coin change problem and the longest common subsequence are good starting points.
Project-Based Exercises
The most useful advanced practice is building small, complete programs. A library management system that allows books to be added, borrowed, and returned tests class design, collections, and exception handling together. A simple contact book that reads from and writes to a text file introduces file I/O. A word frequency analyser for a text document uses string processing, HashMaps, and sorting.
These exercises are the ones worth putting on a GitHub profile. Ciaran Connolly, founder of ProfileTree, regularly notes that the web development work his team delivers for clients across Northern Ireland depends on developers who can reason about data flows and write maintainable code — not just complete exercises in isolation.
Our collection of Java programming project ideas takes this further, with project briefs that mirror the kind of problems that appear in real client work.
Practising Java for Technical Interviews
If your goal is employment, the kind of practice that matters shifts slightly from the exercises above. Interview questions test whether you can think clearly under time pressure, explain your reasoning, and produce clean code from scratch without an IDE catching your errors.
What to Prioritise
Work through exercises on arrays, strings, and hash tables first — these categories cover the majority of screening questions at UK-based tech companies. Then add tree and graph traversal once you are confident with the fundamentals. LeetCode and HackerRank are the standard platforms for this kind of practice, and both have Java-specific tracks.
Quality Over Volume
A GitHub profile showing five well-commented, properly structured projects demonstrates more to a hiring manager than a list of 200 completed exercises. For junior roles at UK tech companies and digital agencies, the ability to explain what your code does — and why you made the choices you did — is often the deciding factor.
There is no fixed number of exercises to target. A rough benchmark used by developers who have made successful junior transitions: be comfortable solving medium-difficulty array and string problems in under 30 minutes, have at least three project-based programs you can walk through confidently, and understand OOP well enough to explain polymorphism and encapsulation with a real example. That combination tends to cover what most UK employers test at entry level.
ProfileTree’s digital training programmes are designed to help professionals develop exactly these kinds of applied technical skills, bridging the gap between conceptual understanding and practical implementation.
Using AI Tools to Learn Java

AI coding assistants like GitHub Copilot and ChatGPT are now a normal part of professional software development. For learners, the risk is obvious: it is easy to let the tool write the answer rather than work through the problem.
The Right Approach to AI-Assisted Practice
Write your own attempt at each exercise first, even if it is wrong. Then ask the AI to explain what your code does, identify where it goes wrong, and suggest why. This reverses the dynamic: instead of receiving an answer, you are interrogating your own thinking with a capable sceptic. You retain the learning; the AI handles the feedback loop that a human tutor would otherwise provide.
This mirrors how development teams integrate AI tools into real project workflows — not as a replacement for developer judgment but as a tool that accelerates feedback and reduces repetitive lookup time. Businesses exploring how AI fits into their technical operations can find a practical overview in our guide to AI transformation services, which covers how organisations across the UK are applying these tools in development and beyond. For a sharper look at how AI is changing content and code detection, our piece on AI content detection is also worth reading alongside this.
Where to Practise Java Online
Several platforms are worth using alongside the exercises in this guide.
Exercism
Exercism offers a test-driven approach where you download a suite of automated tests and write code to pass them. The mentorship track provides feedback from experienced developers, which makes it one of the better free options for serious learners.
HackerRank
HackerRank has a Java-specific domain with problems organised by difficulty and topic. It is particularly useful for interview preparation because problems are time-pressured and syntax-strict, replicating the conditions of a real technical screen.
CodingBat
CodingBat is worth mentioning for complete beginners. The exercises are small and focused, the feedback is immediate, and the progression is well-structured for people still getting to grips with what the language does.
For reference documentation, Oracle’s official Java documentation remains the most reliable source. When you do not understand what a method does, go there before a third-party tutorial site. If you prefer structured learning before practising through exercises, our guide to learning Java online covers the best free and paid courses available.
Connecting Java Skills to Real Projects

Java powers a significant proportion of the enterprise web applications, API layers, and backend systems that support UK business software. Understanding how it fits into the broader development landscape matters as much as the syntax itself.
Developers who explore Java socket programming understand how networked applications communicate — a foundation for building web services and APIs. Those who study what the World Wide Web actually is gain the broader architectural context that makes application-layer development more meaningful. And developers curious about how Java fits within the full set of technologies behind a modern website will find our piece on the three core web technologies useful for positioning their skills.
The progression from exercises to projects to professional work is not automatic. It requires deliberate practice, exposure to real codebases, and some understanding of how software is built in teams. Businesses in Northern Ireland and across the UK looking to build web applications or develop their technical teams can speak with ProfileTree’s web development team to discuss a realistic engagement.
Conclusion
Java programming exercises are most useful when treated as a progression rather than a checklist. Starting from syntax fundamentals, moving through OOP design, and arriving at project-based work builds the practical understanding that translates into employment and real development contributions. The platforms listed in this guide provide the environment to practise; the structure above provides the roadmap.
For businesses and professionals looking to build stronger technical capabilities, ProfileTree’s digital training services cover both foundational skills and the applied tools that matter in modern development teams.
FAQs
What Java exercises should a complete beginner start with?
Start with Hello World, then progress to variables, data types, and basic arithmetic. Once you can write a simple calculator and a loop-based program without syntax errors, move on to arrays and string manipulation. The goal of beginner exercises is fluency with the language’s structure, not problem-solving complexity.
How do Java programming exercises help with coding interviews?
Technical interviews for Java roles typically test array manipulation, string processing, basic algorithm design, and OOP understanding. Practising these categories through structured exercises builds the pattern recognition needed to approach unfamiliar problems methodically. The goal is to internalise common problem structures so you can focus on the logic rather than the syntax under pressure.
What is the difference between beginner and intermediate Java exercises?
Beginner exercises cover syntax, control flow, and basic input and output. Intermediate exercises introduce object-oriented design, data structures like ArrayLists and HashMaps, and more complex method structures. The shift is from making the program run to designing the program well.
Can I learn Java just by doing exercises without a course?
Exercises alone are not enough. You need a baseline understanding of Java syntax and object-oriented concepts before exercises become productive. Free resources like Oracle’s official tutorials or a structured beginner course on Coursera or Udemy provide that foundation. Once you have it, exercises are the most effective way to build fluency.
How do I know when I am ready to move from exercises to real projects?
When you can complete intermediate exercises — particularly OOP challenges like the BankAccount example — without referring to syntax documentation, you are ready to start a simple project. The shift to project work matters because it requires you to make design decisions rather than follow a prescribed problem structure.