LearnFrenzy
Sign in Take a free test

Arrays in Java · Exercise 2 of 4

Arrays in Java Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 11 Easy

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        char[] c = "LEARN".toCharArray();
        for (int i = 0, j = c.length - 1; i < j; i++, j--) {
            char t = c[i];
            c[i] = c[j];
            c[j] = t;
        }
        System.out.println(Arrays.toString(c) + " " + new String(c));
    }
}
A [N, R, A, E, L] NRAEL
B [L, E, A, R, N] LEARN
C [N, R, A, E, L] LEARN
D [N, E, A, R, L] NEARL
Question page & discussion
Question 12 Medium

What is the output of the following Java program?

Main.java
import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        String[] w = {"banana", "Cherry", "apple", "date"};
        Arrays.sort(w);
        System.out.println(Arrays.toString(w));
        Arrays.sort(w, Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder()));
        System.out.println(Arrays.toString(w));
    }
}
A [Cherry, apple, banana, date] [date, apple, Cherry, banana]
B [apple, banana, Cherry, date] [date, apple, banana, Cherry]
C [Cherry, apple, banana, date] [date, apple, banana, Cherry]
D [apple, banana, Cherry, date] [date, apple, Cherry, banana]
Question page & discussion
Question 13 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] count = new int[26];
        for (char ch : "mississippi".toCharArray()) count[ch - 'a']++;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 26; i++) if (count[i] > 0) sb.append((char) ('a' + i)).append(count[i]);
        System.out.println(sb);
    }
}
A i4m1p2s4
B m1i4s4p2
C i4s4p2m1
D m1i1s1p1
Question page & discussion
Question 14 Hard

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] row = new int[3];
        int[][] grid = new int[2][];
        Arrays.fill(grid, row);          // both slots point to the SAME row array
        grid[0][1] = 5;
        int[][] good = new int[2][3];
        for (int[] r : good) Arrays.fill(r, 7);
        good[0][1] = 5;
        System.out.println(Arrays.deepToString(grid) + " " + Arrays.deepToString(good));
    }
}
A [[0, 5, 0], [0, 5, 0]] [[7, 5, 7], [7, 7, 7]]
B [[0, 5, 0], [0, 0, 0]] [[7, 5, 7], [7, 7, 7]]
C [[0, 5, 0], [0, 5, 0]] [[7, 5, 7], [7, 5, 7]]
D NullPointerException
Question page & discussion
Question 15 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int[] a = {12, 35, 1, 35, 10, 34};
        int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
        for (int x : a) {
            if (x > first) {
                second = first;
                first = x;
            } else if (x > second && x != first) {
                second = x;
            }
        }
        System.out.println(first + " " + second);
    }
}
A 35 34
B 35 35
C 34 35
D 35 12
Question page & discussion
Question 16 Medium

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {3, 1, 4, 1, 5, 9, 2};
        int[] pre = new int[a.length + 1];
        for (int i = 0; i < a.length; i++) pre[i + 1] = pre[i] + a[i];
        System.out.println(Arrays.toString(pre));
        System.out.println((pre[5] - pre[2]) + " " + (pre[7] - pre[0]));     // sum of a[2..4] and of everything
    }
}
A [0, 3, 4, 8, 9, 14, 23, 25] 10 25
B [3, 4, 8, 9, 14, 23, 25] 10 25
C [0, 3, 4, 8, 9, 14, 23, 25] 9 25
D [0, 3, 4, 8, 9, 14, 23, 25] 10 23
Question page & discussion
Question 17 Hard

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
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
Question page & discussion
Question 18 Medium

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] temps = {31, 29, 35, 33, 28, 30};
        System.out.println(Arrays.stream(temps).max().getAsInt() + " " + Arrays.stream(temps).min().getAsInt() + " "
                + Arrays.stream(temps).sum() + " " + Arrays.stream(temps).average().getAsDouble() + " "
                + Arrays.stream(temps).filter(t -> t > 30).count());
    }
}
A 35 28 186 31.0 3
B 35 28 186 31 3
C 35 29 186 31.0 2
D 35 28 180 30.0 3
Question page & discussion
Question 19 Medium

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6};
        int k = 8;
        int n = a.length;
        int[] r = new int[n];
        for (int i = 0; i < n; i++) r[(i + k) % n] = a[i];
        System.out.println(Arrays.toString(r));
    }
}
A [5, 6, 1, 2, 3, 4]
B [3, 4, 5, 6, 1, 2]
C [6, 1, 2, 3, 4, 5]
D ArrayIndexOutOfBoundsException
Question page & discussion
Question 20 Hard

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        char[] word = {'J', 'a', 'v', 'a'};
        System.out.println(word);                       // println(char[]) prints the characters
        System.out.println(String.valueOf(word, 1, 2));
        System.out.println(word.length + word[0]);       // int + char: arithmetic
    }
}
A Java av 78
B [J, a, v, a] av 78
C Java va 4J
D Java av 4J
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links