LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Predict the Output · Question 14

Changing the elements of a final array Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    static final int[] DAYS = {31, 28, 31};

    public static void main(String[] args) {
        DAYS[1] = 29;
        int[] alias = DAYS;
        alias[2] = 30;
        System.out.println(Arrays.toString(DAYS));
    }
}
  • A [31, 29, 30] Correct
  • B [31, 28, 31]
  • C Compilation error
  • D [31, 29, 31]
Correct Answer: Option A

A final array reference still allows its elements to change – a "constant" array is not really constant. Use List.of for an unmodifiable list.

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