Challenge - 5 Problems
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Array Access and Modification
What is the output of the following Java code that uses an array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {10, 20, 30, 40}; arr[2] = arr[1] + arr[3]; System.out.print(arr[2] + ", " + arr.length); } }
Attempts:
2 left
💡 Hint
Remember array indices start at 0 and length gives total elements.
✗ Incorrect
arr[2] is set to arr[1] + arr[3] which is 20 + 40 = 60. The length of arr is 4.
🧠 Conceptual
intermediateChoosing Arrays for Fixed Size Data
Why is an array a better choice than a linked list when you know the number of elements will not change?
Attempts:
2 left
💡 Hint
Think about how arrays store elements in memory.
✗ Incorrect
Arrays store elements in contiguous memory locations, enabling fast direct access by index. Linked lists require traversal.
❓ Predict Output
advancedArray vs ArrayList Behavior
What is the output of this Java code comparing array and ArrayList?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] arr = new int[3]; arr[0] = 5; arr[1] = 10; arr[2] = 15; ArrayList<Integer> list = new ArrayList<>(); list.add(5); list.add(10); list.add(15); System.out.print(arr.length + ", " + list.size()); } }
Attempts:
2 left
💡 Hint
Check how length and size differ for arrays and ArrayLists.
✗ Incorrect
Array length is fixed and returns 3. ArrayList size returns the number of elements added, which is also 3.
🧠 Conceptual
advancedWhen to Prefer Arrays Over Other Data Structures
Which scenario best justifies choosing an array over a linked list or other data structures?
Attempts:
2 left
💡 Hint
Think about the strengths of arrays in terms of access speed and size.
✗ Incorrect
Arrays provide fast random access and are efficient when size is fixed. Linked lists are better for frequent insertions/deletions.
🔧 Debug
expertIdentify the Error in Array Usage
What error will this Java code produce and why?
DSA Java
public class Main { public static void main(String[] args) { int[] numbers = new int[3]; numbers[0] = 1; numbers[1] = 2; numbers[2] = 3; numbers[3] = 4; System.out.print(numbers[3]); } }
Attempts:
2 left
💡 Hint
Check the array size and valid indices.
✗ Incorrect
The array has size 3 with valid indices 0,1,2. Accessing index 3 causes runtime ArrayIndexOutOfBoundsException.
