Graded A+
What is the main purpose of Java generics?
A) To make code execute faster
✔✔B) To provide type safety and eliminate the need for explicit casting
C) To allow multiple inheritance
D) To improve garbage collection
Which syntax correctly defines a generic class?
A) `class Box<T> { }`
✔✔B) `class Box<T> { private T item; }`
C) `class <T> Box { }`
D) `class Box { T item; }`
What is the advantage of using bounded type parameters in generics?
✔✔A) They restrict the types that can be used as arguments
B) They make the program run faster
C) They remove the need for constructors
1
,D) They allow access to private fields of other classes
Which wildcard allows any type to be passed to a method?
A) `<T>`
✔✔B) `<?>`
C) `<? extends T>`
D) `<? super T>`
Which of the following correctly defines a generic method?
✔✔A)
```java
public <T> void print(T value) {
System.out.println(value);
}
```
B)
```java
public void print<T>(T value) {
2
, System.out.println(value);
}
```
C)
```java
public T print(T value) {
System.out.println(value);
}
```
D)
```java
public void print(T value) {
System.out.println(value);
}
```
Which of the following correctly declares a generic interface?
✔✔A) `interface DataContainer<T> { T getData(); }`
3