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 1 of 5.

Aryan Sahebrao Salunkhe said:   2 years ago
Good, thanks all for explaining.

Sowmya said:   5 years ago
long[] a1={3,4,5};
long[] a2=a1;

This is equal to,
a1[0]=a2[0] and a1[1]=a2[1] and a1[2]=a2[2].

And in fix method, a3 is method local variable it is not an array.
So changes made to a1 is also reflected in a2 so the answer is 15 15.
(3)

Salik Khan said:   5 years ago
It's because in java array is passed by reference, so any changes in the method reflect in the actual array.

Rajni said:   8 years ago
Please tell me how to work bubble sort method with easy program? Please explain with an example.

Hari said:   8 years ago
Can anyone explain fix method in detail?
(2)

Krishna mohan said:   8 years ago
@ALL.

Refer this code.

public class PassA {

public static void main(String[] args) {
PassA p = new PassA();
p.start();
}
void start()
{
long a1[]={3,4,5};
for(long i:a1)
System.out.print(i+"" );
System.out.println(" ");
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 [] a1)
{
a1[1] = 7;
for(long i:a1)
System.out.print(i);
System.out.println(" ");

return a1;
}
}

Output
345
375
15 15
(1)

Rishikesh Joshi said:   8 years ago
How the value of A1[1] has changed? explain.

Prashant said:   8 years ago
long[] fix(long[] a3) { // a3=a1 and a1= {3,4,5}
a3[1] = 7; // a3[1]=7 means a1[1] will become 7(a1[1]=7), now a1={3,7,5}
return a3;// return a1
}

Suraj kumar said:   8 years ago
fix() is from which package? Please explain.

Mahesh said:   8 years ago
According to me,

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);// In this line we are sending the reference variable of a1.
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3) // Now a3 also starts pointing to the same array as a1 is pointing.
{
a3[1] = 7;// Any modification done now will reflect on both a1..
return a3;//After returning this value array a2 starts will start to point a1 array as well.
}
}


Post your comments here:

Your comments will be displayed after verification.