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

A

adil.fulara

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

Patricia Shanahan

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
 
C

Christian

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

Jeff Higgins

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)
 
S

SadRed

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

Greg R. Broderick

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 (e-mail address removed)

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

Luc The Perverse

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!
 
T

Tom Hawtin

Greg said:
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
 
J

jt

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

Alex Hunsley

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
 
P

Patricia Shanahan

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
 
A

adil.fulara

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

Christian

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top