SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Chapter:
Java Primer
Exercise:
Exercises
Question:2 | ISBN:9781118771334 | Edition: 6

Question

Suppose that we create an array A of GameEntry objects, which has an integer scores field, and we clone A and store the result in an array B. If we then immediately set A[4].score equal to 550, what is the score value of the GameEntry object referenced by B[4]?

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

class Testarray2{
public static void main(String args[])
{
int A[]={33,3,4,450,120};
A[4]=550;
int B[] = A.clone();
for(int i=0;i<A.length;i++)
 System.out.println(A[i]);
for(int i=0;i<B.length;i++)
 System.out.println(B[i]);
}
}
 

The game entry object referenced by B[4] will updated with 550

0 1

Discussions

njorogemaina

Well, I think the question states that, if we have an array A and then take another array B which is a clone of A. If we then change the value of A[n], what will be the value referenced by B[n]. The answer is the original A[n] before it was changed.

public class TestArray2 {

    public static void main(String[] args) {
        int A[] = {1, 2, 3, 4, 5};
        int B[] = A.clone();
        A[4] = 44;
        System.out.println(B[4]); // 5
    }
}

Post the discussion to improve the above solution.