Answers to Sample Midterm Questions (other answers may be possible in some cases) 1. a 2. b 3. d 4. e 5. e 6. c 7. false 8. true 9. false 10. false 11. true 12. true 13. false 14. true 15. true 16. max >= 0, contents.size() <= max 17. public 18. private 19. public (and possibly final) 20. REQUIRES: If N is the number of vertices passed to the constructor, 0 <= vertex1 < N and 0 <= vertex2 < N. 21. MODIFIES: this 22. EFFECTS: Creates an edge from vertex1 to vertex2 with the given weight, replacing the old weight, if any. 23. REQUIRES: If N is the number of vertices passed to the constructor, 0 <= vertex1 < N and 0 <= vertex2 < N. There is an edge from vertex1 to vertex2, or there is vertex k with an edge from vertex1 to k and an edge from k to vertex2. 24. MODIFIES: nothing 25. EFFECTS: nothing 26. RETURNS: The shortest path length from vertex1 to vertex2 via at most one intermediate vertex. 27. Yes. 28. The indirect method does not declare that an exception is thrown, yet it throws a NoSuchPathException, so that must not be a checked exception. 29. An ArrayIndexOutOfBoundsException would be thrown. 30. Could check the bounds and throw an IllegalArgumentException indicating that there is no such vertex. 31. G(x) = (V,E) where V = { i : edges[i][j] != 0 for some j } E = { (i,j) with weight w : edges[i][j] == w and w != 0 }