• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Return Types Exam Question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Figure out the answer?
 
John Cochrane
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

You get this page after a series of navigations from the API pages
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All is now clear. Thankyou very much for your help :-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic