Quiz di Java: Type Casting

This Java type casting quiz covers widening and narrowing conversions between primitives and casts between objects. You will predict what casts truncate or overflow, spot code that needs an explicit cast and see when a cast fails at runtime.

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 {
    public static void main(String[] args) {
        double d = 9.99;
        int i = (int) d;
        System.out.println(i);
    }
}
Mostra la risposta

A. 9 Casting a double to int truncates toward zero, it never rounds. Use Math.round(d) if you want 10.

Cosa copre questo quiz

  • Widening conversions vs narrowing casts
  • Truncation and overflow when narrowing
  • Overflow before widening in int arithmetic
  • Rounding with Math.round
  • Casting objects and ClassCastException

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.