IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

Java Programming - Operators and Assignments - Discussion

@ : Home > Java Programming > Operators and Assignments > Finding the output - Discussion

Read more:

"I never think of the future. It comes soon enough."
- Albert Einstein
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;
    }
}

[A]. 12 15[B]. 15 15
[C]. 3 4 5 3 7 5[D]. 3 7 5 3 7 5

Answer: Option D

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.


Nagarjuna said: (Wed, Jun 30, 2010 03:30:44 AM)    
 
Can any one tell me why a1[] will also change.

Ram said: (Sat, Dec 25, 2010 10:12:19 AM)    
 
The method fix()is fixed the is a3[1]=7, so that value is assing by the position of a[1]=7.

Vasu said: (Tue, Jan 4, 2011 01:19:57 AM)    
 
While program is executing
ofter second step
the method of fix(_)
then it comes again back to 3 step.
so it changes the array elements in the position of 1
i.e (a1[1])

Vagmita said: (Fri, Jul 1, 2011 01:47:14 AM)    
 
I cant understand fix... method...

Sainath said: (Fri, Jul 8, 2011 06:26:32 AM)    
 
Can anyone explain me in clear way?

Gopi said: (Tue, Jul 12, 2011 01:40:51 AM)    
 
@Naga. It's because of reflection in referenced object.

Vijay said: (Sat, Aug 13, 2011 03:13:15 PM)    
 
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.

Prudhvi Kumar said: (Tue, Aug 30, 2011 08:09:27 AM)    
 
Please any one explain me output for this program and fix method in-detail.

Sarang said: (Sun, Sep 11, 2011 06:16:47 PM)    
 
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 ..

Arnab said: (Thu, Oct 27, 2011 09:43:10 AM)    
 
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. .....!!!!!!

Debojyoti Bose said: (Tue, Jan 17, 2012 12:18:51 PM)    
 
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).

Nadeem said: (Mon, Mar 5, 2012 01:57:59 PM)    
 
How is 15 printed even if we are not performing addition of a1 and a2 elements anywhere?

Veerraju said: (Wed, Mar 14, 2012 10:59:44 AM)    
 
Please tell me fix() method from which class and which package.

Abdul Sattar said: (Mon, Mar 19, 2012 12:59:58 PM)    
 
If we write this:

long [] a2 = a1;

which means the address of a1 is being assigned to a2,therefore a2 also contains same elements as a1.

long [] a2 = fix(a1);

the fix(a1); is only replacing the element at a1[1] which is 4 by 7, and assigning the address of a1 to a2n now both a1 and a2 contains same data elements.

up to this point a1 should contains {3,7,5} not {3,4,5}

so the output should be 375 375

why 15 15..........?

Write your comments here:
Name *:     Email:


© 2008-2012 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!