1. java-hell.de
  2. Topics
  3. Practice Problems
Topic 11 of 11

Java Practice Problems – Coding Challenges

No more fill-in-the-blanks: in the challenges you combine everything you've learned into small programs – from a profile card and a mini calculator to the interview classic FizzBuzz.

The exercises

What you'll learn in this topic

  1. ⚔️ Challenge: Profile Card
  2. ⚔️ Challenge: Number Sequence
  3. ⚔️ Challenge: Mini Calculator
  4. ⚔️ Challenge: Report Card
  5. ArrayList
  6. Try-Catch
  7. FizzBuzz
Exercise 1 of 7

⚔️ Challenge: Profile Card

Time to test what you know! Use everything you've learned so far.

Create variables for a profile card and print them like this:

Name: Java-Dev
Age: 20
Language: Java

The variables are already there. You only need to add the three println lines.

Starter code
void main() {
    String name = "Java-Dev";
    int age = 20;
    String language = "Java";

    // Print these three lines:
    // Name: Java-Dev
    // Age: 20
    // Language: Java
}
Background
Why challenges?

You don't learn programming by reading — you learn it by actively solving problems. Challenges combine several concepts — just like real programming tasks.

In real life you'll never just create a single variable. You'll combine variables with output, calculations and conditions. These challenges get you ready for that.

Exercise 2 of 7

⚔️ Challenge: Number Sequence

Combine loops with conditions!

Print the numbers 1 to 20, but skip every number that is divisible by 3.

Expected output:
1 2 4 5 7 8 10 11 13 14 16 17 19 20

Tip: use continue; — it skips the rest of the current loop pass.

Starter code
void main() {
    for (int i = 1; i <= 20; i++) {
        // Skip numbers that are divisible by 3


        IO.print(i + " ");
    }
}
Background
break and continue

break; exits the whole loop. continue; only skips the current pass and jumps to the next one.

In nested loops, both only affect the innermost loop. With labels you can target outer loops too: outer: for (...) { ... break outer; }

Exercise 3 of 7

⚔️ Challenge: Mini Calculator

Write a method calculate that takes two numbers and an operator and returns the result.

Supported operators: +, -, *

Expected output:

15
3
54
Starter code
int calculate(int a, int b, char op) {
    // Return a+b, a-b or a*b
    // depending on op ('+', '-' or '*')
    return 0;
}

void main() {
    IO.println(calculate(10, 5, '+'));
    IO.println(calculate(10, 7, '-'));
    IO.println(calculate(6, 9, '*'));
}
Background
char — the character type

char stores a single character in 2 bytes (strictly speaking a UTF-16 code unit — some characters like emojis need two of them). Chars go in single quotes: 'A'. Strings go in double quotes: "A".

Since char is a primitive type, you actually can use == to compare here. That doesn't work for strings!

Exercise 4 of 7

⚔️ Challenge: Report Card

Create a class Student with:

  • field name (String)
  • field grade (int, German style: 1 = best, 6 = worst)
  • a constructor that sets both
  • a method result() that returns "passed" if grade ≤ 4, otherwise "failed"

Expected output:

Anna: passed
Tom: failed
Starter code
void main() {
    Student a = new Student("Anna", 2);
    Student b = new Student("Tom", 5);
    IO.println(a.name + ": " + a.result());
    IO.println(b.name + ": " + b.result());
}

class Student {
    String name;
    int grade;

    // Constructor
    // Method result() -> "passed" or "failed"
}
Background
Encapsulation

In real Java code the fields would be private and only accessible through getters/setters. That's called encapsulation — you control how the outside world gets at your data.

Example: a setter for grade could check that the value is between 1 and 6. Without encapsulation, anyone could write s.grade = -5;.

Exercise 5 of 7

ArrayList

Arrays have a fixed size. An ArrayList can grow:

ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
list.size(); // 2

Add the number 30 to the list so its size becomes 3.

Starter code
import java.util.ArrayList;

void main() {
    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(10);
    numbers.add(20);
    // Add 30
    IO.println(numbers.size());
}
Background
Generics and wrapper classes

ArrayList<Integer> — the <Integer> is a generic. It fixes which type the list holds. Generics stop you from accidentally putting the wrong type in.

Why Integer instead of int? Generics only work with object types. Integer is the wrapper class for int. Java converts between the two automatically (autoboxing/unboxing).

Exercise 6 of 7

Try-Catch

Sometimes things go wrong — e.g. when you try to turn "abc" into a number.

With try-catch you catch the error instead of letting your program crash:

try {
    // Code that might go wrong
} catch (Exception e) {
    // What to do if it does
}

Wrap the failing line in try-catch and print Error! if something goes wrong.

Starter code
void main() {
    String text = "abc";

    // Wrap the next line in try-catch
    // On error: print "Error!"
    int number = Integer.parseInt(text);
    IO.println(number);
}
Background
Checked vs. unchecked exceptions

Java has two kinds: checked exceptions (e.g. IOException) must be explicitly caught or declared. Unchecked exceptions (e.g. NumberFormatException, NullPointerException) don't.

In practice, many frameworks use only unchecked exceptions. Java's checked-exception rule is controversial — other languages (Kotlin, C#) simply don't have it.

Exercise 7 of 7

FizzBuzz

The most famous job interview exercise of all time.

Print the numbers 1 to 15, but:

  • Divisible by 3 → Fizz
  • Divisible by 5 → Buzz
  • Divisible by 3 and 5 → FizzBuzz
  • Otherwise → the number

Tip: Check the FizzBuzz case first (divisible by 15)!

Tip 2: % gives you the remainder. 15 % 3 == 0 means "15 is divisible by 3".

Starter code
void main() {
    for (int i = 1; i <= 15; i++) {
        // Your if-else chain goes here

        IO.println(i);
    }
}
Background
FizzBuzz — more than an exercise

FizzBuzz is famous because in job interviews it shows whether someone has the basic programming concepts down. The solution needs loops, conditions and modulo — and the right order of checks.

Fun fact: there are hundreds of creative solutions — with streams, pattern matching, or even without if/else. Google "FizzBuzz Enterprise Edition" for the wildly over-engineered version.

Enough reading. Your turn.

In the tutorial you write the code yourself, hit Run and instantly see whether your program works. No JDK, no IDE, no bullshit.

Practice Practice Problems in your browser