Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B master exam question

 
Sneha Kapoor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,




how the output is good

following are options 1)Good 2)Bad 3)Ugly 4)Compilation fails

Source K&B : Master Exam
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a piece of code that might help you understand what is going on



Relate the output with the positive, negative or zero return value of the compare() method.
 
Chad Cook
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Characters can be implicitly cast to integers. 'B' has a value of 66, 'G' of 77 and 'U' of 85. Thus, when you compare, for example, Good and Bad, it is actually returning 77 - 66, which returns a positive number.
 
Sneha Kapoor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks .I got it
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chad Cook wrote:Characters can be implicitly cast to integers. 'B' has a value of 66, 'G' of 77 and 'U' of 85. Thus, when you compare, for example, Good and Bad, it is actually returning 77 - 66, which returns a positive number.


Just to nit pick the comparison is done on the second character, not the first one, and the second character of the second word is used first. Sorry
 
Chad Cook
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch, and please nit pick.

Attention to detail is important when studying for the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic