Please help SCJP 1.5
Source: SCJP 1.5 tips and Tricks Blog
http://technoheads.blogspot.com
Given the following code:
import java.util.Date;
public class Example {
public static void main(String args[]) {
Date d1 = new Date (99, 11, 31);
Date d2 = new Date (99, 11, 31);
method(d1, d2);
System.out.println("d1 is " + d1 + "\nd2 is " + d2);
}
public static void method(Date d1, Date d2) {
d2.setYear (100);
d1 = d2;
}
}
Which one or more of the following correctly describe the behavior when this program is compiled and run?
(1) compilation is successful and the output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Fri December 31 00:00:00 GMT 1999
(2) compilation is successful and the output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Sun December 31 00:00:00 GMT 2000
(3) compilation is successful and the output is:
d1 is Sun December 31 00:00:00 GMT 2000 d2 is Sun December 31 00:00:00 GMT 2000
(4) the assignment 'd1 = d2' is rejected by the compiler because the Date class cannot overload the operator '='
(5) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler because the Date class cannot overload the operator '+'
Answer : 2
Explanation :
1) is false because we know that the data in d2 was changed. 3) is false because we know that the data in d1 was not changed. The names d1 and d2 are used in both main and method to be confusing. They are different and stored on the stack in different place. All the interesting stuff that happen in the Example class is in method. main simply initializes some data and prints the results.
Can anyone help me regarding it. I am not abe to get it. I think the output should be 3rd one.
But the blog tells it is 2. Please explain
Neha