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
- What is the output of the following C program? (Assume little-endian representation, with the least significant byte at the lowest address,…
- Given below are three implementations of swap() in C++: (a) void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } called as swap…
- In Java, which of the following statements is/are True ? S1 : The ‘final’ keyword applied to a class definition prevents the class from bei…
- Which of the following statements is/are True ? P : C programming language has a weak type system with static types. Q : Java programming l…
- A graphic display system has a frame buffer that is 640 pixels wide, 480 pixels high and 1 bit of color depth. If the access time for each…
- Which of the following statements is/are True regarding the solution to the visibility problem in 3D graphics ? S1 : The Painter’s algorith…