← Quiz: Java Generics: all 10 questions
Quiz: Java Generics · Question 2 of 10
What does this print? (code)
What does this print?
class Box<T> {
T item;
Box(T item) { this.item = item; }
T get() { return item; }
}
public class Main {
public static void main(String[] args) {
Box<String> b = new Box<>("pen");
Box<Integer> n = new Box<>(5);
System.out.println(b.get().length() + n.get());
}
}
- A.pen5
- B.35
- C.8
- D.Compile error
Answer & explanation
Correct answer: 8
b.get() is a String of length 3, n.get() is the Integer 5, so 3 + 5 = 8.
Can you get the rest right without peeking?
Take the timed Quiz: Java Generics quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →