• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with marcus questions

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic