Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1Prints: XXYY  The program will not print XXYY. Please note that the question asks which could NOT be a result of attempting to compile and run the program. The finalize method of each instance can only run once; so X or Y can never be printed more than once. The instances referenced by x1 and y1 become eligible for garbage collection when method m returns; so both could be finalized at that time, but there is no guarantee that they will be. Even though System.gc is invoked in the main method, there is no guarantee that the garbage collector will run at that time. If the garbage collector does run before the program terminates, then the name of each object could be printed at most one time. The order in which the names are printed depends on the order in which the objects are finalized. If the garbage collector does not run, then nothing will be printed.  
2Indeterminate.  If method m2 causes a reference to be saved for each object that is passed in as an argument, then none of the objects are eligible for garbage collection when line 3 begins. Since we don't know what method m2 might be doing, we can not know if the objects are eligible for garbage collection.  
3 With each pass through the loop, q1 references a new object, and the old object becomes eligible for garbage collection. When the processing of line 2 begins, the last object referenced by q1 is not eligible for garbage collection.  
4None of the above  Please note that this question asks which object is NOT eligible for garbage collection after method m1 returns. The objects referenced by i1, i2 and i3 form a ring such that each object is referenced by another. Even so, nothing outside of method J.m1 references any of those objects. When method J.m1 returns, the ring becomes an island of isolated objects that are not reachable by any part of the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two objects form an island of isolated objects.  
5 Please note that this question asks which objects are NOT eligible for garbage collection when method m2 begins to execute? All three references, i1, i2 and i3, refer to object named C; so C is not eligible for garbage collection when method m2 begins to execute. The objects named A and B have references to each other, but no other objects refer to A and B. The objects A and B form an island of islolated objects and are eligible for garbage collection.  
6 Please note that this question asks which objects are NOT eligible for garbage collection after method m1 returns. After method m1 returns, the array a1 created on line 1 is not eligible for garbage collection. Method m1 sets all elements of the array to null; so the objects created on lines 2, 3 and 4 are eligible for garbage collection when method m1 returns.  
7None of the above  After method m1 returns, none of the objects are eligible for garbage collection. Method m1 sets the parameter variable a1 to null, but that does not change the reference a1 in the J.main method. Since array a1 continues to reference all three objects, none of the three are eligible for garbage collection.  
8The program compiles, runs and prints X.  The parameter i of method m1 is a copy of the local variable i of method J.main. Setting the parameter variable i of method m1 to null does not change the local variable i of method J.main.  

Copyright © 2002-2003, Dan Chisholm
All rights reserved.