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 1 of 5.
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.
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)
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
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)
Navneet said:
8 years ago
What is role of fix()?
Beeram Siva Damodar Reddy said:
1 decade ago
//**Here when we call fix (a1) method then control will goes to fix method 7 execute it what the result is a1 is passed as a parameter to the fix (long[] a3).
So a3 is replaced by a1 & a3[1] = a1[1] = 7 value will be returned to its calling method. So, finally we will get a1[1] = 7.
I hope my explanation is easy to understand**//.
So a3 is replaced by a1 & a3[1] = a1[1] = 7 value will be returned to its calling method. So, finally we will get a1[1] = 7.
I hope my explanation is easy to understand**//.
Meera said:
9 years ago
Thank you all for the given explanations.
Lavanya said:
9 years ago
Out put: 3 4 5 3 7 5.
Because + are concatenate operator.
Because + are concatenate operator.
Riya said:
9 years ago
Please help me out that when a (+) operator is used for concatenating and for addition purpose.
Anjali said:
9 years ago
When both operands are of the numerical type then the operation is an addition.
If both operands are of string type then it is a concatenation.
If both operands are of string type then it is a concatenation.
Gaurav gupta said:
9 years ago
Long [] a1 = {3, 4, 5};
Long [] a2 = fix (a1) ;
As we know an array itself is a pointer. Here the address of a1 is stored at a2. Therefore when the a1 is changed it will be reflected globally and therefore both have same data.
Long [] a2 = fix (a1) ;
As we know an array itself is a pointer. Here the address of a1 is stored at a2. Therefore when the a1 is changed it will be reflected globally and therefore both have same data.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers