Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 92 fix this that its tempting to assume that this argument is a String but we neither know that nor need to know that Lets consider the remaining options in light of this and assume that the type of the argument s is defined by some imaginary interface HasLength The argument and return types of a function are defined independently so a Function HasLength Integer would match our lambda successfully and option A is a correct answer The argument type of an IntFunction is a primitive int therefore option B can be ruled out We already eliminated option C so well ignore it now The ToIntFunction takes an argument of a generic type and returns a primitive int so ToIntFunction HasLength fits our lambda perfectly too Its worth noting that this is in a sense the best fit because it avoids the autoboxing of the return value but the question doesnt get into such value judgments But clearly option D is correct too The argument type and return type for a UnaryOperator are constrained to be the same We already know the return is int so the only way that UnaryOperator might possibly match our lambda is as UnaryOperator Integer That would work only if the Integer object has a length method Of course it doesnt so option E cannot work either article Simon Roberts joined Sun Microsystems in time to teach Suns first Java classes in the UK He created the Sun Certified Java Programmer and Sun Certified Java Developer exams He wrote several Java certification guides and is currently a freelance educator who teaches at many large companies in Silicon Valley and around the world He remains involved with Oracles Java certification projects
You must have JavaScript enabled to view digital editions.