• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about chapter 2 sample exam question 10 (CH2Q10, Mala Gupta)

 
Jimmy Valdez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm studying the book OCA Java SE 7 By. Mala Gupta. The problem is that this question has another solution. It seems a bit confusing, so are all the questions of the book?



Solution.

Answer: b
Explanation: Because the question mentioned swapping the functionality of the operator
> with =, the code on lines 4, 5, and 6 will actually evaluate to the following:

The result is shown in b.
Note that the expression myBool = false uses the assignment operator (=) and not
a comparison operator (==). This expression assigns boolean literal false to myBool;
it doesn’t compare false with myBool. Watch out for similar (trick) assignments in the
exam, which may seem to be comparing values.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimmy Valdez wrote:The problem is that this question has another solution. It seems a bit confusing, so are all the questions of the book?

I don't know what you mean by another solution. It gives the right answer and the explanation explains it. It is strange that the author makes you substitute the operators instead of just writing them that way in the first place.
 
Jimmy Valdez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:
Jimmy Valdez wrote:The problem is that this question has another solution. It seems a bit confusing, so are all the questions of the book?

I don't know what you mean by another solution. It gives the right answer and the explanation explains it. It is strange that the author makes you substitute the operators instead of just writing them that way in the first place.


For the correct answer is d. But the will is b, because change.



for



Because it changes the sysout?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I see the problem you have with it. The author doesn't want you to consider what the result would be without the operator substitution. Only the result after the substitution.
 
Jimmy Valdez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Guillermo Ishi wrote:Maybe I see the problem you have with it. The author doesn't want you to consider what the result would be without the operator substitution. Only the result after the substitution.


I understand. I understand. So here it should be equal ?



For me it is true.
But the answer is false, why?

For the expression (a++ > 10 || ++b < 30), because a++ > 10 evaluates to false,
both operands will evaluate.

false || true -> true

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, like you said, after the substitution (which is all that matters) it becomes:

So:
 
Jimmy Valdez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the explanation.

And here as it should be?.



For me it is true.
But the answer is false, why?

For the expression (a++ > 10 || ++b < 30), because a++ > 10 evaluates to false,
both operands will evaluate.


According to this rule.
false || true -> true
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimmy Valdez wrote:The problem is that this question has another solution. It seems a bit confusing, so are all the questions of the book?

I have to say I'm not a huge fan of this kind of questions, because
  • it could really confuse readers
  • such questions are definitely not on the actual exam
  • instead of asking what the output will be if operators are swapped, you could easily ask what the output of this snippet is
  • It tests exactly the same knowledge but way simpler! And just for the record: B is definitely the correct answer, output of the above snippet:
    10.0
    false
    false


    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10666
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jimmy Valdez wrote:For me it is true.
    But the answer is false, why?

    That code snippet as-is will definitely print true, no doubt about that! Who says the answer should be false

    The 1st operand (a++>10) evaluates to false and because it's the short-circuit OR operator, the 2nd operand will be evaluated as well. The 2nd operand (++b<30) definitely evaluates to true (21 is less than 30) and thus true is printed.

    Hope it helps!
    Kind regards,
    Roel
     
    Jimmy Valdez
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for everything. I will continue studying. greetings
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic