• 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

Mock exam questions section 3

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slowly getting there.. please ask questions

1. Which three of the following statements are true?

1 finalize() is called only once for each object instance
2 The garbage collector frees memory in the heap
3 You can force the garbage collector to operate
4 An object only used by a blocked thread can be cleaned up
5 An object with a reference to it will never be cleaned up by the garbage
collector
6 finalize() may never run
7 A java application will not run out of memory because of the garbage
collector

2. What is the output of the following code?


1 Compile error
2 Run time exception thrown
3 memoryemptyclean
4 memoryclean
5 memory empty
6 The output can not be known for certain beyond that memory will be output
before empty, with clean possibly printed between them or after empty.


3 The correct syntax for requesting garbage collection is:

1 class garbage extends garbageCollector { public void collect(); }
2 system.
3 GarbageCollection();
4 system.deleteUnusedObjects();
5 system.GarbageCollection();
6 system.gc();
7 None of the above


4 Which line of code inserted //Here will free up a single object for garbage

collection?


1 a.i = null;
2 b.i = a.i;
3 b = null;
4 a = d.i;
5 d.i = b.i;
6 e = null;

5 At the line // Here, how many objects are eligible for garbage collection?



1 Compile error
2 Run time exception
3 0 objects available for collection
4 1 object available for collection
5 2 objects available for collection
6 3 objects available for collection

6 At the line // Here, how many objects are eligible for garbage collection?



1 Compile error
2 Run time exception
3 0 objects available for collection
4 1 object available for collection
5 2 objects available for collection
6 3 objects available for collection




1 Objective 3.1

1,2,6 are true

1 - True. finalize() is called once for each object instance
2- True. The garbage collector operates on the heap
3 - False. You can request the garbage collector be run, but it may not be.
4 - False. An object used by a blocked thread can not be cleaned up.
5 - False. An object with a reference to it may be referenced by an object
which itself is not referenced, and both can be cleaned up.
6 - True. Finalize may never run
7 - False. A Java application can still run out of memory


2 Objective 3.1

Answer - 1

The method finalize is void protected finalize, so it can not be compiled. The garbage collector does not guarantee when or if it will call finalize, so one can not be sure what the output is. It may or may not print "clean" at any point. (In my tests, it never does, but that is implementation dependent on the JVM)

3 Objective 3.2

Answer 7 - none of the above. System.gc() is the correct syntax (capitalized System)

4 Objective 3.2

Answer 5 d.i = b.i

At the line // Here the only object which has a single reference to it
(available to be erased) is the object formerly pointed to by a. This is where d.i points. So changing d.i causes nothing to refer to it, so it can be cleaned up.

5 Objective 3.3

Answer 3- 0 objects available for collection. At // Here, every object still has a reference to it.

6 Objective 3.3

Answer - 5 two objects available for collection. The object originally pointed to by a and the object originally pointed to by b are pointing to each other but no other reference exists to them.

Section 3: Garbage Collection

1 State the behavior that is guaranteed by the garbage collection system.

2 Write code that explicitly makes objects eligible for garbage collection.

3 Recognize the point in a piece of source code at which an object becomes
eligible for garbage collection.
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without having public class how one can run the programs ? The question listed above doesn't contain any public class. So it won't be possible to execute them let alone objects be gc'ed. Let me know if I am wrong
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think you need a public class to run the programs? I compiled and ran them all. Here is another one for you:



If you save this as boo.java and compile it it will compile- but you can't run boo because it does not have a main.

If you save this as foo.java and compile it it also will compile, and you CAN run it.

However, I think you are correct in that it is poor programming practice not to declare one of the classes public. The only rules I can find here in Sierra and Bates book is:

There can only be one public class per source file
The name of the file must match the name of the public class

Then they give an example of a class which is not declared public and show it compiles fine.
[ September 20, 2004: Message edited by: Tom Tolman ]
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's interesting. I executed the program and works fine. Here is my question.

When you don't declare a package JVM will create unnamed package for this program. Since the class has default accessor the class can be accessed only from the current package.

I am not sure how JVM invokes the class with default accessor. Please let me know how this happens Tom.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it- and I may be wrong- the JVM looks for the static method
static public void main (String []values)

as the initiation point of the program. The JVM, again a guess, is using the name of the class given as the name of the file to search for this static method.

You can declare multiple static public void main (String [] values) in many (non public) classes and it doesn't care- it will always invoke the one associated with the class which the file is compiled into.

Of course you can only have one public class. I can't imagine how you would invoke the static public void main on a non public class if another class were public- it wouldn't allow you to compile it with anything but the public class as the name of the file.
reply
    Bookmark Topic Watch Topic
  • New Topic