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;
}
}
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 4 of 5.
Gopi said:
1 decade ago
@Naga. It's because of reflection in referenced object.
Nagarjuna said:
2 decades ago
Can any one tell me why a1[] will also change.
Rishikesh Joshi said:
8 years ago
How the value of A1[1] has changed? explain.
Suraj kumar said:
8 years ago
fix() is from which package? Please explain.
Meera said:
9 years ago
Thank you all for the given explanations.
Jegathees said:
1 decade ago
Why & how to change a1[1] value is 7?
Hari said:
8 years ago
Can anyone explain fix method in detail?
(2)
Sainath said:
1 decade ago
Can anyone explain me in clear way?
Vagmita said:
1 decade ago
I cant understand fix... method...
Aryan Sahebrao Salunkhe said:
2 years ago
Good, thanks all for explaining.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers