The array has elements: arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.
Step 2: Access element at index 2
arr[2] is 30, so printing arr[2] outputs 30.
Final Answer:
30 -> Option C
Quick Check:
arr[2] = 30 [OK]
Hint: Remember array indexing starts at 0 in Java [OK]
Common Mistakes:
Confusing index 2 with value 20 at index 1
Expecting output 40 for index 2
Thinking array index starts at 1
4. Identify the error in the following Java code snippet that tries to add an element to an array:
int[] arr = new int[3];
arr[3] = 10;
medium
A. Syntax error in array declaration.
B. Array size is too small; index 3 is out of bounds.
C. Cannot assign value to array elements after declaration.
D. Missing initialization of array elements.
Solution
Step 1: Check array size and valid indices
Array of size 3 has valid indices 0, 1, and 2 only.
Step 2: Analyze assignment at index 3
Assigning arr[3] = 10 tries to access index 3, which is outside the array bounds, causing an error.
Final Answer:
Array size is too small; index 3 is out of bounds. -> Option B
Quick Check:
Index 3 invalid for size 3 array [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
Trying to assign outside array bounds
Confusing array size with last index
Assuming arrays auto-expand on assignment
5. You need to store a fixed number of temperature readings for a week and access them quickly by day index. Which data structure should you choose and why?
hard
A. Array, because it provides fast indexed access and fixed size fits the data.
B. Linked list, because it allows easy insertion and deletion.
C. HashSet, because it prevents duplicate temperature values.
D. ArrayList, because it automatically resizes when needed.
Solution
Step 1: Analyze data requirements
Temperature readings are fixed in number (7 days) and need fast access by day index.
Step 2: Match data structure features
Arrays have fixed size and provide fast access by index, perfect for fixed-size data.
Step 3: Evaluate other options
Linked lists are slower for indexed access, HashSet removes duplicates which is not needed, ArrayList resizes but resizing is unnecessary here.
Final Answer:
Array, because it provides fast indexed access and fixed size fits the data. -> Option A
Quick Check:
Fixed size + fast index access = Array [OK]
Hint: Fixed size + fast access = Use array [OK]
Common Mistakes:
Choosing linked list for fixed size data
Using HashSet when duplicates are allowed
Picking ArrayList when resizing is not needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.