Assume the following compiles and runs - what would be theoutput? char[] a = new
ID: 3613112 • Letter: A
Question
Assume the following compiles and runs - what would be theoutput?char[] a = new char[3]; char[] b; for (int i=0; i<a.length;i++) { a[i]='a'; } b=a; b[2]='b'; System.out.println("a[1]=" + a[1] + ", a[2] =" + a[2]); System.out.println("b[1]=" + b[1] + ", b[2] =" + b[2]);
Assume the following compiles and runs - what would be theoutput?
char[] b; for (int i=0; i<a.length;i++) { a[i]='a'; } b=a; b[2]='b'; System.out.println("a[1]=" + a[1] + ", a[2] =" + a[2]); System.out.println("b[1]=" + b[1] + ", b[2] =" + b[2]);
System.out.println("b[1]=" + b[1] + ", b[2] =" + b[2]);
Explanation / Answer
please rate - thanks the declaration of a creates an array, and allocates it memory. the declaration of b creates the array but doesn't allocate itmemory. you then fill the array a with the value 'a' and then say that b is in the same memory locations as a (inFORTRAN these arrays would have been EQUIVALENCE) which is a goodterm for this they have equivalent/ same values so when you make a change in a or b you will now change the otheras well. so when change b[2] you are also changing a[2] a[1]=a, a[2] =b b[1]=a, b[2] =b the output is char[] a = new char[3]; char[] b; for (int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.