S artaySwap Main Page Problems- Solve a Problem O BJP4 Self-Check 7.19: Referenc
ID: 3708269 • Letter: S
Question
S artaySwap Main Page Problems- Solve a Problem O BJP4 Self-Check 7.19: ReferenceMystery1 avrite Language/Type: &Java; arrays reference semantics Author: Marty Stepp (on 2016/09/08) The following program produces 4 lines of output Write each line of output below as it would appear on the console import java.util. for Arrays class public class ReferenceMysteryl( public static void main(Stringl] args) t int x 0; int [ ] a new int[4]; nystery(x, a); System.out.printin(xArrays.tostring(a)); nystery(x, a); System.out.println(xArrays.tostring(a)); public static void mystery(int x, int[) )( x+4 alx) System out.printnxArrays.tostring(a)); line 1 2 (o,0,s,e line 2 1 to.e,1,ej line 3 te,e.1.1 ine 4 2 te,e,i. Submit B eExplanation / Answer
Change the value of variable doesn't reflect in the calling function.
But, Change of array value will reflect in the calling function.
--------------------------------------------------------------------------
value of x before the call to mystery(x,a); is 1
So, First call to the function is
mystery([0,0,0,0], 1)
So, x is incremented by 1. So x becomes 2 and x[2] becomes 1
So It prints 2 [0, 0, 1, 0]
In the main function it prints
1 [0, 0, 1, 0]
Because change of variable doesn't reflects and array will reflects in main function
In main, x value is incremented by 1 more. So, x value is 2
Second call becomes mystery([0,0,1,0], 2)
In mystery, x is incremented by 1 so, It becomes 3 and changes the array value x[3] = 1
So, It prints 3 [0, 0, 1, 1]
In the main function it prints
2 [0, 0, 1, 1]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.