📅  最后修改于: 2023-12-03 14:54:49.455000             🧑  作者: Mango
This is a question from the Computer Science section of the UGC NET exam held in June 2015. The question is related to Java programming language.
What will be the output of the following Java program?
public class Test {
public static void main(String[] args) {
int x[] = {1, 2, 3, 4};
int y[] = {2, 3, 4, 5};
x = y;
y = x;
System.out.print(y[0] + " ");
System.out.print(x[0]);
}
}
(A) 2 2
(B) 1 2
(C) Compilation Error
(D) None of the Above
The given Java program creates two integer arrays x and y, each containing four elements. The elements of the arrays are initialized with the values 1 to 4 and 2 to 5 respectively.
int x[] = {1, 2, 3, 4};
int y[] = {2, 3, 4, 5};
The program then assigns the reference of array y to variable x.
x = y;
After that, the reference of array x is assigned to variable y.
y = x;
Now, both x and y refer to the same integer array {2, 3, 4, 5}. Therefore, y[0]
and x[0]
both will have the value 2.
System.out.print(y[0] + " ");
System.out.print(x[0]);
So, the correct output of the program will be option (A) 2 2.
Hence, the answer is (A) 2 2.
This question tests the candidate's knowledge of Java programming concepts such as arrays and references. It also evaluates the candidate's ability to predict the output of a given program.