Menu
Topics Index
...
`

Arrays And Methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays And Methods
What will be the output of the following program?
class Moon
{
    public static void main(String [] args)
    {
        Moon O = new Moon();
        O.run();
    }
    
    void run()
    {
        long [] a = {3, 4, 5};
        long [] b = modify(a);
    
        System.out.print(a[0] + a[1] + a[2] + " ");
    
        System.out.println(b[0] + b[1] + b[2]);
    }
    
    long [] modify(long [] c)
    {
        c[1] = 7;
        return c;
    }
}

A. 3 4 5 3 7 5
B. 3 7 5 3 7 5
C. 12 15
D. 15 15
E. Compilation Error
Topic: Java Multiple Methods In One Class

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Optin A is correct as for the first print the input to array a will be printed and then the modified data of a that is array b will be printed and as c[1]=7 thus in place of 4 you'll have 7 in output :)

Posted by Jigar Mehta    2014-02-17 13:04:26


after the function calls completed values are printed and hence ans is D since start address is send and c[1] is being modified..3+7+5

Posted by Karthik Narahari    2014-02-17 13:04:56


answer is d 15 15..bcoz c[1] is 7...modify function changes the values of a[1] and b[1] to 7..thus the answer is 15 15

Posted by Sneha Rapool    2014-02-17 13:11:37


Congratulations Karthik Narahari. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-18 04:43:58


© meritcampus 2019

All Rights Reserved.

Open In App