0
0
Javaprogramming~15 mins

Array declaration and initialization in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of array initialization and access
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        int[] numbers = new int[3];
        numbers[0] = 5;
        numbers[1] = 10;
        numbers[2] = 15;
        System.out.println(numbers[1]);
    }
}
A0
B5
C10
D15
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Output of array initialization with values
What will be printed by this Java program?
Java
public class Main {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana", "Cherry"};
        System.out.println(fruits.length);
    }
}
A3
B2
C0
DCompilation error
Attempts:
2 left
💻 code output
advanced
2:00remaining
Output of multi-dimensional array initialization
What is the output of this Java code?
Java
public class Main {
    public static void main(String[] args) {
        int[][] matrix = new int[2][3];
        matrix[0][1] = 7;
        matrix[1][2] = 9;
        System.out.println(matrix[1][2] + matrix[0][1]);
    }
}
AArrayIndexOutOfBoundsException
B79
C0
D16
Attempts:
2 left
💻 code output
advanced
2:00remaining
Output of array default values
What will this Java program print?
Java
public class Main {
    public static void main(String[] args) {
        boolean[] flags = new boolean[4];
        System.out.println(flags[2]);
    }
}
Atrue
Bfalse
Cnull
DCompilation error
Attempts:
2 left
🧠 conceptual
expert
3:00remaining
Number of elements in a jagged array
Consider the following Java code snippet. How many elements does the jagged array contain in total?
Java
public class Main {
    public static void main(String[] args) {
        int[][] jagged = new int[3][];
        jagged[0] = new int[2];
        jagged[1] = new int[4];
        jagged[2] = new int[3];
    }
}
A9
B7
C3
D12
Attempts:
2 left