Monday, February 22, 2010

Java Quiz: Array Initialization

Is the following correct? What will the output be?
And PS: Try to figure it out before actually running the code :-)
public class BirthDays
{
static int [] year = new int[100];
public static void main(String [] args)
{
for(int i=0;i<100;i++)
System.out.println("year[" + i + "] = " + year[i]);
}
}

Please give your answer in the comments.

2 comments:

Forumtroll said...

Yep, it's correct, and output is 0 on every line.

Ewald Horn said...

Year 0 - 99, all with 0 as int's are automatically initialised to 0 in Java.

Running a similiar thing in C is more fun, you'd have a bunch of seemingly random numbers :)

So do we win something? I want an icecream, with a flake, and a sugarcone!