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.
Veerraju said:
1 decade ago
Please tell me fix() method from which class and which package.
Nadeem said:
1 decade ago
How is 15 printed even if we are not performing addition of a1 and a2 elements anywhere?
Debojyoti Bose said:
1 decade ago
The situation, when working with arrays, is somewhat different. If we were to make copies of arrays to be sent to methods, we could potentially be copying very large amounts of data.
Not very efficient!
Arrays are passed-by-reference. Passing-by-reference means that when an array is passed as an argument, its memory address location is actually passed, referred to as its "reference". In this way, the contents of an array CAN be changed inside of a method, since we are dealing directly with the actual array and not with a copy of the array.
int [ ] num = {1, 2, 3};
testingArray(num); //Method call
System.out.println("num[0] = " + num[0] + "\n num[1] = " + num[1] + "\n num[2] =" + num[2]);
. . .
//Method for testing
public static void testingArray(int[ ] value)
{
value[0] = 4;
value[1] = 5;
value[2] = 6;
}
Output:
num[0] = 4
num[1] = 5
num[2] = 6
(The values in the array have been changed.
Notice that nothing was "returned".)
You will need to be careful when sending an array to a method. Remember that any changes made to the array in the method will change the data in the original array. Be sure that your intention is to change the original data (thus losing the original data).
Not very efficient!
Arrays are passed-by-reference. Passing-by-reference means that when an array is passed as an argument, its memory address location is actually passed, referred to as its "reference". In this way, the contents of an array CAN be changed inside of a method, since we are dealing directly with the actual array and not with a copy of the array.
int [ ] num = {1, 2, 3};
testingArray(num); //Method call
System.out.println("num[0] = " + num[0] + "\n num[1] = " + num[1] + "\n num[2] =" + num[2]);
. . .
//Method for testing
public static void testingArray(int[ ] value)
{
value[0] = 4;
value[1] = 5;
value[2] = 6;
}
Output:
num[0] = 4
num[1] = 5
num[2] = 6
(The values in the array have been changed.
Notice that nothing was "returned".)
You will need to be careful when sending an array to a method. Remember that any changes made to the array in the method will change the data in the original array. Be sure that your intention is to change the original data (thus losing the original data).
Arnab said:
1 decade ago
I didn't understand....the arguement in fix() in the calling function is a1 whose values are copied in a3. So why the hell a1 gonna be updated if a3 is changed! Besides the return keyword returns the updated value to a2 not to a1. .....!!!!!!
Sarang said:
1 decade ago
a1[0]=3,a1[1]=7,a1[2]=5;a2[0]=3,a2[1]=7,a2[2]=5
a1[0]+a1[2]+a1[3]=15 & a2[0]+a2[2]+a2[3]=15 ..
a1[0]+a1[2]+a1[3]=15 & a2[0]+a2[2]+a2[3]=15 ..
Prudhvi Kumar said:
1 decade ago
Please any one explain me output for this program and fix method in-detail.
Vijay said:
1 decade ago
3 elements should be printed know why only 2 elements are being printed
When I give like this
System.out.print("a1[0]"+a1[0] +"a1[1]"+ a1[1] +"a1[2]"+ a1[2] + " ");
Then it will give the result as
a1[0] = 3
a1[1] = 7
a1[2] = 5 this is why.
When I give like this
System.out.print("a1[0]"+a1[0] +"a1[1]"+ a1[1] +"a1[2]"+ a1[2] + " ");
Then it will give the result as
a1[0] = 3
a1[1] = 7
a1[2] = 5 this is why.
Gopi said:
1 decade ago
@Naga. It's because of reflection in referenced object.
Sainath said:
1 decade ago
Can anyone explain me in clear way?
Vagmita said:
1 decade ago
I cant understand fix... method...
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers