Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question

 
xin sun
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
QUESTION NO: 104
Given that b and c refer to instances of wrapper classes, which two statements are
true? (Choose two)
A. b.equals(b) returns true.
B. b.equals(c) returns the same result as b == c.
C. b.eqials(c) can return false even if c.equals(b) returns true.
D. b.equals(c) throws an exception if b and c are different wrapper types.
E. b.equals(c) returns false if the type of wrapper objects being compared are different.

Answer: B, C

why? thanks!


(No need to shout, and please use a meaningful topic)
[ February 20, 2005: Message edited by: Barry Gaunt ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you experiment with some code, you'll quickly see that the answers provided are not correct.

For example, A is true and B is false. (Well, actually B is true if you're using 1.5 to autobox a value within the range of a byte, but I don't think that's what they're asking.)
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xin Sun,

Statement A and E are correct based on jdk 1.4.

B is incorrect because if b and c are referencing to different objects of the same type and they contain the same value, b.equals(c) returns true.


C is incorrect because there is no such method eqials in any wrapper class.

D is incorrect because it returns false (not throwing an exception) if b and c are different wrapper types (as stated in statement E).


Joyce
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is incorrect because there is no such method eqials in any wrapper class.

Even with equals() spelled correctly, c is false because the contract for equals requires:
for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.


[ February 19, 2005: Message edited by: Mike Gershman ]
[ February 20, 2005: Message edited by: Mike Gershman ]
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. When spotted "eqials", I thought that's a trick question so I didn't bother to read the rest of the sentence. Thanks for pointing that out, Mike.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic