• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from mock exam

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which assignments are legal?
Select all valid answers.
a. long test = 012;
b. float f = -412;
c. int other = (int)true;
d. double d = 0x12345678;
e. short s = 10;
The correct answers are a,b,d,e.
I don't understand how b and d are correct.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b is correct because it is a int value and can be put in float without casting. (even if it is negative).
Second is correct, because I think double can hold the biggest value and no cast is required to put any primitive into double
(only cannot put boolean value)
 
Punitha krishna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks It is clear now. I guess I didn't read the question properly.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably already know this but:
float f = -412; //Would Pass (int fits in float)
float f = -412.0; //Would fail (double doesn't fit in float)
float f = -412.0f; //Would pass (explicitly stating float)
float f = (float)-412.0; //Would pass (Cast)
- David

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java allows conversion between integer values( byte, short, int, long) and floating point values( float, double).
Both of questions are examples of "automatic" widening conversion, when value of "narrower" type is converted into "wider" type.
Vice versa explicit casting needed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic