• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

From mIND q TEST some ques

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

MY answer were b,c but it says "a" also ? how can u invoke GC.
///CODE 2
void myMethod()
{ try
{
fragile();
}
catch( NullPointerException npex )
{
System.out.println( "NullPointerException thrown " );
}
catch( Exception ex )
{
System.out.println( "Exception thrown " );
}
finally
{
System.out.println( "Done with exceptions " );
}
System.out.println( "myMethod is done" );
}

the choice were
a) "NullPointerException thrown"
b) "Exception thrown"
c) "Done with exceptions"
d) "myMethod is done"
e) Nothing is printed
my ans were b,c but answer says "d" also how?
plz clarify

------------------
"Winners don't do different things
They do things differently"
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as for the first Q i am not sure ..
can anyone answer .. because we can invooke GC but its not sure that it will run at that time or not ?? does the answer mean this.
and for the second part..
if an exception occurs, and it is caught
1) then all the statements below the statement where Ex occured are skipped
2) it goes to the catch block and
3) runs finally and come out of the try block
4) and then it executes the rest of the code after the try block
but if it is not caught then
1) all the statements below the statement where Ex occured are skipped
2) it goes to the finally block and executes it and comes out of the function. ignoring the statements after teh try catch block
HTH
anil
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the Runtime.gc() method, you can indeed invoke the garbage collector. However, it is not guranteed that the garbage collector will actually run and do its job after such a call.
Here's the excerpt of the method from API documentation. Pay attention to the text in bold


public void gc()
Runs the garbage collector. Calling this method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made its best effort to recycle all discarded objects.
....


This description should not be intepreted at face value. Infact the very first statement "Runs the garbage collector" is not semantically accurate and is quite misleading. Since the garbage collection is dependant on the implementation, it is perfectly legal ( and acceptable ) if some implementations choses to turn ignore to explicit requests for running the GC. In such a case, the call to System.gc() or Runtime.gc() will have no effect.
Ajith
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
So, For q.1, is the statement 1 true or false ?. What I understood from the above messages is this : you can invoke GC, but it does not guarantee that it will run, right ?
Thanks in advance...
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you got it right. So the answer (a) in the original question is indeed true.
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
A tiny monkey bit me and I got tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic