• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

OCA review question

 
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is from OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide by Jeanne Boyarsky and Scott Selikoff.

Question 13 from Chapter 2 is as follows:

What is the output of the following code snippet?


Optional Answers are:
A: Success
B: Failure
C: The code will not compile because of line 4.
D: The code will not compile because of line 5.

My initial answer was D, because I thought the code would not compile since the = is an assignment, not a comparison. However, after reading the answer the book provides I found out this does not give a compiler error.

Answer from the book:
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.

Based on the above I have 2 questions:
1) What do they mean with: The variable b is assigned true on line 3? Is this a typo since boolean b is declared and initialized on line 4.
2) How does an assignment operator works in an if-then statement?

Cheers
 
Sheriff
Posts: 22841
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bart Boersma wrote:Based on the above I have 2 questions:
1) What do they mean with: The variable b is assigned true on line 3? Is this a typo since boolean b is declared and initialized on line 4.
2) How does an assignment operator works in an if-then statement?


1) I think it should say line 5, because that's the only assignment that sets b to true.
2) The result of any assignment is the value of the variable after the assignment. This is not just for boolean variables but for all. For instance, the following is valid and assigns 100 to both variables:
The reasoning behind this is that first 100 is assigned to x2. That result of that assignment is 100, which is then assigned to x1.


That said, you shouldn't use assignments in conditions in most cases, because it's confusing and error prone. The only two cases I can think of that I use myself are the following:

Both loop conditions will perform the assignment first, then check the result against the loop terminating condition. For reference, here's how that would work out otherwise, with duplicated reading code:
 
Saloon Keeper
Posts: 5606
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bart Boersma wrote:
Based on the above I have 2 questions:
1) What do they mean with: The variable b is assigned true on line 3? Is this a typo since boolean b is declared and initialized on line 4.
2) How does an assignment operator works in an if-then statement?

Cheers


hi Bart,

in line 3 (according to the book) the value 'true' is indeed assigned to b ("if (b = true) ...."). That is your line 5. In your line 4 b is also assigned a value (false in this case). But the trick of this assignment is that you shoud see that (b = true) is used here, instead of what you would expect: (b == true).

2) an expression (like b = 3 or b = true) returns a value. See the java tutorial:  tutorial

In    "if (b) ... "   b must be a boolean. The assignment "b = true" is itself a boolean with the value of "true'.
That is why this construction works. But try: "if (x = 3) ...." (with x being an integer). That won't compile.
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your answers.

There is one thing left which I cannot quite grasp yet:

So I am correct in stating that whatever the value of b is before the if statement does not matter? Assigning the value true to b in the if statement will always result in printing "Success" and assigning the value false to b in the if statement will always result in printing "Failure"? If so, could you explain why assigning b the value true will run the body of the if statement and assigning b the value false will run the body of the else statement?

Prints: "Success"


Prints: "Success"



Prints: "Failure"


Prints: "Failure"

 
Piet Souris
Saloon Keeper
Posts: 5606
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bart Boersma wrote:So I am correct in stating that whatever the value of b is before the if statement does not matter?


Correct.

The point is that in "if (b = true) ..." first 'true' is assigned to b, and then the statement is interpreted as: if (true) ....
That is why the body of the if is always executed.

If we had 'if (b = false) ...', then, likewise, the body of the else is always executed.

That is why it is always risky to use a construct like: "if (b == true) ...", because if you make a typo: "if (b = true) ..." then the compiler does not warn you for this mistake. So always use "if (b) ...".

As said, this is one of the famous 'trick' questions that you can expect in the OCA exam. If you fail to see that an assignment is being used here, you will end up giving an incorrect answer.
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great answer.

Thanks Piet!
 
Piet Souris
Saloon Keeper
Posts: 5606
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic