• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA Java SE 8 Programmer I Study Guide - Chapter 2 - Review Question Confusion

 
Maxwell Xu
Greenhorn
Posts: 12
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Today I would like to bring up a question I am confused about in the OCA Java SE 8 Programmer I Study Guide by Jeanne Boyarsky and Scott Selikoff. In the Chapter 2 Review Questions, I found #1 a little confusing.

Question:
1. Which of the following Java operators can be used with boolean variables? (Choose all that apply)
A. ==
B. +
C. --
D. !
E. %
F. <=

I chose A, D, and F. However, the answer was supposed to be A and D. But <= is very alike == so why is answer F wrong?

Thanks,

Maxwell

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x <= y is like a shorthand for x < y && x == y.

Let's think about this in English. The number 4 is clearly less than (or equal to) the number 5. The number 5 is also (less than or) equal to  the number 5. So <= works.

What would it mean for false to be less than than true? It wouldn't. So Java doesn't allow comparing booleans with <= because they can;t be substituted for x and y above.
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x <= y is like a shorthand for x < y && x == y.


I think It should be x <= y is like a shorthand for x < y || x == y. Because it is  x less than (or equal to) y but not x less than (and equal to) y .
Please correct me I am wrong.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana,
Yeah, I meant that . Thanks for pointing it out.

What I posted would always evaluate to false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic