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
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Hi, this mock-exam question confused me:

Select the valid primitive assignments of the following.

int i = 10;
char c = i;
float f;
long l = 100L;
f = l;
short s = 20;
char c = s;
byte b = 20;
char c = b;
short s1 = 10;
short s2 = 20;
short result = s1*s2;

Hanna Barenthin
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Hi sorry, posted before I was done ;-) The question that got me confused was:

Select the valid primitive assigements:

a)
int i = 10;
char c = i;

b)
float f;
long l = 100L;
f=l;

c)
short s = 20;
char c = s;

d)
byte b = 20;
char c = b;

The correct answer is b). I do not understand why since float has 32 bits and float 64 bits, so how can this be done without a cast? And if it has to do with that 100 is "enough small" to fit in a float reference, why are not the other alternatives also right then since char can hold anything up to 65535?

Happy if someone understands...Thanks
Hanna

author
Posts: 23928
142
• Number of slices to send:
Optional 'thank-you' note:
Hanna,

Thanks,
Henry

Henry Wong
author
Posts: 23928
142
• Number of slices to send:
Optional 'thank-you' note:

I do not understand why since float has 32 bits and float 64 bits, so how can this be done without a cast?

Hanna,

Keep in mind that a floating point number has range and precision limitations. Believe it or not, a 32 bit float has a larger range than a 64 bit long, hence, the cast is implicit.

Henry

Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
a) invalid because you cannot put int in char loss of precision

b) valid while long can fit into float

c) invalid , char can be assign only int value not byte or short or long
and up to 65535
char c=65536 // error

d) same c

any operation between two numbers ( byte , short , char , int ) the result will be int so
short s1 = 10;
short s2 = 20;
short result = s1*s2 // error possible loss of precision

short result=(short) s1*s2 // fine

Hanna Barenthin
Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
Thanks! This helped a lot! Sorry for not stating the source, it was the mock exam:

http://www.javablackbelt.com/QuestDefListing.wwa
The exam by Siva Valiveru

 All of life is a contant education - Eleanor Roosevelt. Tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising