Generics & Reflection Questions with Answers

7 practice questions on Generics & Reflection (Advanced Java) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

What is the PECS rule for wildcards?

  1. Public extends, Child super
  2. Producer super, Consumer extends
  3. Primitive extends, Class super
  4. Producer extends, Consumer super

Answer: D. Producer extends, Consumer super

Explanation

Use ? extends T when you only read items from a structure, and ? super T when you only write items into it.

Step-by-step solution

  1. Reading from a list: extends.
  2. Writing into a list: super.

Why the other options are wrong

  • A. That wording is made up.
  • B. It is reversed.
  • C. Not a real mnemonic.

Question 2Medium

Will this compile?

List<? extends Number> l = new ArrayList<Integer>();
l.add(5);
  1. No, only null can be added to a ? extends list
  2. Yes, because 5 is a Number and Integer is a subtype
  3. Yes, because it autoboxes to Integer
  4. Only with a cast

Answer: A. No, only null can be added to a ? extends list

Explanation

A ? extends Number list is read-only for practical purposes: the compiler cannot guarantee the actual element type, so only null can be added.

Step-by-step solution

  1. Wildcard hides exact type.
  2. add needs that type.
  3. Compiler refuses.

Why the other options are wrong

  • B. The compiler cannot know the exact element type.
  • C. Autoboxing does not help since the capture type is unknown.
  • D. A cast would still not fix the type safety rule.

Tip: PECS: producer extends, consumer super.

Question 3Medium

What does this print?

System.out.println(new ArrayList<String>().getClass()
    == new ArrayList<Integer>().getClass());
  1. false
  2. Compile error
  3. ClassCastException
  4. true

Answer: D. true

Explanation

Generics are erased at compile time, so both objects are plain ArrayList at run time and share the same Class object.

Step-by-step solution

  1. Type arguments are removed (type erasure).
  2. Both are ArrayList.
  3. getClass() returns the same Class.

Why the other options are wrong

  • A. No separate class exists per type argument.
  • B. The expression is valid.
  • C. No cast happens.

Question 4Medium

What is the output?

@interface A {}
@Retention(RetentionPolicy.RUNTIME) @interface B {}
@A @B class C {}
System.out.println(C.class.isAnnotationPresent(A.class) + " " + C.class.isAnnotationPresent(B.class));
  1. true true
  2. true false
  3. false true
  4. false false

Answer: C. false true

Explanation

The default retention is CLASS, so annotation A is not available through reflection. Only B, marked RUNTIME, is visible.

Step-by-step solution

  1. A has default retention (CLASS).
  2. B is retained at run time.
  3. Reflection sees only runtime annotations.

Why the other options are wrong

  • A. A is not visible because of retention.
  • B. The result is not symmetric.
  • D. B is runtime retained, so it is present.

Question 5Hard

Why does void f(List<? extends Number> l) { l.add(1); } not compile?

  1. Because Integer is not a subclass of Number, so the types clash
  2. The compiler does not know the exact element type, so it cannot accept any value except null
  3. Because List has no add method
  4. Because wildcards are only allowed in return types, never in parameters

Answer: B. The compiler does not know the exact element type, so it cannot accept any value except null

Explanation

? extends Number could be List<Double>, so adding an Integer would be unsafe. Such a list is read-only for practical purposes (producer extends).

Step-by-step solution

  1. l may be a list of Integer, Double or others.
  2. Adding an Integer could break a List<Double>.
  3. Only null is accepted.

Why the other options are wrong

  • A. Integer extends Number.
  • C. List has an add method.
  • D. Wildcards are allowed in parameters.

Tip: PECS: producer extends, consumer super.

Question 6Hard

What does method.setAccessible(true) allow in reflection?

  1. Making the method static so it is callable without an instance
  2. Calling a private method despite access modifiers
  3. Removing the method from the class at run time
  4. Making the whole class public for every other package

Answer: B. Calling a private method despite access modifiers

Explanation

It suppresses access checks so private members can be invoked. It may be restricted by modules in newer Java.

Step-by-step solution

  1. Get Method via getDeclaredMethod.
  2. setAccessible(true).
  3. invoke().

Why the other options are wrong

  • A. It cannot change static-ness.
  • C. Nothing is removed.
  • D. Class visibility is not modified.

Question 7Hard

Which statement is legal for generics?

  1. new T()
  2. new T[10]
  3. new ArrayList<String>()
  4. instanceof List<String>

Answer: C. new ArrayList<String>()

Explanation

Because of erasure, you cannot create instances or arrays of a type variable, and cannot test against a parameterised type.

Step-by-step solution

  1. Erasure removes T at runtime.
  2. Operations needing T fail.
  3. Concrete type arguments are fine.

Why the other options are wrong

  • A. A type parameter cannot be instantiated.
  • B. Generic array creation of T is illegal.
  • D. instanceof with a parameterised type is not allowed.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Advanced Java topics · All topics