Question 1
Why are generics used in Java?
To increase file size
For backward compatibility
For compile-time type safety
For runtime type detection
Question 2
Which syntax correctly defines a generic class?
class Test
generic class Test {}
class
Test
Question 3
What is the output of this code?
class Box<T> {
T value;
void set(T val) { value = val; }
T get() { return value; }
}
Compilation error
Generic class Box
Box with fixed type
None of the above
Question 4
Which is a benefit of using generics in Java?
Slower execution
Manual type casting
Type safety and code reusability
Runtime type checks
Question 5
Which of the following is a valid generic method declaration?
public void print(T t)
public
public T print(T t)
void
Question 6
Generics work only with which type of data?
Primitive types
Reference types
Static data
Enums only
There are 6 questions to complete.