Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how is this answear correct?

 
umesh atada
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question found in one of the mock exam?
1. class MyClass {
2. public float getNum() {
3. return 1.2f;
4. }
5. }
6. public class YourClass {
7. public ______ getNum() {
8. return 3;
9. }
10. }
What should be entered in the blank on line 7 that will allow compilation to succeed?
CORRECT ANSWEAR GIVEN IS "float".
but i feel it is "int"
What do u feel?
 
Asma Zafar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i also thimk its an int.
I don't find any reason for it to be float, becuse the 2 classes r not related in any way.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi umesh,
'float' and 'int' would both work; although, based on the returned value an 'int' makes more sense.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Rehan Malik
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first thought was "int" as well.
However, the ONLY possible reason which it MIGHT be "float" is that it's attempting to override the getNum() function but since it doesn't mention any of this, my first instinct would still have been "int"
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
short and byte will work, too.
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the method "return 3", how can it be other than byte, short, or int??
--Farooq
 
Snylt Master
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dunno but it can. The system makes a cast to 3.0!
can anyone explain this?
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SynIt Master,
Your right, widening conversion comes into play, they system will just convert the '3' to '3.0f'.
Looking at the question again, it's not quite clear what's being tested At first I thought it was related to overriding, in which case the return value must be the same but YourClass does not extend MyClass. I'm not sure where the question came from but all in all IMHO it seems a rather pointless one

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SynIt Master,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic