Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What does a generic class provide over a non-generic class in Java?

  1. Faster execution time

  2. Dynamic type casting

  3. Runtime type safety

  4. Compile-time type safety

The correct answer is: Compile-time type safety

A generic class provides compile-time type safety, which ensures that all objects being used in the code are of the specified type. This prevents potential runtime errors that can occur with non-generic classes, where type casting must be done manually and can lead to unexpected results. While dynamic type casting (B) may provide flexibility, it can also make the code more error-prone and may also result in slower execution time compared to generic classes. Similarly, runtime type safety (C) only checks for type correctness during runtime, which can also lead to unexpected errors. Faster execution time (A) is not a feature of generic classes, as it only provides type checking and has no impact on the actual performance of the code.