Java Quiz Day 32

Last Updated :
Discuss
Comments

Question 1

Why are generics used in Java?

  • A

    To increase file size

  • B

    For backward compatibility

  • C

    For compile-time type safety

  • D

    For runtime type detection

Question 2

Which syntax correctly defines a generic class?

  • A

    class Test {}


  • B

    generic class Test {}

  • C

    class Test {}

  • D

    Test class {}

Question 3

What is the output of this code?

Java
class Box<T> {
    T value;
    void set(T val) { value = val; }
    T get() { return value; }
}
  • A

    Compilation error

  • B

    Generic class Box

  • C

    Box with fixed type

  • D

    None of the above


Question 4

Which is a benefit of using generics in Java?

  • A

    Slower execution


  • B

    Manual type casting

  • C

    Type safety and code reusability


  • D

    Runtime type checks

Question 5

Which of the following is a valid generic method declaration?

  • A

    public void print(T t)

  • B

    public void print(T t)

  • C

    public T print(T t)

  • D

    void print(T t)

Question 6

Generics work only with which type of data?

  • A

    Primitive types

  • B

    Reference types

  • C

    Static data

  • D

    Enums only

Tags:

There are 6 questions to complete.

Take a part in the ongoing discussion