Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
check out the following code

Q. What code placed after the comment //For loop would populate the elements of the array ia[] with values of the variable i.?
public class Lin{
public static void main(String argv[]){
Lin l = new Lin();
l.amethod();
}
public void amethod(){
int ia[] = new int[4];
//Start For loop
{
ia[i]=i;
System.out.println(ia[i]);
}
}
}
1) for(int i=0; i < ia.length(); i++)
2) for (int i=0; i< ia.length(); i++)
3) for(int i=1; i < 4; i++)
4) for(int i=0; i< ia.length;i++)
Answer given is 4.
it's correct but I think 3 is also correct, because in case of 3 the array will be like this
[0,1,2,3]
here also the values papulated are same as the vale of i.
Does anybody wants to put comment?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3) for(int i=1; i < 4; i++)


this is incorrect because it will only loop from 1 to 3, not 0 to 3. Easy to miss this one!
Take care,
Khalid
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Khalid I am fully agreed with your point.
But if you look at the question,it is saying
"
What code placed after the comment //For loop would populate the elements of the array ia[] with values of the variable i.?"
...in 4th case the values will be like this:
i=0 a[0]=0
i=1 a[1]=1
i=2 a[2]=2
i=3 a[3]=3
...in 3th case the values will be like this:
i=1 a[1]=1
i=2 a[2]=2
i=3 a[3]=3
I am saying, in 3rd case also the values in array is matching with the value of i.
am i making sense?
 
Khalid Bou-Rabee
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. All the values (due to default initialization) match those in the array after executing option 4. However, the question explicitly asks which option will populate the array. So, Marcus Green is asking which lines will actually fill in the entire array with values given by the user, not automatically initialized by the VM.
Regards,
Khalid
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic