All mock tests are FREE till 31 December — register to save your progress

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

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. 15 : 15

Explanation

Arrays are passed by reference, so R[1] = 7 changes P, and Q is the same array {3, 7, 5}. Both sums are 15: '15:15'.

More Programming Languages and Computer Graphics questions