This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
And the options are: a) Will not compile at line 1 and 2 b) will not compile at line 2 c) Has runtime errors. D) Will compile E) Will not compile at line 1
The correct answer is given as (b) but explanation provided was "List is required" My question is, is this explanation justified? I understand that if we return ArrayList where List is required compiler won't mind as covariant returns are allowed. Moreover, if I change line 2 to return new ArrayList<T>();, code compiles just fine. So what must be the correct way to say why line 2 does not compile?
First up thanks for trying the exam. Although the answer says a List of type T is required it is just one of the right answers. Since a List is an interface you would certainly have to use a linked list or an array list or something else that implements List when you pass the List reference as a return parameter ( the object the List reference points to I mean). The exception being of course when you pass a List reference pointing to null.
You are right about the co variant return part. You could as well return an ArrayList if you wish, as of java 5.
The Inquisition application was not written by me. It was written by Matt. You can explore the site where Inquisition is located to get more details about the project. Only the mock exam that Inquisition hosts was written by me.
The real SCJP exam is pretty straight forward. I tried to set a difficulty level that is a notch or two greater than the real exam.