Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Return Types Exam Question

John Cochrane
Greenhorn
Posts: 8
Hi,

I'm studying for my Sun Certified Programmer exam and am going through some test questions, one of which I can't figure out at all. It goes as follows:

14 long test (int x, float y) {
15
16 }

which two of the follwoing lines, inserted independatly, at line 15 would not compile? (Choose two).

A. return x;
B. return (long) x/y;
C. return (long) y;
D. return (int) 3.14d;
E. return (x/y);
F. return x/7

The answers are B and E. I can't understand what the difference is between E and F! Surely they both return a float???

Cheers,

John

Vijayendra V Rao
Ranch Hand
Posts: 195
Originally posted by John Cochrane:
I can't understand what the difference is between E and F!

Option E: division looks like this: integer / floating-point number

Option F: division looks like this: integer / integer

John Cochrane
Greenhorn
Posts: 8
Thanks for the reply but I'm still confused. What if x = 9? The result would be 1.28571... which is a double so wouldn't that cause an error if you try to fit that into a long??

Vijayendra V Rao
Ranch Hand
Posts: 195
will it? Why don't you try the following piece of code?
truncated. If you wish to have a good deal of insight into these conversions, then I would suggest you look Language Specifications. So if you look through the language specs, chapter 5 of it will explain stuff in a very clear manner. Hope this helps

John Cochrane
Greenhorn
Posts: 8
Thanks for the reply. I obviously need to do a bit more reading on the subject :-) I've got the exam tomorrow so I'm panicing a bit now. I tried the link that you gave me but it took me to the api specs. Was this intentional? I can't find a chapter 5 (I'm not that familiar with the java docs).

Vijayendra V Rao
Ranch Hand
Posts: 195
Originally posted by John Cochrane:
Was this intentional? I can't find a chapter 5 (I'm not that familiar with the java docs).

Oh I am very sorry I forgot to mention where you need to navigate to. Let me provide the direct link to that page:

http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html

Layne Lund
Ranch Hand
Posts: 3061
Integer division is a strange creature until you get used to it. The way I think about any operation is that the two operands (on the left and right of the operator) MUST be the same type. The result is also the type of the two operands. In many of the choices as answers to this question, the operands aren't the same type. However, Java has several rules that allows it to convert between primitive types automatically. In the case of x / 7, this conversion is not necessary, though, since both x and 7 are ints. This also means the result will be an int (not a double). So how do we get an int value when x does not divide by 7 evenly? The answer is truncation; the remainder is ignored. So in your example, 9 / 7 is 1! You can see this by compiling and running Vijayendra's code example above.

HTH

Layne
[ January 21, 2005: Message edited by: Layne Lund ]

John Cochrane
Greenhorn
Posts: 8
All is now clear. Thankyou very much for your help :-)