Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

finalize() doubt

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I have this question from A Programmer's Guide to SCJP Certification by Khalid Mughal et al Q# 9.12

What scenario can be the result of compiling and executing the following code -


Select the 2 correct answers -
a - The program may print AB
b - The program may print BA
c - The program may print A
d - The program may print B
e - The program may print nothing

Their answer : c & e
My answer : a & e

Option e is definitely correct as the GC may not run at all.

However how the anonymous MyClass object is GCed before the concat() method is invoked is beyond me. Thus i think that option A is more plausible that option C

Also Option D is false since the anonymous String object " World" invokes the finalize() method of String class and not of the MyClass class. Correct me if I am wrong.

Thanks

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may print 'Hello' and it may print nothing. To settle the latter, System.gc(); suggests that the JVM run the garbage collector but doesn't guarantee it will, so finalize() may never execute. And, the code wouldn't print 'Hello World' because the statement on line 16 doesn't reassign the result to the instance variable str. If str were a StringBuilder or StringBuffer it would concatenate.

Hope that helps.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep in mind that after the concat() executes the value of str is still "Hello" and not "Hello World".

So a & e are correct.

If in concat()
str = this.str.concat(s)

then c & e would be correct.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

victor kamat wrote:Keep in mind that after the concat() executes the value of str is still "Hello" and not "Hello World".

So a & e are correct.

If in concat()
str = this.str.concat(s)

then c & e would be correct.


You got it right, like Ryan did, but I think you meant to say that c & e are correct now, and if concat() were changed then a & e would be correct.
 
Chaitanya Lele
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all

Dumb error on my part
Forgot that Strings are immutable...

Got to avoid those kind of errors in the SCJP on Monday
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic