Meaningless Drivel is fun!*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Doubt in Sun's sample question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Doubt in Sun Watch "Doubt in Sun New topic
Author

Doubt in Sun's sample question

Minu Jain
Ranch Hand

Joined: Mar 24, 2008
Posts: 74
The question below is from the following link:webpage

9) Given an excerpt from an entity:
10. @Entity
11. public class Koala {
12. int children;
13. @Id
14. private Integer id;
15. /* ... */
16. }

The following code was written to find the sum and average number of children across all Koala entities (assume the variable em is bound to a valid EntityManager instance):
20. String query = "SELECT SUM(k.children), AVG(k.children) from Koala k";
21. Query q = em.createQuery(query);
22. Object[] res = (Object []) q.getSingleResult();

Which two statements are correct? (Choose two.)
a) There is a syntax error in the JPQL on line 20.
b) If the variable o is NOT null, the types of the two values in the array are Integer and Double respectively. (*)
c) There is an error in the typecasting on line 22, which results in a runtime or compile-time exception.
d) If the variable o is NOT null, the types of the two values in the array are Double and Double respectively.
e) The syntax of the JPQL statement on line 20 is valid and the code executes without an error. (*)
f) A runtime exception is generated on line 22.

REFERENCE:
Option E and Option B are correct. This is a valid JPQL query. (See 4.8.4 of the JPA Specification.) The Type of the return value is (Object[]) (see 3.6.1 of the JPA Specification) and in this case, the MAX aggregate function returns Integer and AVG a Double (See 4.8.4 of the JPA Specification.)
Option A, C, D and F are incorrect because there is no syntax error in the query or the code.



How can option e and b be correct???
Isn't SUM supposed to return Long when applied to state-fields of integral types???
Also, in line 22, how can getSingleResult() return an array of Object??? So option e also should be incorrect.
Please help............
Thanks in advance!


SCBCD5, SCWCD5, SCJP5
"Even if you're on the right track, you'll get run over if you just sit there."
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
Minu Jain wrote:

How can option e and b be correct???
Isn't SUM supposed to return Long when applied to state-fields of integral types???
Also, in line 22, how can getSingleResult() return an array of Object??? So option e also should be incorrect.
Please help............
Thanks in advance!


SUM aggregate function can return the following types: Long, Double, BigInteger and BigDecimal depending on the
field type.

Which means, Theoretically (i've never tried) that since you can Narrow a Long into an Integer, the result could be
an int as well. How this works under the hood, i have no idea, but since the field "children" is marked as "int" i would
expect to get an "int" as a result of the SUM aggregate function...

For the second question, is interesting, ...

I will try that, but maybe is a way to avoid being thrown the javax.persistence.NonUniqueResultException when
the result is not unique... even though there will be only one Row returned from the query, there are actually two
columns in a real database query...one with the SUM and one with the result of the AVG...which are not Koala
entities and i'm not sure how they are seen from the entity manager...

Dave


SCJP, SCBCD
Davide Crudo
Ranch Hand

Joined: Sep 08, 2009
Posts: 62
...Actually now I've found the rule:

Any Query with SELECT cause, if the select, like in your case, contains multiple select expressions like:



The rule says that the returned result type is of type Object[].

So, in the specific case, the single query result, has one singe result which is of type Object[] and not of type Object.
This is why, you get an Array as a single result and not multiple results in an Array!

Dave
Minu Jain
Ranch Hand

Joined: Mar 24, 2008
Posts: 74
Thank you so much.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Sun's sample question
 
Similar Threads
Sun Cirtification
Answers of Sun's Free Proficiency Assessment
Doubt in K&B - Generics & Collections - Question 8
A question form Sun's Free Proficiency Assessment System
A question aboute EJB-QL