Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

What does this Snippet Do ? Interview Question!!!!!!!!!

Thread view: 
adil.fulara@gmail.com - 14 Apr 2007 23:38 GMT
Hi,

I got the following question in a interview test that i gave and i was
unable to answer it.

========================================================================
What does the following program print?

public class Initializer
{
   private static boolean initialized = false;
   static
   {
       Thread t = new Thread( new Runnable()
       {
           public void run()
           {
               initialized = true;
           }
       } );

       t.start();
       try
       {
           t.join();
       }
       catch (InterruptedException e)
       {
           throw new AssertionError(e);
       }
   }

   public static void main(String[] args)
   {
       System.out.println(initialized);
   }
}

(provide the exact output for this question, there are no choices)
=================================================================

At the time of the test, i just couldnt figure if the code would be
executed or not since in Main, there was no instance of the object
created.

Any help on the question  ?

Also where could i find such tricky snippets of code so that icould
test myself and in the process improve my java knowledge.

Thank You.
Patricia Shanahan - 14 Apr 2007 23:59 GMT
> Hi,
>
[quoted text clipped - 47 lines]
>
> Thank You.

I have not run it, but I would expect it to print "true".

A static initializer is run during class initialization, which has to
complete before the main method can be run.

Patricia
Christian - 15 Apr 2007 00:08 GMT
adil.fulara@gmail.com schrieb:
> Hi,
>
[quoted text clipped - 47 lines]
>
> Thank You.

What happens is:

1.  initialized is initialized false by the thread loading the class

2. another Thread is created an run that will set initialized to true

3. with join() the thread loading the Initializer class will wait for
this Thread to finish his job..

4. the main method is executed and it prints out  "true".

afaik the dying of the thread and waiting for it with join guarantees a
proper "happens before" relation of the event of changing the
initialized variable to true and printing it.
Jeff Higgins - 15 Apr 2007 00:29 GMT
> Hi,
> What does the following program print?
[quoted text clipped - 30 lines]
>
> (provide the exact output for this question, there are no choices)
SadRed - 15 Apr 2007 00:45 GMT
On Apr 15, 7:38 am, "adil.ful...@gmail.com" <adil.ful...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 48 lines]
>
> Thank You.

The program hangs because join() blocks indefinitely.
Got no output.
Patricia Shanahan - 15 Apr 2007 05:36 GMT
...
> The program hangs because join() blocks indefinitely.
> Got no output.

Well, I got that one wrong. I can see why it hangs now.

Patricia
Greg R. Broderick - 15 Apr 2007 01:05 GMT
> Also where could i find such tricky snippets of code so that icould
> test myself and in the process improve my java knowledge.

Get a copy of the book "Java Puzzlers" by Joshua Block and Neil Gafter, it is
full of stuff like this.

Cheers!

P.S.  And remember, don't code like my brother! :-)

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb+usenet200704@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

Tom Hawtin - 15 Apr 2007 03:47 GMT
>> Also where could i find such tricky snippets of code so that icould
>> test myself and in the process improve my java knowledge.
>
> Get a copy of the book "Java Puzzlers" by Joshua Block and Neil Gafter, it is
                                                             ^^^^Neal
> full of stuff like this.

Yes, in fact this is puzzle 85 (only this has a different class name and
the layout screwed up).

I understand the problem this puzzle illustrates does happen in
practice, particularly with badly written code.

I guess what they were trying to do was to get you to think. Go through
your thought processes. Can you think through what could possibly go
wrong and why.

Most Java programmers are probably not going to get most of the puzzles.
The best Java programmers have already read the book.

Tom Hawtin
Luc The Perverse - 15 Apr 2007 01:50 GMT
> Hi,
>
> I got the following question in a interview test that i gave and i was
> unable to answer it.

I'm just curious what kind of an interviewer let you keep the test you were
given.

I've taken tests with employers before and they intended on screening future
applications and didn't want me taking the test!

--
LTP

:)
jt - 15 Apr 2007 04:02 GMT
> (provide the exact output for this question, there are no choices)
> =================================================================

I ran this code in the Eclipse IDE and got no output at all.  I would
suggest that you put this code into a java source file, compile it and
see what it does.

Signature

There are 10 types of people in this world.  Those who understand binary
and those who don't.

Alex Hunsley - 15 Apr 2007 04:26 GMT
> Hi,
>
> I got the following question in a interview test that i gave and i was
> unable to answer it.
[snip]

You are giving out interview code tests to people and you don't know the
answer yourself?
Please tell me where you work. Then I can avoid this place.

If instead you mean that you were given this code in an interview as an
interviewee: have you tried running this code?
lex
adil.fulara@gmail.com - 15 Apr 2007 05:38 GMT
> adil.ful...@gmail.com wrote:
> > Hi,
[quoted text clipped - 11 lines]
> interviewee: have you tried running this code?
> lex

Hi. I was an interviewee.... I did try running the code and it
produced no result.
More imporatant to me was to understand the code and how it functions
and not the output.
I guess every1 knows how to get output of a code......

Just wanted to know how that static piece of code ran without an
object being created for that class.
But i guess static blocks are executed for every class.

I need to find out more on that.

A.
Patricia Shanahan - 15 Apr 2007 06:16 GMT
...
> Just wanted to know how that static piece of code ran without an
> object being created for that class.
> But i guess static blocks are executed for every class.

JLS 12.4 Initialization of Classes and Interfaces,
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4

Patricia
adil.fulara@gmail.com - 15 Apr 2007 08:53 GMT
> adil.ful...@gmail.com wrote:
>
[quoted text clipped - 7 lines]
>
> Patricia

I thank each one of you for providing your inputs.

Hope to find some more puzzles like this on the net.

A.
Christian - 15 Apr 2007 11:58 GMT
adil.fulara@gmail.com schrieb:
>> adil.ful...@gmail.com wrote:
>>
[quoted text clipped - 12 lines]
>
> A.

I thank you Patricia..

that explains why I got the wrong idea on this

If I understood the text right

the created thread will lock because an initialization is in progress..
and it has to synchronized on the class too because the class is not
finished with initialization yet. With the join then its a deadlock.

Christian


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.