LearnFrenzy
Sign in Take a free test

Arrays in Java · Predict the Output · Question 17

Arrays.asList writes through to the array Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] arr = {"a", "b", "c"};
        List<String> list = Arrays.asList(arr);
        list.set(0, "z");
        arr[2] = "y";
        System.out.print(Arrays.toString(arr) + " " + list + " ");
        try {
            list.add("d");
        } catch (UnsupportedOperationException e) {
            System.out.println("cannot add");
        }
    }
}
  • A [z, b, y] [z, b, y] cannot add Correct
  • B [a, b, y] [z, b, c] cannot add
  • C [z, b, y] [z, b, y, d]
  • D [z, b, c] [z, b, y] cannot add
Correct Answer: Option A

The list is a view of the array: changes in either are visible in both, but its size cannot change.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links