• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

QUICK assignments exam q ..

 
sarim raza
Ranch Hand
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 57.
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;
//I LIKE B ONLY, BUT ANSWER LIKES A AS WELL, HOW COME ?
 
Aru Ven
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarim,
"B" is the only right anwer.
The thumb rule is -
Remember any other primitive other than a char, when assigned to char requires "EXPLICIT CASTING".
int i = 10;
char c = (char) i;
The above compiles fine.
HTH,
Aruna
 
sachin patel
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes aruna is right it needs a explicit cast to convert from int to char.
------------------
Sachin,
**************************************************
Learn from others mistakes. Life is too short to make all yourself.
**************************************************
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic