Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 1)
1.
What will be the output of the program?
class PassA 
{
    public static void main(String [] args) 
    {
        PassA p = new PassA();
        p.start();
    }

    void start() 
    {
        long [] a1 = {3,4,5};
        long [] a2 = fix(a1);
        System.out.print(a1[0] + a1[1] + a1[2] + " ");
        System.out.println(a2[0] + a2[1] + a2[2]);
    }

    long [] fix(long [] a3) 
    {
        a3[1] = 7;
        return a3;
    }
}
12 15
15 15
3 4 5 3 7 5
3 7 5 3 7 5
Answer: Option
Explanation:

Output: 15 15

The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.

So Output: 3+7+5+" "3+7+5

Output: 15 15 Because Numeric values will be added

Discussion:
43 comments Page 3 of 5.

Riya said:   9 years ago
Please help me out that when a (+) operator is used for concatenating and for addition purpose.

Ram said:   1 decade ago
The method fix()is fixed the is a3[1]=7, so that value is assing by the position of a[1]=7.

Sarah Glory Olivia said:   9 years ago
Here, fix(a1) is for a2, so why a1[1] in a[1] change to 7 while a1 doesn't use fix method?

Nadeem said:   1 decade ago
How is 15 printed even if we are not performing addition of a1 and a2 elements anywhere?

Ramesh said:   1 decade ago
It's all because of the cloning of the same array object reference this is the concept.

Prudhvi Kumar said:   1 decade ago
Please any one explain me output for this program and fix method in-detail.

Veerraju said:   1 decade ago
Please tell me fix() method from which class and which package.

Suresh said:   1 decade ago
@Abdul.

Numeric values will be added in the print statements.

Anandi said:   1 decade ago
The answer is 375 375. Then how it is 15 15 please explain me?

Lavanya said:   9 years ago
Out put: 3 4 5 3 7 5.

Because + are concatenate operator.


Post your comments here:

Your comments will be displayed after verification.