Which of the following best explains why trees cannot model all types of relationships in data?
Think about what happens if a node has more than one parent or if there is a loop.
Trees are hierarchical structures where each node has exactly one parent except the root. They cannot represent cycles or nodes with multiple parents, which are common in many real-world relationships like social networks or road maps. Graphs allow these complex connections.
What is the output of the following Java code that creates a graph with cycles and prints adjacency lists?
import java.util.*; public class GraphExample { public static void main(String[] args) { Map<Integer, List<Integer>> graph = new HashMap<>(); graph.put(1, Arrays.asList(2, 3)); graph.put(2, Arrays.asList(3)); graph.put(3, Arrays.asList(1)); for (int node : graph.keySet()) { System.out.print(node + " -> "); for (int neighbor : graph.get(node)) { System.out.print(neighbor + " "); } System.out.println(); } } }
Look carefully at how neighbors are assigned for each node.
The graph is represented as a map where each node points to a list of neighbors. Node 1 points to 2 and 3, node 2 points to 3, and node 3 points back to 1, creating a cycle. The output prints each node followed by its neighbors.
What error will the following Java code produce when trying to detect a cycle in a graph?
import java.util.*; public class CycleCheck { private Map<Integer, List<Integer>> graph; private Set<Integer> visited = new HashSet<>(); private Set<Integer> recStack = new HashSet<>(); public CycleCheck(Map<Integer, List<Integer>> graph) { this.graph = graph; } public boolean hasCycle(int node) { if (recStack.contains(node)) return true; if (visited.contains(node)) return false; visited.add(node); recStack.add(node); for (int neighbor : graph.get(node)) { if (hasCycle(neighbor)) return true; } recStack.remove(node); return false; } public static void main(String[] args) { Map<Integer, List<Integer>> g = new HashMap<>(); g.put(1, Arrays.asList(2)); g.put(2, Arrays.asList(3)); g.put(3, Arrays.asList(1)); CycleCheck cc = new CycleCheck(g); System.out.println(cc.hasCycle(1)); } }
Check how the recursion and visited sets prevent infinite loops.
The code uses recursion with a recursion stack and visited set to detect cycles. It returns true if a node is found in the recursion stack again, indicating a cycle. The code correctly detects the cycle 1->2->3->1 and prints true without errors.
Which of the following Java code snippets will cause a syntax error when creating a graph adjacency list?
Check the constructors available for ArrayList.
ArrayList does not have a constructor that takes multiple elements directly. Option C tries to call new ArrayList(2, 3), which is invalid syntax. The other options use valid ways to create lists.
You want to model a social network where users can follow multiple other users and cycles can exist (e.g., user A follows B, B follows C, and C follows A). Which data structure is best and why?
Think about whether cycles and multiple parents are allowed in the data.
Social networks have complex relationships with cycles and multiple connections. Trees cannot represent cycles or multiple parents. Graphs allow these connections and are the best choice to model such networks.
