This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SCJP Brainteaser (7)  RSS feed

 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is next one.




Another question is:

Here you need to declare i as so the loop goes continue.



Output: ???

Explanations: ???

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

to the first question a hint:
int i=Integer.MIN_VALUE;

System.out.println(Integer.toBinaryString(i));
i=i<<1;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toBinaryString(-1<<31));
System.out.println(Integer.toBinaryString(-1<<32));

But I cannot explain the result.

Yours,
Bu.
[ November 10, 2006: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bu's explanation is good enough to understand, why negative numbers can not be converted into 0? Infact no negative numbers can be converted into any positve numbers through shift(>>,<<,<,>,>>>)operators.

Sharma jee,Can you explain the second question once again.
[ November 10, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:

Sharma ji,Can you explain the second question once again.



Well my question is simple enough.

We need to declare i such that.




This loops goes continue.....

I haven't said any time, i should be int or any number

Now that's it....

All ranchers are very smart enough..
[ November 10, 2006: Message edited by: Sharma Ji ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare i as a String type.
Suppose we declare i as a,
String i = "SCJP Brainteaser"
then the LHS of

will result in "SCJP Brainteaser"
and RHS "SCJP Brainteaser0" and the expression will result true and loooooop will not end..
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers, back to the first question again.

Sanjeev Kumar Singh posted November 10, 2006 03:53 AM

Bu's explanation is good enough to understand, why negative numbers can not be converted into 0?


Sorry, Sanjeev, but the first lines I posted do exatly this. The smallest possible Integer shifted once to the left produces zero.


Bu.
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!