Frequent Questions      

  Java™ Certification: SCJP™ 6- Sun Certified Java Programmer Exam 

 

Language Fundamentals

 

Home
           Up       Previous                 Next

3. 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?

a) 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

b) 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

c) 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

d) the assignment 'd1 = d2' is rejected by the compiler because the Date class cannot overload the operator '='.

e) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler because the Date class cannot overload the operator '+'.

  Correct Answer: B

 

Explanation:

a) is false because the data in d2 was changed in method. 

c) is false because the data in d1 was not changed

d) is false as code is perfectly legal, Object d1 is set to be the same as d2. This is a change of the actual reference, not in the data at d1.

e) is false, because Java is smart enough to call the method toString() for any object that is used in a String context. toString() is defined by the Object class and so it is available on all classes in Java. Most non-trivial classes override toString() to return more explicit information about themselves. 

Home
   Up           Previous                 Next

Copyright © 2000-2017 SimulationExams.com All rights reserved

Website design by Anandsoft.com

Disclaimer: All Simulation Exams practice tests, study guides and/or material are neither sponsored by, nor endorsed by, nor affiliated with Oracle® or any other company. All trademarks are trademarks of their respective owners and duly  acknowledged. OCA™, OCPJP™ are registered trade marks of Oracle® . The practice tests material is a copyright of SimulationExams.com and the same is not approved or endorsed by respective certifying bodies. Thank-you for your interest in Simulation Exams. Please see read me file before you download, install, and/or use any software from SimulationExams.com  For any information or questions regarding this Website, please e-mail webmaster at simulationexams.com