• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equality

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 9834-9835). Enthuware. Kindle Edition.
I am not sure why this returns true:

considering that trim() here doesn't do anything? Are the extra spaces ignored?
thanks
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That line of code should return false.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought so, but apparently not according to Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 9021-9027). Enthuware. Kindle Edition. ):
Which of these expressions will return true? Select 4 options
A.
B.
C.
D.
E.

Correct answers:A,B,C,E.
And here is a screenshot of the question too:

equals.jpg
[Thumbnail for equals.jpg]
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:I would have thought so, but apparently ...


It takes only a few seconds to write a test program to confirm. In fact, you probably could have done it faster than posting this topic...


... and BTW, with the answer, since the "hello" and "world" are on different lines, how do you know that there is only one space?

Henry
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:. . . C. . . .
Now, that is interesting. You will have to go to the Java® Language Specification (=JLS). Now, the JLS can be difficult to read at the best of times, but I am going to make you read the grammar. That will be worse. You want the line saying
ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
Ignore the type arguments and you have
ExpressionName = "hello"
. = . (surprise, surprise ‍)
Identifier = concat
( = (
ArgumentList = "    world"
) = )

Now, it is evident that you have a kind of expression on the left of that line of grammar; you are also saying that it is a kind of expression. So we can fiddle that into
KindOfExpression ::= KindOfExpression . identifier ( argumentList )
So  that grammar shows that the . before identifier associates to the left. This is very important. If the . operator associated to the right, you would execute it like this:-But . associates to the left, so it is executed like this:-You can show that for yourself very simply:How on earth can you get line C to return true?
 
Piet Souris
Rancher
Posts: 1521
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it myself yesterday, because like Henry I thought that it should be false. And so it was. Maybe it was a typo, with some parentheses misplaced? This would return true:

But that seems unlikely.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic