• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with marcus questions

 
Gauri Tilloo
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here

OPTIONS:
1)0
2)1
3)2
4)3
Answer is 0. Can any one explain how?


2)What will happen when you attempt to compile and run the following code?

options:
1)compile time error, the call to col has the wrong type
2)compilation and output of 1010
3)Compile time error Object cannot be cast to Integer
4)Compilation and output of 2020

Answer is 4. Pl. explain how?

( tags added, fixed formatting and missing brace)

[ July 08, 2005: Message edited by: Barry Gaunt ]
[ July 08, 2005: Message edited by: Barry Gaunt ]
 
Rocky Jaiswal
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think i know the answer to the second one. It is because the variable is static the variable belongs to the class and not to the object so the variable value is what you assign to it last, which is 20.

Am i right javaranch?

Thanks,
Rocky
 
Kedar Dravid
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 0, because x still refers to an Integer object with value 10,
and the object reference pointed to by y still refers to an Integer object with value 99. Setting z=null does not make any object eligible for GC.
[ July 08, 2005: Message edited by: Kedar Dravid ]
 
Fes D Gaur
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as an object has a live thread referencing it, the object will not become eligible for garbage collection. In case of question 1 the reference x is passed to a method. Objects are passed by reference so a copy of the reference was passed. That copy in the method became null while the original reference still was attached to the object. Same case applies to reference y.

Fess D Gaur
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try not to make questions available without including an explanation of the answer. In the case of that first question I have included the explanation ...

"A reference passed into a method is passed as if it were a copy of a pointer pointer rather than the actual object. Thus if that reference is assigned to a null it makes no difference to any other copy of that pointer. Thus the code within the method findOut makes no difference to any other references. Although reference z is assigned to null reference y still points to the object so no objects are eligible for garbage collection."

Was this explanation availible/visible/correct/meaningful. Your feedback is greatly appreciated.

(This question is one that is publicly visible from the database at
http://www.jchq.net/phezam and is from the JDK1.4 subject set)
Marcus
[ July 08, 2005: Message edited by: Marcus Green ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read both the question and explanation given by Marcus Green. I found it satisfactory. I appreciate if you can avoid words like pointer(even though it makes sense).


Note: Thanks Marcus for offering such an wonder mock site freely. You work is phenomenal. Continue your good work. God Bless U.

 
latha yeduguri
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the following exception when I try to compile the above Code

Exception in thread "main" java.lang.NoClassDefFoundError: Csturt (wrong name: C
Sturt)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by latha yeduguri:
I am getting the following exception when I try to compile the above Code

Exception in thread "main" java.lang.NoClassDefFoundError: Csturt (wrong name: C
Sturt)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)



Did you read what the error was and check your typing?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems that your class file name should be CSturt.java instead of Csturt.java.

Class/File names are case sensitive.

Nick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic