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 2 of 5.
Krishna Murthy said:
9 years ago
In C, we have pointers to get the address of a variable. But in java pointers concept is not there but java has indirectly using pointers such as objects. Objects are nothing but references. So the array is a class of object in java. That's why a1 affected by the method fix().
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?
Navneet said:
8 years ago
What is role of fix()?
Navneet said:
8 years ago
What is role of fix()?
MK Dino said:
1 decade ago
The reason why is because arrays are not primitive types.
Primitive types: pass copy of variable to parameter.
Objects(user defined types): pass by reference variable/objects as parameter.
There are exceptions to arrays(int[],long[], etc).
Formally speaking, an array is a reference type, though you cannot find such a class in the Java APIs.
Primitive types: pass copy of variable to parameter.
Objects(user defined types): pass by reference variable/objects as parameter.
There are exceptions to arrays(int[],long[], etc).
Formally speaking, an array is a reference type, though you cannot find such a class in the Java APIs.
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.
}
}
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.
}
}
Suraj kumar said:
8 years ago
fix() is from which package? Please 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
}
a3[1] = 7; // a3[1]=7 means a1[1] will become 7(a1[1]=7), now a1={3,7,5}
return a3;// return a1
}
Rishikesh Joshi said:
8 years ago
How the value of A1[1] has changed? explain.
Rajni said:
8 years ago
Please tell me how to work bubble sort method with easy program? Please explain with an example.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers