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

Discussion

What will be the output of the program?

import java.util.*;

public class Sketch21
{
    public static void main(String[] args)
    {
        int[] raw = {1, 2, 3};
        System.out.println(Arrays.asList(raw).size());
    }
}

  • A.The program prints 1
  • B.The program prints 0
  • C.Compilation fails.
  • D.The program prints 3

Answer: A

asList takes a varargs parameter of a reference type, and int[] is itself an object rather than an array of objects, so the whole array becomes a single element. The list holds one int[] and its size is 1. An Integer[] would have been spread into three elements.

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

Post your comment

Please log in or register to post a comment.