Here are the answers to yesterday’s practice questions. No cheating! If you’re still having trouble with something, or I screwed something up, email me.
1. b, c , d
Two concepts to keep in mind here: First, the two Thread objects created in main are created with different Test objects, thus the synchronized keyword doesn’t affect the output at all. If both Thread objects had been passed the same Test object, then the statements in the run method would have to execute as one (atomically). Since they are different objects, though, the statements can be interrupted.
Second, there are only two Thread objects created (besides the main thread), so there can only be two distinct numbers in the output.
2. e.
The sayHi( ) method is called from main, which is a static method. An instance method cannot be called from a static context. At first glance, this problem seems like it’s testing you on command line arguments, possibly exception handling. This is a clear example of how the SCJP will try to get you thinking way, while the right answer actually lies in a different direction.
3. d
On line 1, when t1 is set to null, it is still being referenced by t3.t1, so it is not eligible for garbage collection. On line 2, when t2 is set to null, two objects are lost at the same time – the object referenced by t2 and the object referenced by t2.t1. Thus, never in this program is exactly one object eligible for garbage collection.
This is a pretty straightforward example of the type of garbage collection problems you will encounter on the SCJP. Using the scrap paper that is provided will make this type of problem much easier. Trying to keep track of all the objects in your head is a recipe for disaster.
4. e
Again, two concepts to keep in mind. One: the compiler will always insert a call to super( ) in a constructor if there is not already a call to super( ) or this( ). The compiler will never insert a call to any constructor except the default, no-argument constructor. Two: The compiler will only insert a default constructor if no constructor is defined. Since PreTest defines a constructor, the compiler will not insert a default constructor in PreTest. Thus, when it attempts to insert a call to the default super-constructor in Test, compilation will fail. To fix this compiler error, either insert a call to super(String) as the first line of the Test constructor or insert a no-argument constructor into PreTest.
5. e
Make sure you look at the modifiers! This code looks like it should compile and run without a problem, until you realize that the two code fragments are in different packages. Since the PreTest constructor does not declare an access modifier, it can only be accessed within the same package.
I can’t make this point enough: Always always always look at the modifiers in this type of problem. It will save you time in the long run – this problem should take you less than a minute if you look at the modifiers first.
Well that’s it for now. Next week we’ll move onto the second pattern, but until then, try out my strategies for the “Results” Pattern on some other practice questions and see how it works for you.
Have something to say about this post? Let me have it!