Sample questions
Question 1Easy
What is the PECS rule for wildcards?
- Public extends, Child super
- Producer super, Consumer extends
- Primitive extends, Class super
- 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
- Reading from a list: extends.
- 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);
- No, only null can be added to a ? extends list
- Yes, because 5 is a Number and Integer is a subtype
- Yes, because it autoboxes to Integer
- 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
- Wildcard hides exact type.
- add needs that type.
- 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());
- false
- Compile error
- ClassCastException
- 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
- Type arguments are removed (type erasure).
- Both are
ArrayList. 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));
- true true
- true false
- false true
- 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
A has default retention (CLASS).B is retained at run time.- 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?
- Because Integer is not a subclass of Number, so the types clash
- The compiler does not know the exact element type, so it cannot accept any value except null
- Because List has no add method
- 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
l may be a list of Integer, Double or others.- Adding an
Integer could break a List<Double>. - 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?
- Making the method static so it is callable without an instance
- Calling a private method despite access modifiers
- Removing the method from the class at run time
- 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
- Get Method via getDeclaredMethod.
- setAccessible(true).
- 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?
- new T()
- new T[10]
- new ArrayList<String>()
- 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
- Erasure removes T at runtime.
- Operations needing T fail.
- 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 →