#248 PECS: Reading from and Writing to Generic Collections

Companies:
Alfa-Bank
Themes:
JavaCollections & GenericsPractice

Which methods do not cause a compilation error?

public class Main {

    public static void main(String[] args) {

    }

    public static <T> T getFirst(List<? super T> list) {
        return list.get(0);
    }

    public static <T> T getFirst(List<? extends T> list) {
        return list.get(0);
    }

    public static <T> void putLast(List<? super T> list, T t) {
        return list.add(t);
    }

    public static <T> void putLast(List<? extends T> list, T t) {
        return list.add(t);
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in