Milbourne.com
15Jan/110

Interview Question #1

Posted by Mark Milbourne

Today I came accross an awesome interview question for programmers.

Problem

Given this psuedo C like code;

if "operation"
    printf("Hello");
else
    printf("World");

What "condition" would cause it to output "HelloWorld"?

C Solution

if (0 == printf("Hello"))
  printf("Hello");
else
  printf("World");

C# Solution

if ((new string[] { "Hello" }).All<string>(delegate(string n) { Console.Write(n); return false; }))
{
    Console.Write("Hello");
}
else
{
    Console.Write("World");
}

JAVA Solution

Graciously from Jack Dunham (the Java man);

if (new Object() 
  {@Override public boolean equals(Object foo) { System.out.print("hello ");return false;}}
  .equals("hello ")) {
System.out.print("hello ");
} else {
System.out.print("World");
}

Post to Twitter