• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green's Exam 2 #46

 
Ada Wang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 46)
Given the following variables which of the following lines will compile without error?
String s = "Hello";
long l = 99;
double d = 1.11;
int i = 1;
int j = 0;
1) j=i<<s;>
2) j=i<<j;>
3) j=i<<d;>
4) j=i<<l;>
I think 4) is incorrect. Before shifting, variable i is promoted to long since l is a long variable, but j is still int. The long result can't assign to a int variable j. Right? Please comment.
Ada
 
Sarada Bhasker
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of shift operators only the left-hand operand data type is taken into account.
The right-hand operand is converted to a number less than 32 if left-hand operand in int
or less than 64 if the left-hand operand in long and the resultant data type is always
same as left-hand operand.
so your point would have been correct if it is l >> j and not j >> l.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic