Sim-Ex™ Practice Exams for Java SE 8 Programmer II: Sample Questions

Language Fundamentals

Home     Previous     Up     Next

Q3. 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 200

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     Previous     Up     Next


Disclaimer: Simulationexams.com is not affiliated with any certification vendor, and Sim-Ex™ Practice Exams are written independently by SimulationExams.com and not affiliated or authorized by respective certification providers. Sim-Ex™ is a trade mark of SimulationExams.com or entity representing Simulationexams.com.SCJP® is a trademark of Oracle™.