• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

can anyone explain how this is answer?

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below shown is the program code



According to me the answer is 4,but the actual answer is 2,i am not getting how................please if anyone can explain me this program in detail......
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

mathur

------------------------------------------------
What is the result of trying to compile and run this program.
public class Test
{
public static void main(String[] args)
{
int[] a = {1};
Test t = new Test();
t.increment(a);
System.out.println(a[a.length - 1]);//line 1
}
void increment(int[] i)
{
i[i.length - 1]++;//line 2
}
}
-----------------------------------------------------


At line 2 i[1-1]-->i[0]-->1++=2 // herew i.length is 1

so now i[0] is 2 not 1
ok
At line 1 a[1-1]-->a[0]-->2// Because at line 2 we incremented the value of i[0]


Thanks

Anil Kumar
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see dhwani mathur

by writing
int[] a = {1};

you have created an array of lenght 1 and with element value 1..

Now by calling method increment(int[] i)
you have incremented the first and only element of array by 1
i.e. now it has become 2..
Right..

Then by writing
System.out.println(a[a.length - 1]);
you are printing the first and only element of array with index 0..

Remember a.length is giving you 1..
So a[a.length - 1] giving you a[1-1] i.e. a[0]..

So no chances of ArrayIndexOutOfBounds Exception ..

Regards..
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:
Below shown is the program code





Can you tell me how according to you answer is 4??

I guess you know arrays are objects in java, and you can change the state of the object. Here you are doing the same thing.You are updating element at index 0.(Please Note : you are not updating array refference). For more explanation see following

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani,


public class Test{
public static void main(String[] args){
int[] a = {1};//declares and assigned an anonymous array to a
Test t = new Test();//declares and assign an instance of Test to t
t.increment(a);//call increment method, passing a
System.out.println(a[a.length - 1]);//prints a[0] since a.length is always equal to 1
}
void increment(int[] i){
i[i.length - 1]++;//i[0] is incremented (that is equivalent to 1++)
}
}



Hope this helps,

Regards,

Lhorenz
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you dear all for such a good explanation,i got cleared with my doubt.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"dhwani mathur",

Please Quote Your Sources.

Thanks,
Henry
 
Poop goes in a willow feeder. Wipe with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic