Tuesday, February 27, 2007

JAVA PUZZLES ... session @ sun tech days 07

JAVA PUZZLES by Raghavan N. Srinivas

This was undoubtedly the best session of the conference
Over all presentation goal was .. learning some of the quirks of programming in general and Java Programming language in particular

try getting the answer to the following code snippets
Keep in mind that i didn't try checking and running these programs .. so its quite possible that a syntax error might have crept it ... as i concentrated on concept not syntax when posting

lets start with an easy one ...

Class Example1
{

public static void main(String args[])
{
System.out.print("H"+"a");
System,out,print('H'+'a');
}
};


this is a good one ...


Class Example2
{

public Example2(Object o)

{

System.out.println("object");

}

public Example2(double[] d_a)
{

System.out.println("double array");

}


public static void main(String args[])
{

new Example2(null);

}
}



this one is simply awesome ...


Class Example3
{

public void main(String[] args)

{

Set s = new HashSet();

for(int i=0;i<100;i++)

s.add(randomInt());

System.out.println(s.size());

}


private static Integer randomInt()
{

return new Integer(new Random().nextInt());

}

};


a little tricky .. but comparatively easy ...


class Example4
{

public static synchronized void main (String args[])

{

Thread t = new Thread()

{

public void run()

{

pong();

}

};

t.run();

System.out.println("ping");

}

static synchronized void pong()
{
System.out.println("pong");
}
}

again a very good one ... must try

class Example5
{

public static void main(String args[])
{
// NOte : \u000A this is a comment
Char c =0x000A;
System.out.println(c);

}
}


a simple one again ...


class Example6
{

public static void main(String args[])
{

System.out.println(func());

}

public boolean func()
{

try
{

return true;

}

finally
{

return false;

}

}
}

Ans : Example 1
the output will be Ha169
Moral of story ...
use string con cation operator with care .. at least one of the two must be a string .. If it isn't then cast or convert

Ans : Example2
double array .. the most specific constructor is called and not the most GENERIC
We must use casting to fix it

Ans : Example3
the output will be a number close to 1
because the seed of the pseudo random number generator don't change .. as its time stamp based
may be if we put a sleep in between then the output is a number close to 100
but .. later after the session a guy came up with his laptop and ran the program in Java6 or may be java5 and the answer was a number close to 100.
when he showed it Raghavan he was surprised too said that he had checked the output on many JVMs .. ans was number close to one but strange that its 100

Ans : Example4
Wat'sthe output of above program
a) PingPong
b) PngPing
c) It varies

ans is pongping .. as its not a multi threaded program at all ... as it didn't call the start()


Ans : Example5
there will be a compile time error ,,as the \u000A in the comments will cause the comment to break up
and that in turn will give error

Ans : Example6
the ans will be false as the finally block will always be executed

There was another Example in which he tried to make a class called 'String'
and surprisingly ... it gave run time error and not compile time error

3 comments:

Anonymous said...

Dont u think in ex 6) func() dhould be static?

Lavnish said...

Yes my anonymous friend
you are right :)
as non-static function cannot be referenced from a static context so func() should be a static function ...
as said earlier i didnt try to compile .. quite possible that a syntax error might have crept...

I happen to recall One more puzzle from the seminar

class Example7
{
public static void main(String[] args)
{
System.out.println(548321+54832l);
}
}

What will be the output of above
a) 603153
b) 1096642
c) 548322
d) None of these

Lavnish said...

When you view it in html the ans is quite obvious ...
a) 603153
Coz if one pays attention the second values is a Long ... it could have been better written as
System.out.println(548321+54832L);

but if you try the above code in an editor , its tough to make out ...
:)
MORAL of the story ...
use L and not l ... one might confuse him/her-self