aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Q" Watch "Mock Exam Q" New topic
Author

Mock Exam Q

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
class SomeClass
{
public static void main(String args[])
{
int size = 10;
int[] arr = new int[size];
for (int i = 0 ; i < size ; ++i) System.out.println(arr[i]);
}
The above code prints 0 ten times.Shouldn't it print 0 only nine times.Any comments/suggestions
TIA
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi Tanuja,
When you declare an array, all fields of array are initialized to default values( Classes to null,numeric values to 0, boolean values to false)
That's all
Jamal
Alf Fernandez
Greenhorn

Joined: Sep 01, 2001
Posts: 13
Tanuja,
The size of the array you has declares, is 10 (int size = 10; int[] arr = new int[size]
And the for loop, goes from the first element to the last (the first element of an array is 0, and de last, is array.length minus one)
for (int i = 0 ; i < 10 ; ++i)
So, it prints the contents of the array, that are 10 ceros.
[The above code prints 0 ten times.Shouldn't it print 0 only nine times.Any comments/suggestions]

Alf.


Alf.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
midirectorio,
Please read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
 
subject: Mock Exam Q