Could someone please help me with these problems and explain how they are done?
ID: 3730431 • Letter: C
Question
Could someone please help me with these problems and explain how they are done? Thank you!
b) //main method intp array-B-1, 2, 3); method(array, array[O|) public static void method(int source, int value) source = new inteurselength); for (int i-0; issourlenetb: ti) source[] = value+1; Heap Main Stack Frame Identifier Address Contents 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 Identifier Address Contents 101 102 103 array Method Stack Frame Identifier Address Contents source value 200 201 202Explanation / Answer
b)
Initially array values are
array[0]=9
array[1]=-1
array[2]=2
array[3]=3
we are passing array , array[0] to method where array[0]=9
so inside method source is referring to starting of array and value is equal to 9
source.length will return one because it refers to the starting element of the array not the entire array
so loop runs for i=0 only
hence source[0] becomes 9+1 = 10
c)
Initially array values are
array[0]=7
array[1]=1
array[2]=5
array[3]=2
array[4]=3
we are passing array ,10,12 to method
so inside method source is referring to starting of array and a=10,b=12
so source[0]=10 and source[1]=12
but source = new int[3] allocates a new memory to source equal to size of 3 integers(array of 3 intgers), so the values previously stored are lost
d)
in above b,c method doesnt return anything but in this case it returns int[](its written public static int[] method)
intially
data[0]= 9 ,data[1]= 1 ,data[2]= 3 ,data[3]= 5,data[4]= 7
and data[2] =3 is passed to method so size in method becomes 2
so source will be an array of 2 integers
and source[0]=source[1]=size=2
and source is returned and alloted to data , hence starting address of data becomes starting address of source
so data[0]= 2 data[1]= 2 ,data[2],data[3],data[4] will have garbage valiues
This happens so because any array is alloted a contiguous memory, so if data[0] is at address 200(for example) the data[1] will be at 200+(size of int) = 204 and so on
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.