aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JvalTest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JvalTest" Watch "JvalTest" New topic
Author

JvalTest

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Select the valid primitive assignments of the following.
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;
E.short s1 = 10;
short s2 = 20;
short result = s1*s2;
This is a question from JVALTEST answer given ia a and b which i think is wrong. Shouldnt it be only b?
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
That's true,
I think only b is valid. for a you need an explicit cast to convert it to char.


Amir
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Thnx
Anybody else wants to confirm that.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Why not b and d ?
d is definitely correct.
nupur dhawan
Ranch Hand

Joined: Jun 26, 2002
Posts: 71
Originally posted by Alan Chong:
Why not b and d ?
d is definitely correct.

Please note :
5.1.3 Narrowing Primitive Conversions
The following 23 specific conversions on primitive types are called the narrowing primitive conversions:
byte to char
short to byte or char
char to byte or short


Nupur. <br />SCJP2.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
A char is not wider than a byte, since a byte can take negative values, while a char cannot.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Originally posted by Alan Chong:
Why not b and d ?
d is definitely correct.

Byte is smaller than char but can have negative values which are not there in char so u need a cast there.
Same theory why these dont work
byte to char
short to char/byte
char to byte/short
HTH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JvalTest