Quiz di Java: try, catch & finally

This Java quiz walks through try, catch and finally, including the cases that surprise people, like a return inside finally. You will also trace try-with-resources and see when resources get closed.

Medio 10 domande ~6 minuti Superato con il 80% Java 17

Scegli una modalità

Stai giocando come ospite: accedi per salvare XP e serie.

Domanda di esempio

Domanda 1 di 10

What does this print?

public class Main {
    static String test() {
        try {
            return "try";
        } finally {
            System.out.print("finally ");
        }
    }

    public static void main(String[] args) {
        System.out.println(test());
    }
}
Mostra la risposta

B. finally try The return value is computed first, then finally runs before the method actually returns. So finally is printed inside test(), and try is printed afterwards by main.

Cosa copre questo quiz

  • The order of try, catch, finally and the code after them
  • finally after return, and return inside finally
  • Returning a primitive vs an object from try
  • Catch block order and exceptions thrown from catch
  • Try-with-resources, AutoCloseable and close order

Domande frequenti

Serve un account?

No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.

Posso rifarlo?

Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.