Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an array of integers with size 5.
DSA Java
int[] numbers = new int[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as size which creates an empty array.
Using 1 or 10 which does not match the required size.
✗ Incorrect
The array size is set to 5, so it can hold 5 integers indexed from 0 to 4.
2fill in blank
mediumComplete the code to add a new node with value 10 at the start of a linked list.
DSA Java
Node newNode = new Node([1]);
newNode.next = head;
head = newNode;Attempts:
3 left
💡 Hint
Common Mistakes
Using null or head as the value instead of 10.
Confusing the value with the next pointer.
✗ Incorrect
We create a new node with value 10 and link it to the current head, then update head to newNode.
3fill in blank
hardFix the error in the code to access the third element in an array named arr.
DSA Java
int thirdElement = arr[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 which is the fourth element.
Using index 1 which is the second element.
✗ Incorrect
Array indices start at 0, so the third element is at index 2.
4fill in blank
hardFill both blanks to iterate over a linked list and print each node's value.
DSA Java
Node current = head; while (current [1] null) { System.out.println(current.[2]); current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the loop condition.
Printing current.next instead of current.value.
✗ Incorrect
We loop while current is not null and print the current node's value.
5fill in blank
hardFill all three blanks to create a list of squares for numbers 1 to 5 using an array.
DSA Java
int[] squares = new int[5]; for (int i = 0; i [1] 5; i[2]) { squares[i] = (i [3] 1) * (i [3] 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i <= 5 which causes index out of bounds.
Using i-- which decreases i and causes infinite loop.
Using i - 1 instead of i + 1 for correct squares.
✗ Incorrect
The loop runs while i < 5, increments i by 1 each time, and squares (i + 1).
