LearnFrenzy
Sign in Take a free test

Generics · Exercise 3 of 4

Generics Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program breaks a generics rule that javac enforces. Choose the option that names the real problem.

Question 1 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Factory<T> {
    T create() {
        return new T();
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Factory<StringBuilder>().create());
    }
}
A create must be static
B T is erased at run time, so new T() is impossible; pass a Supplier<T> (e.g. StringBuilder::new) or a Class<T>
C StringBuilder has no constructor
D Factory must be abstract
Question page & discussion
Question 2 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    static void addBonus(List<? extends Number> scores) {
        scores.add(10);
    }

    public static void main(String[] args) {
        addBonus(new ArrayList<Integer>());
    }
}
A 10 must be 10.0
B The list could be a List<Double>, so nothing (except null) can be added through ? extends Number; use List<? super Integer> to add Integers
C ArrayList cannot be passed
D add needs an index
Question page & discussion
Question 3 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.List;

public class Main {
    static boolean isStringList(Object o) {
        return o instanceof List<String>;
    }

    public static void main(String[] args) {
        System.out.println(isStringList(List.of("a")));
    }
}
A List cannot be used with instanceof
B Because of erasure, the element type is unknown at run time, so an Object cannot be safely tested against List<String>; test List<?> and check the elements
C isStringList must be generic
D List.of is immutable
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Registry<T> {
    static T defaultValue;
    T value;
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Registry<String>().value);
    }
}
A value must be static
B A static field is shared by Registry<String>, Registry<Integer> …, so it cannot have the type parameter T
C T must be bounded
D Registry needs a constructor
Question page & discussion
Question 5 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.List;

public class Main {
    static int total(List<Integer> nums) { return nums.size(); }
    static int total(List<String> words) { return words.size(); }

    public static void main(String[] args) {
        System.out.println(total(List.of(1, 2)));
    }
}
A total must return long
B Both methods erase to total(List), so they clash; give them different names
C Static methods cannot be overloaded
D List.of needs a type argument
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Buffer<T> {
    private final T[] slots = new T[10];
    T get(int i) { return slots[i]; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Buffer<String>().get(0));
    }
}
A 10 is too small
B Arrays need a known run-time element type, but T is erased; use a List<T>, or (T[]) new Object[10] with care
C get must be static
D slots must not be final
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    static <T> T larger(T a, T b) {
        return a.compareTo(b) >= 0 ? a : b;
    }

    public static void main(String[] args) {
        System.out.println(larger(3, 9));
    }
}
A larger must not be static
B An unbounded T is only known to be an Object, which has no compareTo; declare <T extends Comparable<T>>
C Integers cannot be compared
D The ternary operator is not allowed
Question page & discussion
Question 8 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    static int first(List<? super Integer> list) {
        Integer x = list.get(0);
        return x;
    }

    public static void main(String[] args) {
        List<Number> nums = new ArrayList<>(List.of(1, 2));
        System.out.println(first(nums));
    }
}
A get needs an Integer index
B The list may be a List<Object> or List<Number>, so get returns Object, which cannot be assigned to Integer; use List<? extends Integer> or List<Integer> for reading
C first must be generic
D nums must be List<Integer>
Question page & discussion
Question 9 Hard

The program below does not compile. Which statement correctly describes the problem?

Main.java
class ValidationException<T> extends Exception {
    private static final long serialVersionUID = 1L;
    final T badValue;
    ValidationException(T badValue) { this.badValue = badValue; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("validate");
    }
}
A badValue must be private
B A generic class may not extend Throwable, because catch clauses need the exact type at run time and T would be erased
C Exception classes cannot have fields
D serialVersionUID must be public
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.List;

public class Main {
    static <T> T first(List<T> list) { return list.get(0); }

    public static void main(String[] args) {
        String s = Main.<String>first(List.of(1, 2, 3));
        System.out.println(s);
    }
}
A first must be non-static
B With T explicitly set to String, the argument must be a List<String>, but List.of(1, 2, 3) is a List<Integer>
C List.of cannot hold Integers
D Explicit type arguments are not allowed
Question page & discussion
Up next · Exercise 4Generics Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links