recategorized by
4,073 views
1 votes
1 votes

 

What is the output of the following JAVA program?

class simple
{
    public static void main(String[ ] args)
    {
        simple obj = new simple();
        obj.start();
    }
    void start()
    {
        long [] P = {3, 4, 5};
        long [] Q = method (P);
        System.out.print (P[0] + P[1] +P[2]+”:”);
        System.out.print (Q[0] + Q[1] + Q[2]);
    }
    long [ ] method (long [ ] R)
    {
        R[1] = 7;
        return R;
    }
} //end of class
  1. 12:15
  2. 15:12
  3. 12:12
  4. 15:15
recategorized by

3 Answers

3 votes
3 votes
Answer is 4 15:15

1. p=3,4,5

2. q=3,7,5

when we have the statement long[]Q=method(P)

 

It will call method function and p is passed as parameter and in method function we are changing r[1]=7.

Actually arrays are passed as reference. So whatever changes you make in r, it actually gets reflected in p. Hence both the arrays will display sum=3+7+5=15
0 votes
0 votes

D. 15:15

long [] Q = method (P);
 System.out.print (P[0] + P[1] +P[2]+”:”);

because 'method' function is called before printing values of P and thus it has updated values of P also.

Related questions

0 votes
0 votes
1 answer
2
2 votes
2 votes
3 answers
3
makhdoom ghaya asked Oct 4, 2016
2,594 views
Java uses threads to enable the entire environment to be ______.SymmetricAsymmetricSynchronousAsynchronous