• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Confused. p.338, CHAPTER 2, Q13 (Sybex)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 13.
3:    int x1 = 50, x2 = 75;        // According to answers on p.338, variable b is assigned true on line 3

4:    boolean b = x1 >= x2;
5:    if (b = true) System.out.println("Success");
6:    else      System.out.println("Failure");

Answer explanation: A. The code compiles successfully, so options C and D are incorrect. The value of b
after line 4 is false. However, the if-then statement on line 5 contains an assignment,
not a comparison. The variable b is assigned true on line 3, and the assignment operator
returns true, so line 5 executes and displays Success, so the answer is option A.

My confusion: How is b assigned true on line 3? How does b relate to line 3?
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shavi,
Welcome to CodeRanch!  You managed to stumble on an error in the explanation that wasn't caught by any early readers in over two years!

That text should say that b is assigned true on line 3 rather than line 5.

I've added this to the errata and credited you.
 
Shavkat Juraev
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,

Thank you for giving me credit.
I updated my name here, is it possible to update it on errata page as well?
Sorry for inconvenience.
 
Jeanne Boyarsky
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shavkat Juraev wrote:Jeanne,

Thank you for giving me credit.
I updated my name here, is it possible to update it on errata page as well?
Sorry for inconvenience.


Certainly. Updated.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic