Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a 2D adjacency matrix for 4 vertices.
DSA Java
int[][] adjacencyMatrix = new int[[1]][[1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong size for the matrix like 5 or 3.
Confusing the number of vertices with edges.
✗ Incorrect
The adjacency matrix for 4 vertices is a 4x4 2D array.
2fill in blank
mediumComplete the code to add an edge from vertex 1 to vertex 3 in the adjacency matrix.
DSA Java
adjacencyMatrix[[1]][3] = 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for vertex 1.
Using 3 instead of 1 for the row index.
✗ Incorrect
Vertices are zero-indexed, so vertex 1 corresponds to index 1.
3fill in blank
hardFix the error in the code to correctly check if there is an edge from vertex 2 to vertex 0.
DSA Java
if (adjacencyMatrix[[1]][0] == 1) { System.out.println("Edge exists"); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or 0 instead of 2 for the source vertex index.
Confusing row and column indices.
✗ Incorrect
To check edge from vertex 2 to 0, use row index 2 and column 0.
4fill in blank
hardFill both blanks to correctly initialize an adjacency matrix for 5 vertices and set no edges initially.
DSA Java
int[][] adjacencyMatrix = new int[[1]][[2]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using different sizes for rows and columns.
Using sizes less or more than the number of vertices.
✗ Incorrect
The adjacency matrix for 5 vertices is a 5x5 matrix initialized with zeros.
5fill in blank
hardFill all three blanks to print all edges from vertex 0 in the adjacency matrix.
DSA Java
for (int [1] = 0; [1] < adjacencyMatrix[0].length; [1]++) { if (adjacencyMatrix[0][[2]] == [3]) { System.out.println("Edge from 0 to " + [2]); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and index.
Checking for 0 instead of 1 to find edges.
✗ Incorrect
Use 'i' as loop variable, check adjacencyMatrix[0][i] == 1 to find edges.
