• 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

Marcus Green mock exam 3 Q1

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following are legal statements?
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;
answer is 1,2,4
how come not only 4?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) float f=1/3;
Is right because the type of 1/3 is int which can be converted to float without losing precision.
2) int i=1/3;
Strange it may seem, this is right too. The type of expression 1/3 happens to be int here and it gets converted to int i. Though the precision is lost, this is a valid expression.

3) float f=1.01;
You know why this is wrong. Integer literals with a decimal point in them are considered as double, so you need an explicit cast.
4) double d=999d;
Perfectly legal.
So the answers are 1,2,4.

Since there is nothing wrong with this question( and Marcus is ) I am moving this to SCJP forum

Ajith
[This message has been edited by Ajith Kallambella (edited August 29, 2000).]
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic