Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

import java.util.*;

public class Item22
{
    public static void main(String[] args)
    {
        Integer[] boxed = {10, 20, 30};
        List<Integer> view = Arrays.asList(boxed);
        boxed[0] = 99;
        System.out.println(view.size() + " " + view.get(0));
    }
}

  • A.The program prints 1 10
  • B.The program prints 3 99
  • C.The program prints 3 10
  • D.The program prints 1 99

Answer: B

An Integer[] is an array of references, so varargs spreads it and the list holds three elements, unlike the int[] case where the whole array arrives as one object. The list is also a VIEW over that array, not a copy, so writing to the array afterwards is visible through it and the first element reads 99.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.