• 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

Errata - OCA/OCP JAVA SE 8 PROGRAMMER PRACTICE TEST (Ch02, p.30, Q-48)

 
Greenhorn
Posts: 24
Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code has taken from "OCA/OCP JAVA SE 8 PROGRAMMER PRACTICE TEST" book Chapter 2 - Working with Java Data Types
Query: There are two possible answers

"48. Which of the following is not a possible output of this code, assuming it runs to completion?

A. play-
B. play-play-
C. play-play-clean-
D. play-play-clean-clean-"

on page 437

"48. A. Remember that garbage collection is not guaranteed to run on demand. If it doesn’t run at all, Option B would be output. If it runs at the requested point, Option C would be output. If it runs right at the end of the main() method, Option D would be output. (which is wrong) Option A is the correct answer because play is definitely called twice. Note that you are unlikely to see all these scenarios if you run this code because we have not used enough memory for garbage collection to be worth running. However, you still need to be able to answer what could happen regardless of it being unlikely."

As per chapter 2 - Q.17 explanation on p.434 "The finalize() method may not be called, such as if your program crashes. However, it is guaranteed to be called no more than once."So, There are not two following possible outputs of this code
A. play-
D. play-play-clean-clean


 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amir,
Welcome to CodeRanch! The book is correct, but this question is tricky so I'm glad you asked!

Note that there are two Toy instances in this example. The finalize() method is called an an object instance (not on a class level.) This means, it is guaranteed to be called no more than once *per instance*. Since we have two instances in the code, clean may be printed zero, one or two times.

Make sense?
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm preparing to OCA and this question is unclear for me. It is about answer C, when it is possible to happen. Even if garbage collector run on demand after call System.gc(), then it may happen before second object is created. But the first object is still referenced by car variable so it is not suitable for garbage collection so finalize() will not be called. Only right after main method both variables are out of scope and would be both collected by GC.

Am I missing something?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! You are right. There should be a car= null; in there!

I think C would be right without it (because GC doesn't have to get all the garbage, but not sure. And that's definitely not something you need to know for the exam. The null assignment makes the question what we were trying to ask.
 
Pawel Pusz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK,

thanks for confirmation!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic