🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
    }
}
  1. A.pen5
  2. B.35
  3. C.8
  4. 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 →