Skip to content
Justice Tech School

Java Exercises

Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.

Java Introduction

Exercise

Print a line of text.

System.out.("Hi");

Setup and Program Structure

Exercise

Which method does a Java program start in?

public static void (String[] args)

Output

Exercise

Print without a new line.

System.out.("same line");

Comments

Exercise

Start a single line comment.

 this is ignored

Variables

Exercise

Declare a whole number.

 count = 5;

Data Types

Exercise

Which type holds true or false?

 ok = true;

Type Casting

Exercise

Convert text to an int.

int n = Integer.("12");

Operators

Exercise

Which operator gives the remainder?

int r = 17  5;

Strings

Exercise

Compare two strings correctly.

if (a.(b)) {}

The Math Class

Exercise

Find the larger of two numbers.

Math.(3, 9)

Booleans and Conditions

Exercise

Reverse a boolean.

boolean no = yes;

Java String Methods

Exercise

Compare text content.

s.("x")

StringBuilder and Formatting

Exercise

Efficient text builder.

new ()

Wrapper Classes and Autoboxing

Exercise

Convert text to int.

Integer.("12")

var and final

Exercise

Prevent reassignment.

 int N = 5;

Bitwise Operators and Ternary

Exercise

Shift left.

1  3

Variables and Constants

Exercise

Make a constant.

 int MAX = 10;

var Type Inference

Exercise

Infer the type.

 x = 10;

Primitive Types

Exercise

Big integer type.

 big = 10000000000L;

Type Casting

Exercise

Cast to int.

int n = () 9.9;

Math Class

Exercise

Square root.

Math.(16)

String Methods

Exercise

Compare text.

a.(b)

String.format and Text Blocks

Exercise

Format decimals.

"%.f"

StringBuilder

Exercise

Add text.

sb.("x")

Reading Input with Scanner

Exercise

Read an int.

sc.()

Operators

Exercise

Remainder.

7  2

Wrapper Classes and Parsing

Exercise

Parse text.

Integer.("5")

If ... Else

Exercise

Run when the condition is false.

if (ok) {}  {}

Switch

Exercise

Handle all other values.

switch (x) { case 1 -> "a";  -> "b"; }

While Loops

Exercise

Repeat while a condition is true.

 (i < 3) { i++; }

For Loops

Exercise

Walk through each item.

for (String n  names) {}

Break and Continue

Exercise

Skip to the next round.

if (i == 2) ;

Arrays

Exercise

Get the number of items.

int n = nums.;

Switch Expressions and Pattern Matching

Exercise

Arrow in a switch case.

case 1  "a";

Nested Loops and Patterns

Exercise

Leave nested loops.

 outer;

if, else if and else

Exercise

Else branch.

if (x) {}  {}

switch Expressions

Exercise

Arrow case.

case 1  "one";

for Loops

Exercise

Enhanced for.

for (int v  a)

while and do while

Exercise

Run once first.

 { } while (c);

break, continue and Labels

Exercise

Skip a round.

;

Arrays

Exercise

Array length.

a.

Two Dimensional Arrays

Exercise

Row count.

g.

Arrays Utilities

Exercise

Fill all.

Arrays.(a, 0)

Methods

Exercise

A method that returns nothing uses...

static  greet() {}

Method Overloading

Exercise

Overloaded methods share the same...

Methods can have the same  with different parameters

Recursion

Exercise

Name the part that stops the calls.

if (n <= 1) return 1; // the  case

Varargs and Pass by Value

Exercise

Accept many values.

static int f(int n)

Static Methods and Utility Classes

Exercise

Belongs to the class.

 int count;

Methods

Exercise

Return type for no value.

static  run() {}

Method Overloading

Exercise

Same name different params.

Overloading changes the 

Varargs

Exercise

Varargs symbol.

int... 

Recursion

Exercise

Base case result.

if (n <= 1) return ;

Pass by Value

Exercise

Java always passes by ___.

Java always passes by 

Classes and Objects

Exercise

Create an object.

Car c =  Car();

Constructors

Exercise

Refer to the current object.

.name = name;

Access Modifiers and Encapsulation

Exercise

Hide the field from other classes.

 double balance;

Inheritance

Exercise

Inherit from Animal.

class Dog  Animal {}

Polymorphism

Exercise

Which annotation marks a replaced method?


void speak() {}

Abstract Classes and Interfaces

Exercise

A class promises to follow an interface with...

class A  Payable {}

Static Members and Enums

Exercise

A field shared by the whole class is...

 int total;

Object, toString, equals and hashCode

Exercise

Readable text for an object.

public String ()

Inner, Nested and Anonymous Classes

Exercise

Name this kind of class: new Greeter() { } is an ___ class.

new Greeter() { } is an  class

Sealed Classes and Records

Exercise

Immutable data class.

 Point(int x, int y) {}

Classes and Objects

Exercise

Create an object.

Person p =  Person();

Encapsulation

Exercise

Hide a field.

 int balance;

Inheritance

Exercise

Subclass keyword.

class Dog  Animal

Polymorphism

Exercise

Parent holds child.

Shape s = new ();

Abstract Classes and Interfaces

Exercise

Interface keyword.

class A  I {}

static Fields and Methods

Exercise

Class level field.

 int count;

this and super

Exercise

Parent reference.

.who()

Enums

Exercise

Declare an enum.

 Color { RED, BLUE }

Records

Exercise

Declare a record.

 P(int x) {}

Generics

Exercise

Type parameter.

class Box<> {}

equals, hashCode and toString

Exercise

Compare values.

a.(b)

ArrayList

Exercise

Add an item to the list.

list.("x");

HashMap and HashSet

Exercise

Read a value by key.

map.("apple")

Lambdas and Streams

Exercise

The arrow in a lambda is...

(a, b)  a + b

Records and Text Blocks

Exercise

Declare an immutable data class.

 Point(int x, int y) {}

List, Set, Queue and Deque

Exercise

Keep unique sorted values.

Set<String> s = new <>()

Maps In Depth

Exercise

Count with merge.

map.(w, 1, Integer::sum)

Java Generics

Exercise

Bounded type.

<T  Comparable<T>>

Streams and Collectors

Exercise

Keep matching items.

stream.(x -> x > 1)

Optional

Exercise

Value if empty.

opt.("none")

Sorting and Comparators

Exercise

Then sort by a second key.

.(P::n)

Date and Time API

Exercise

Add days to a date.

d.(30)

ArrayList

Exercise

Add an item.

l.("x")

HashMap

Exercise

Read by key.

m.("a")

HashSet

Exercise

Unique collection.

Set<Integer> s = new <>();

Iterating Collections

Exercise

Remove by condition.

l.(n -> n > 1)

Sorting and Comparators

Exercise

Sort by length.

Comparator.(String::length)

Lambdas

Exercise

Arrow symbol.

x  x * 2

Streams

Exercise

Keep items.

stream().(x -> x > 1)

Optional

Exercise

Fallback.

o.("none")

Queue and Deque

Exercise

Add to the front.

d.(0)

Switch Patterns and instanceof

Exercise

Test a type.

o  String s

Exceptions

Exercise

Handle the error.

try { risky(); }  (Exception e) {}

Reading and Writing Files

Exercise

Which exception can file operations throw?

throws 

User Input with Scanner

Exercise

Read a whole line.

String s = in.();

Custom Exceptions and try with resources

Exercise

Resource block syntax.

try (Res r = new Res ) {}

Files with java.nio

Exercise

Read all lines.

Files.(path)

try, catch, finally

Exercise

Always runs.

try {}  {}

Custom Exceptions

Exercise

Your own exception.

class E  Exception

try with resources

Exercise

Auto close.

try ( r = new R())

Files with java.nio

Exercise

Read lines.

Files.(p)

Threads and Executors

Premium exercise. See plans

Synchronization and Atomics

Premium exercise. See plans

CompletableFuture and Virtual Threads

Premium exercise. See plans

Reflection and Annotations

Premium exercise. See plans

Composition, SOLID and Design Patterns

Premium exercise. See plans

JVM, Memory and Garbage Collection

Premium exercise. See plans

Maven and Gradle

Premium exercise. See plans

Testing with JUnit

Premium exercise. See plans

JSON with Jackson or Gson

Premium exercise. See plans

HTTP Client and REST

Premium exercise. See plans

Spring Boot Overview

Premium exercise. See plans

Databases with JDBC and JPA

Premium exercise. See plans

Logging and Configuration

Premium exercise. See plans

Java Best Practices

Premium exercise. See plans

Practice Projects

Exercise

Classic Java book.

Effective