This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Q25 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Q25" Watch "Marcus Green Q25" New topic
Author

Marcus Green Q25

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
What will happen if you attempt to compile and run the following code?

Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);

1) 19 followed by 20
2) 19 followed by 11
3) Compile time error
4) 10 followed by 1

The exam says the answer is 3, I answered 2.

I ran the code and I found out answer 2 was the output. Anyone care to comment?


Be Afraid...Be very Afraid...
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You cannot compile that code with JDK 1.4. Because it will try to use an overloaded operator that sums two Integers, and that operator does not exists, nor the operator that sums a primitive interger with an Integer.

That's why. May you're compiling the code with JDK 1.5 and autoboxing is happening automatically.
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Yeah youre right, im using jdk 1.5. Thanks! I didnt know that.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
This is a very subtle change between the versions and it will affect many existing questions. I have spent a huge amount of time over the last few months working on JDK 1.5 mock exams and this includes compiling up code like this to see if effects like autoboxing change the result. In several cases the answer to the question changes.

(note these new questions are not available for consumption yet)
[ April 27, 2005: Message edited by: Marcus Green ]

SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Marcus Green Q25
 
Similar Threads
Wrapper Class
Wrapper Question
wrapper classes
Wrapper class
Marcus Green Exam 1 Question 25