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

Discussion in 'Java' started by adil.fulara@gmail.com, Apr 14, 2007.

  1. Guest

    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.
     
    , Apr 14, 2007
    #1
    1. Advertising

  2. wrote:
    > 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.
    >


    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
     
    Patricia Shanahan, Apr 14, 2007
    #2
    1. Advertising

  3. Christian Guest

    schrieb:
    > 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.
    >


    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.
     
    Christian, Apr 15, 2007
    #3
  4. Jeff Higgins Guest

    wrote:
    > Hi,
    > 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(); //t.join(3000);
    > }
    > 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)
     
    Jeff Higgins, Apr 15, 2007
    #4
  5. SadRed Guest

    On Apr 15, 7:38 am, "" <>
    wrote:
    > 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.


    The program hangs because join() blocks indefinitely.
    Got no output.
     
    SadRed, Apr 15, 2007
    #5
  6. "" <> wrote in
    news::

    > 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! :)

    --
    ---------------------------------------------------------------------
    Greg R. Broderick

    A. Top posters.
    Q. What is the most annoying thing on Usenet?
    ---------------------------------------------------------------------
     
    Greg R. Broderick, Apr 15, 2007
    #6
  7. <> wrote in message
    news:...
    > 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

    :)
     
    Luc The Perverse, Apr 15, 2007
    #7
  8. Tom Hawtin Guest

    Greg R. Broderick wrote:
    > "" <> wrote in
    > news::
    >
    >> 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
     
    Tom Hawtin, Apr 15, 2007
    #8
  9. jt Guest

    wrote:

    > (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.


    --
    There are 10 types of people in this world. Those who understand binary
    and those who don't.
     
    jt, Apr 15, 2007
    #9
  10. Alex Hunsley Guest

    wrote:
    > 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
     
    Alex Hunsley, Apr 15, 2007
    #10
  11. SadRed wrote:
    ....
    > The program hangs because join() blocks indefinitely.
    > Got no output.
    >


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

    Patricia
     
    Patricia Shanahan, Apr 15, 2007
    #11
  12. Guest

    On Apr 14, 8:26 pm, Alex Hunsley <> wrote:
    > wrote:
    > > 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


    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.
     
    , Apr 15, 2007
    #12
  13. Patricia Shanahan, Apr 15, 2007
    #13
  14. Guest

    On Apr 14, 10:16 pm, Patricia Shanahan <> wrote:
    > wrote:
    >
    > ...
    >
    > > 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#...
    >
    > Patricia


    I thank each one of you for providing your inputs.

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

    A.
     
    , Apr 15, 2007
    #14
  15. Christian Guest

    schrieb:
    > On Apr 14, 10:16 pm, Patricia Shanahan <> wrote:
    >> wrote:
    >>
    >> ...
    >>
    >>> 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#...
    >>
    >> Patricia

    >
    > I thank each one of you for providing your inputs.
    >
    > Hope to find some more puzzles like this on the net.
    >
    > 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
     
    Christian, Apr 15, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    18
    Views:
    501
    Richard Bos
    Feb 22, 2007
  2. Ramon F Herrera

    What does this snippet do?

    Ramon F Herrera, Dec 4, 2007, in forum: Java
    Replies:
    2
    Views:
    422
    Ramon F Herrera
    Dec 4, 2007
  3. reema
    Replies:
    0
    Views:
    314
    reema
    Aug 26, 2008
  4. Tom Cole

    Does this code snippet look okay?

    Tom Cole, Apr 25, 2008, in forum: Javascript
    Replies:
    1
    Views:
    134
    Tom de Neef
    Apr 25, 2008
  5. Wake up Brazil
    Replies:
    10
    Views:
    148
    Öö Tiib
    Apr 16, 2014
Loading...

Share This Page