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.
Navneet said:
8 years ago
What is role of fix()?
Navneet said:
8 years ago
What is role of fix()?
Navneet said:
8 years ago
What is role of fix()?
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.
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)
Hari said:
8 years ago
Can anyone explain fix method in detail?
(2)
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