Solved Java exercises (FP2)
Every solved Java exercise on SergioLearns, by topic and in the order of FP2: from syntax, loops and arrays to classes, inheritance, interfaces and collections. Each topic comes with challenges from easy to hard, commented solutions with the program’s real output, an exam-style quiz and its cheat sheet.
sergio@learns:~$ ls -l ~/exercises/java --sort=level
total 16 topics · 48 exercises · 176 quiz questions
[ OK ] every solution is commented and was run before publishing
Exercises by topic
IntermediateFP216 topics
- Syntax.javaSyntaxtheoryguided practicequizcheat sheet
- Variables.javaVariablestheoryguided practicequizcheat sheet
- Conditionals.javaConditionalstheoryguided practicequizcheat sheet
- Loops.javaLoopstheoryguided practicequizcheat sheet
- Methods.javaMethodstheoryguided practicequizcheat sheet
- Arrays.javaArraystheoryguided practicequizcheat sheet
- Strings.javaStringstheoryguided practicequizcheat sheet
- Regex.javaRegextheoryguided practicequizcheat sheet
- Classes.javaClassestheoryguided practicequizcheat sheet
- Inheritance.javaInheritancetheoryguided practicequizcheat sheet
- Exceptions.javaExceptionstheoryguided practicequizcheat sheet
- Interfaces.javaInterfacestheoryguided practicequizcheat sheet
- ComparableAndCloneable.javaComparable and Cloneabletheoryguided practicequizcheat sheet
- Iterators.javaIteratorstheoryguided practicequizcheat sheet
- Collections.javaCollectionstheoryguided practicequizcheat sheet
- LinkedList.javaLinkedListtheoryguided practicequizcheat sheet
How each exercises article works
They all share the same structure: “How to use this article”, the challenges on three levels (basic, intermediate and a final challenge), three “What’s wrong here?” snippets with mistakes people really make, the commented solutions all together at the end, the quiz, the Python Tutor section and the cheat sheet. All the code is compiled and run before it is published: if a solution says it prints something, it is because it does.
In the list above each line is a topic, with how many exercises it has, how many questions its quiz has and its name, which takes you to the article. Next to it you have the topic’s guided practice (a complete program explained step by step), the quiz and the cheat sheet. The list updates itself when I publish a new topic.
Which order to do them in
The order of the list is the order of FP2. First the basics of the language: syntax, variables, control flow, loops, methods, arrays, strings and regular expressions. Then object-oriented programming: classes and objects, inheritance, exceptions, interfaces, Comparable and Cloneable, iterators, collections and a LinkedList.
If you are coming from Python in FP1, the first topics will sound familiar, but don’t skip them: in Java everything has a type, everything lives inside a class and the compiler lets nothing slide. Most mistakes in the first week come from that, not from the logic.
With the exam close, do the final challenge of each topic without looking. If you can do it, move on; if not, go back to that topic’s basic exercises.
How to practice so it sticks
Write every solution yourself, compile it and run it; don’t copy it from the article. If you don’t have the JDK, the Java playground compiles and runs in your browser, and when you want it on your own computer, here is how to install Java on Windows with JDK 21.
Read compiler errors to the end before you change anything: they almost always tell you the line and what was expected. And before you open each “What’s wrong here?”, try to say whether it compiles and, if it does, what it prints. That is exactly what the exam will ask you.
The exam-style quiz
The topics end with an interactive quiz: single-answer, true or false, fill-in-the-gap and put-the-lines-in-order questions, like the ones in the multiple-choice exam. It marks you straight away and explains every answer. Do it at the end, write down the ones you get wrong and come back to them a couple of days later. At the top you also have the question of the day, taken each day from a different Java quiz.
Once you have done a few topics, try the mock exams, timed and marked.
The cheat sheet and Python Tutor (which also speaks Java)
Each topic’s cheat sheet sums up on one page the syntax, the methods you use most and the typical mistakes. For revising the night before it beats rereading the theory. You have them all together in Resources.
Python Tutor is not only for Python: it also runs Java step by step and draws the objects and references in memory. In “See it step by step in Python Tutor” you have the code ready to open. It is much easier to see why two variables can point to the same object, or what an array of objects really holds, once you see it drawn.
Before and after
If you are still on FP1, start with the solved Python exercises; and when you get to IC2, the solved C exercises come next. Everything else for practising is in Practice.

IC2 practical mock exam — flowcharts, fork and threads in C (and the screenshots you can’t leave out)