File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes R & H Bonus1 question#36 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "R & H Bonus1 question#36" Watch "R & H Bonus1 question#36" New topic
Author

R & H Bonus1 question#36

Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
You are given the class file for a class called Secret. However, you do not have the source code or any information about the internals of the Secret class. You do know that the class has a protected int variable called i. What does the following application print out?
1. class Mine extends Secret {
2. public static void main(String[] args) {
3. Mine m1 = new Mine(); m1.i = 5;
4. Mine m2 = new Mine(); m1.i = 5;
5. if (m1.equals(m2))
6. System.out.println("YES");
7. else
8. System.out.println("NO");
9. }
10. }
A. Yes
B. No
C. Impossible to know
Correct answer is C. The result of the call to equals() on line 5 depends on how the method was written in the Secret superclass.
Can someone please explain?
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Parimala.
First off, I assume in line #4 you meant to say Mine m2 = new Mine(); m2.i = 5;.
The point here is, whoever programmed Secret.java could have made the requirements for equals() just about anything: without access to that code, you just don't know:
For instance, this version of Secret.java returns "NO" in Mine.java:

And this version of Secret.java returns "YES":

Therefore, the answer is indeterminate, and "C" is correct.
Art
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: R & H Bonus1 question#36