Java-Quiz: static & final

This Java quiz checks what static and final really mean for fields, methods, classes and variables. You will predict output with shared counters and static blocks, and spot code where static or final makes the compiler say no.

Mittel 10 Fragen ~6 Minuten Bestanden ab 80% Java 17

Modus wählen

Du spielst als Gast: Melde dich an, damit XP und Serie gespeichert werden.

Beispielfrage

Frage 1 von 10

What does this print?

public class Main {
    static class Ticket {
        static int count = 0;
        int id;

        Ticket() {
            count++;
            id = count;
        }
    }

    public static void main(String[] args) {
        Ticket a = new Ticket();
        Ticket b = new Ticket();
        Ticket c = new Ticket();
        System.out.println(a.id + " " + Ticket.count);
    }
}
Lösung zeigen

C. 1 3 count is static, so all tickets share one counter that reaches 3. id is an instance field, so a keeps the value it copied when it was created, 1.

Darum geht es in diesem Quiz

  • Static fields shared by all objects
  • Why static methods cannot use instance fields
  • Static initializer blocks and when they run
  • final variables, references and fields
  • final classes and constants

Häufige Fragen

Brauche ich ein Konto?

Nein. Jeder kann spielen. Mit Anmeldung werden XP, Serie, Abzeichen und Verlauf gespeichert.

Kann ich es wiederholen?

Ja. Dein Bestwert bleibt erhalten, und Fragen und Antworten werden jedes Mal neu gemischt.