0
0
Javaprogramming~15 mins

One-dimensional arrays in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of array element modification
What is the output of this Java code snippet?
Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        arr[2] = arr[0] + arr[4];
        System.out.println(arr[2]);
    }
}
A8
B3
C5
D6
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Sum of array elements
What is the output of this Java program?
Java
public class Main {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40};
        int sum = 0;
        for (int i = 0; i < numbers.length; i++) {
            sum += numbers[i];
        }
        System.out.println(sum);
    }
}
A100
B10
C40
D90
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Identify the error in array access
What error does this Java code produce when run?
Java
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[3];
        arr[3] = 10;
        System.out.println(arr[3]);
    }
}
ASyntaxError
BNullPointerException
CArrayIndexOutOfBoundsException
DNo error, prints 10
Attempts:
2 left
💻 code output
advanced
2:00remaining
Output after array element swap
What is the output of this Java program?
Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {7, 8, 9};
        int temp = arr[0];
        arr[0] = arr[2];
        arr[2] = temp;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A9 8 7
B7 8 9
C8 7 9
D9 7 8
Attempts:
2 left
🧠 conceptual
expert
2:00remaining
Number of elements after array initialization
How many elements does the array contain after this declaration?
int[] arr = new int[5];
A0
B5
C6
D4
Attempts:
2 left