What will be the output of following code?

class Main {
    public static void main(String[] args) {
        int[] array1 = {1, 2};
        for (long x : array1) {
In this case array has int elements which are casted implicitly, as there is no precision lost with the cast and there is no compilation exception. The loop prints values in order.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes