Certification tests Certification training Certification tests mcse ccna a+
Practice Tests CCNA MCSE A+ Network+           
  IT Certification Practice Exams   HOME  | Cisco  | CompTIA  | CheckPoint | CIW | Microsoft | Books | Bootcamp |  Buy Now!

Java Certification: SCJP - Sun Certified Java Programmer Exam 

Practice Questions: Exam SCJP 1.4 (beta)
Declaration and Access Control

 

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

Email: test@simulationexams.com