wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization of Local Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initialization of Local Arrays" Watch "Initialization of Local Arrays" New topic

Initialization of Local Arrays

Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
I am confused i have read in Khalid Mughal that local array's doesnot get intialized to default values, but while doing mock test i came across the question:
What will happen if you try to compile and run the following code?
public class Q
public static void main(String argv[])
int anar[]=new int[5];
1) Error: anar is referenced before it is initialized
2) null
3) 0
4) 5
according to me the answer is 1 as anar is referenced before it is intialized but to my surprise the answer is 3 that is zero.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
On the contrary rashi, arrays are always initialized to their default values whether they where defined locally or globally. It seems like a typo error to me.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Thanks Vicken i guess you are right.
I agree. Here's the link: http://aspose.com/file-tools
subject: Initialization of Local Arrays
Similar Threads
Local variable initialization - source: http://www.jchq.net/mockexams/exam1.htm
mock exam question
doubt(marcus mock test)