Java Quiz Day 34

Last Updated :
Discuss
Comments

Question 1

Why should collections use generics?

  • A

    To allow all data types

  • B

    To ensure type safety


  • C

    To make them faster

  • D

    To reduce memory

Question 2

What happens when we use raw types in collections?

  • A

    Type safety is ensured


  • B

    Compilation error

  • C

    Possible ClassCastException

  • D

    Generics are enforced


Question 3

What does type erasure mean in Java generics?

  • A

    Types removed at runtime

  • B

    Generics used at runtime

  • C

    Types checked at runtime


  • D

    Compiler adds extra type

Question 4

Which is a valid declaration for a generic List?

  • A

    List list = new ArrayList();


  • B

    List list = new ArrayList();


  • C

    List list = new ArrayList();

  • D

    List list = new ArrayList();

    Question 5

    What does the following code produce?

    Java
    List<String> list = new ArrayList<>();
    list.add("Java");
    String val = list.get(0);
    


    • A

      Compilation error

    • B

      Runtime error

    • C

      Java

    • D

      null


    Question 6

    Which of the following is NOT true about generics with collections?

    • A

      Prevents runtime casting

    • B

      Enforces compile-time safety

    • C

      Increases chance of ClassCastException

    • D

      Makes code cleaner

    Tags:

    There are 6 questions to complete.

    Take a part in the ongoing discussion