Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For question 6, I don\'t know why the output is 0 1 2 instead of 1 2 2. I think

ID: 666344 • Letter: F

Question

For question 6, I don't know why the output is 0 1 2 instead of 1 2 2. I think "array1[0] = 0; array1[1] = 1;" are wrote after array1 gave its value to array2, so I think "array1[0] = 0; array1[1] = 1;" these statements only change the array1 not array2 any more.

What is the printout for following code public class Test { Question 6 Correct Mark 1.00 out of 1.00 public static void main(Stringl args) int[] array1 = {1, 2, 3} int[] array2 = {4, 5, 6); array2 = array 1; array 1 [0] = 0; array 1 [1] = 1; array2[2] = 2 for (int i = 0; i

Explanation / Answer

6.

It's correct. Once you do array2=array1 both the objects are allocated on the heap in Java and they share same memory. So basically this is a pass by reference examples where array2 has same values as array1. Now any change to array1 values will be reflected in array2 values.

7.

In 7 also same concept of heap is applied where it is allocated on heap instead of stack.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote