in Java recategorized by
3,170 views
1 vote
1 vote

 

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
in Java recategorized by
3.2k views

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

1 comment

Right
0
0
0 votes
0 votes
option (3) 12:12 is answer

1 comment

how answer came?

can u explain me?
0
0
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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true