0
0
Javaprogramming~15 mins

Array traversal in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of nested array traversal
What is the output of the following Java code that traverses a 2D array?
Java
public class Main {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2}, {3, 4}};
        int sum = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                sum += matrix[i][j];
            }
        }
        System.out.println(sum);
    }
}
A10
B24
C0
DCompilation error
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Output of enhanced for-loop traversal
What will be printed by this Java code that uses an enhanced for-loop to traverse an array?
Java
public class Main {
    public static void main(String[] args) {
        String[] fruits = {"apple", "banana", "cherry"};
        for (String fruit : fruits) {
            System.out.print(fruit.charAt(0));
        }
    }
}
ACompilation error
Bapplebananacherry
Cabc\n
Dabc
Attempts:
2 left
🧠 conceptual
advanced
2:00remaining
Understanding array traversal with break statement
What is the output of this Java code that traverses an array and uses a break statement?
Java
public class Main {
    public static void main(String[] args) {
        int[] numbers = {5, 10, 15, 20};
        int total = 0;
        for (int num : numbers) {
            if (num == 15) {
                break;
            }
            total += num;
        }
        System.out.println(total);
    }
}
A0
B30
C15
D50
Attempts:
2 left
🔧 debug
advanced
2:00remaining
Identify the error in array traversal
What error will this Java code produce when run?
Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
AArrayIndexOutOfBoundsException
BNullPointerException
CCompilation error
DNo output, infinite loop
Attempts:
2 left
🚀 application
expert
3:00remaining
Calculate the product of all even numbers in an array
What is the output of this Java program that multiplies all even numbers in an array?
Java
public class Main {
    public static void main(String[] args) {
        int[] nums = {2, 3, 4, 5, 6};
        int product = 1;
        for (int n : nums) {
            if (n % 2 == 0) {
                product *= n;
            }
        }
        System.out.println(product);
    }
}
A120
B48
C0
D15
Attempts:
2 left