0
0
Javaprogramming~15 mins

Common array operations 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 sum calculation
What is the output of this Java code that sums all elements in an integer array?
Java
int[] numbers = {2, 4, 6, 8};
int sum = 0;
for (int num : numbers) {
    sum += num;
}
System.out.println(sum);
A30
B24
CSyntaxError
D20
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Output of array reverse print
What does this Java code print when it prints the array elements in reverse order?
Java
int[] arr = {1, 3, 5, 7};
for (int i = arr.length - 1; i >= 0; i--) {
    System.out.print(arr[i] + " ");
}
A7 5 3 1
B1 3 5 7
C7 5 3
DRuntimeError
Attempts:
2 left
💻 code output
advanced
2:30remaining
Output of array element replacement
What is the output after replacing all negative numbers with zero in this array?
Java
int[] data = {4, -1, 7, -3, 0};
for (int i = 0; i < data.length; i++) {
    if (data[i] < 0) {
        data[i] = 0;
    }
}
for (int num : data) {
    System.out.print(num + " ");
}
ASyntaxError
B4 0 7 0 0
C4 0 7 -3 0
D4 -1 7 -3 0
Attempts:
2 left
💻 code output
advanced
2:30remaining
Output of array copy with System.arraycopy
What is the output of this code that copies part of one array into another?
Java
int[] source = {10, 20, 30, 40, 50};
int[] dest = {1, 2, 3, 4, 5};
System.arraycopy(source, 1, dest, 2, 2);
for (int i : dest) {
    System.out.print(i + " ");
}
A1 2 20 30 5
B10 20 30 40 50
C1 2 10 20 5
DArrayIndexOutOfBoundsException
Attempts:
2 left
🧠 conceptual
expert
2:00remaining
Error type when accessing invalid array index
What error does this Java code produce when trying to access an invalid index in an array?
Java
int[] arr = {5, 10, 15};
System.out.println(arr[5]);
ASyntaxError
BNullPointerException
CArrayIndexOutOfBoundsException
DClassCastException
Attempts:
2 left