What are some good java interview questions and answers regarding generics and annotations?
views:
668answers:
10Generics: Ask a question designed to see if they understand type erasure.
Annotations: Ask them what their favorite annotation is, and how it works (you don't need a detailed technical explanation, but you're looking for something more than "magic").
Annotations: What are the risks? (the compiler can get into an infinite loop and jam the build process).
Generics: How to build a mixin using generics? (write a generic class that takes a parameter, and then extend it with the sub-class as the parameter).
Also, +1 on type erasure.
Here are a couple on generics:
1)
Question: If I had this method, how would I create a new instance of type T
and assign it to a variable called item
inside this method (assume Class<T>
has a default constructor)?
public static <T> Collection<T> select(Class<T> c, String sqlStatement) {
}
Answer:
T item = c.newInstance();
2)
Question: If you wanted to indicate that the generic extends some base class, but you don't know what the class that extends the base class is going to be until runtime, how could you declare the generic?
Answer: Use a wildcard: <? extends SomeBaseClass>
Question: How can you determine what type of object a generic is using at runtime?
Answer: It is not possible to determine the type because of type erasure.
If you are looking for a rock star Java programmer, you could create quite a few advanced questions from the Generics chapter in Bloch's Effective Java. Things like the homogeneous or heterogenous containers (the bird cage and lion cage examples from one of the java tutorials), erasure, etc.
In less senior folks I primarily look for an understanding of why one would want generics (you'd be surprised how many folks don't appreciate that and believe the Java 2 way of doing things still rules), and things like that.
In annotations, I like asking about the "Override" annotation and its benefits. I also like having a deeper discussion about the pros and cons of annotations and when it is appropriate to use them - I'm not a fan of overzealous meta programming. It's also a good chance to see if someone used Hibernate or JUnit with annotations.
- What does the
Class Enum<E extends Enum<E>>
ensure? - Is
<T> List<? extends T> x()
a useful signature? - Annotation retentions policies and why they are in place?
- Suppose you would like to reuse a class in different contexts would you use annotations or external configuration? (i.e. annotation introduce dependencies).
Harder:
- Examples of a valid generic type that cannot be expressed with the Java type system and will lead to compiler warnings.
- Examples where the java compiler will/will not infere the generic type? (examples of code where the compiler will infere the wrong type)
- What generic type information is not erased and can be retrieved from the byte code? (Super type tokes are one application)
- What's APT (use cases when not to use reflection)?
Here are a few I just made up:
-- [Cagey generics] Would uncommenting any of these lines cause problems? Which ones, if any? Why or why not?
public class Cage<T> { ... }
public class Animal { ... }
public class Bear extends Animal { ... }
// Cage<Animal> c = new Cage<Bear>();
// Cage<Bear> c = new Cage<Animal>();
// Cage<?> c = new Cage<Animal>();
// Cage<Animal> c = new Cage<?>();
-- [Constraints] Only Animals
should go into Cages
. How can we tighten up the class definitions above so that they reflect this new requirement?
-- [Legal troubles] You cannot instantiate an array of a generic type. Thus, something like new List<Animal>[10]
is illegal. Can you think of a scenario where, if this were legal, you would run into trouble?
Answers left as an exercise to the reader -- you're not going to learn anything if you don't figure them out yourself! But here are some hints.
- [Cagey generics]: What does
?
mean? Do you remember the term "covariance"? - [Constraints]: Java lets you constrain the values of a type parameter. Do you remember the syntax for this?
- [Legal troubles]: Suppose you could do
Object[] arr = new List<String>[]
. Can you write some code that puts something intoarr
'sLists
that shouldn't go in there?
"What type of things are annotations good at?" and "what type of things are annotations bad at?" comes to mind.
Annotations are good at meta-programming, but as a possible best-practice, code that worked with the annotations in should still work if you take all of them out.
Or not. Your mileage may vary, but you probably want all of your senior developers to agree on that one.
Since Java 5 came out, I have seen dozens of people not understand why, given an interface I
, and classes A
and B extends A
you can't pass an I<B>
where an I<A>
is required. A lot of people find it counter-intuitive.
To test a person's ability to reason about Generics, then I would first ask them if it is possible to assign an I<B>
to an I<A>
reference as described above. If not, why not? If they get it wrong, tell them they're wrong and ask them to try to fill in the blanks here to show why this example would be type un-safe if it could compile:
//...
List<String> list = new LinkedList<String>();
someMethod(list);
//blank 1
}
public void someMethod(List<Object> list) {
//blank 2
}
At this point it should be pretty easy and I would be a bit worried if they couldn't construct such an example. An example is
//blank 1
String item = list.get(0);
//blank 2
list.add(Integer.valueOf(5));
- Generic:
Q: What is the difference between a Hashmap and a Hashtable?
A: Hashtable is synchronized, Hashmap is not.
- Annotations:
Q: Describe serializing java objects with the javax.xml.bind.Marshaller interface and annotations.
A: Describing something like this in a meaningful context ought to be acceptable:
@XmlRootElement
class Employee {
...
}