









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










UC Berkeley – Computer Science CS61BL: Data Structures Midterm 1, Summer 2017 Optional. Mark along the line to show your feelings Before exam: [L____________________J]. on the spectrum between L and J. After exam: [L____________________J]. This test has 8 questions worth a total of 30 points, and is to be completed in 110 minutes. The exam is closed book, except that you are allowed to use one double-sided page of notes as a cheat sheet (front and back). No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided. Write the statement out below in the blank provided and sign. You may do this before the exam begins. Any plagiarism, no matter how minor, will result in points deducted from your exam. “I have neither given nor received any assistance during the taking of this exam.” I have neither given nor received any assistance during the taking of this exam. _______________________________________________________________________________________
Write your name and student ID on the front page. Write the names of your neighbors. Write and sign the above statement. Once the exam has started, write your class ID in the corner of every page. Name: Sora Your Class ID: 9001 SID: 1 Name of person to left: Donald Duck TA: Mickey Mouse Name of person to right: Goofy Tips:
Class ID: _______
Write the full output of attempting to compile and run the following programs. In the case of compiler errors, write “ Compiler Error ”. In the case of runtime exceptions, you should write the output of lines that execute before the exception occurs in addition to “ Runtime Exception ”. Each main method is run independently of the others. Code Output public class Palette { String color; public Palette blackOut() { color = "black"; return this; } public static void main(String[] args) { Palette iu = new Palette(); iu.color = "red"; Palette gd = iu.blackOut(); System.out.println(gd.color); gd.color = "peach"; System.out.println(gd.color); System.out.println(iu.color); } } black peach peach When you run gd = iu.blackout(), gd’s pointer will be reassigned to the iu object, so when you print gd’s color, it should be the same as iu’s color since they’re the same color public class Mym { String xaler; public Mym(String s) { this.xaler = s; } public static void eterize(Object[] arr) { for (int i = 0; i < arr.length; i++) { Mym lay = (Mym) arr[i]; System.out.println(lay.xaler); } } public static void main(String[] args) { Object[] arr = {new Mym("cs61bl"), new Mym("rox"), "covfefe"}; eterize(arr); } } cs61bl rox Runtime Exception Covfefe is just a string which is not a Mym object. You cast it to a Mym object, so the compiler will trust you. But, you’ll be caught doing an illegal cast at runtime. The compiler will trust the cast even though it’s illegal, so this code compiles. Only at runtime will the error be caught
Class ID: _______
Consider the following implementation of IntList: public class IntList { public int head; public IntList tail; public IntList(int head, IntList tail) { this.head = head; this.tail = tail; } public static void swapTails(IntList first, IntList second) { IntList temp = first.tail; first.tail = second.tail; second.tail = temp; } public static void swapHeads(int x, int y) { int temp = x; x = y; y = temp; } } a. The following code is executed in order. There are no compiler or runtime errors. For each print statement, fill in the bubble completely corresponding to the integer that's printed. You will be asked to draw two box- and-pointer diagrams on the next page. Statement(s) 1 2 3 4 5 6 IntList L = new IntList(1, new IntList(2, new IntList(3, new IntList(4, null)))); System.out.println(L.tail.tail.head);
IntList M = L.tail.tail; System.out.println(M.tail.head); // Draw the state of the program on next page
M.tail.tail = new IntList(5, null); IntList.swapTails(L, M); System.out.println(M.tail.head);
M.tail.tail.head = 6; IntList.swapHeads(M.head, L.head); System.out.println(M.head);
Class ID: _______ b. Now, fill in the box-and-pointer diagram below to represent the state of the program after the execution of the 2nd^ box above. Not all boxes may be needed. c. Now, fill in the box-and-pointer diagram below to represent the state of the program after the execution of the final line above. Not all boxes may be needed. Common error was having L point to ( 6 → 4 → 5 → null) and M point to ( 1 → 2 → null). The 6 and 1 should be swapped
Class ID: _______
a. Fill in the square next to the lines of code that can never cause a runtime error (not including system errors such as StackOverflowError, OutOfMemoryError, etc.). You may assume that all code compiles and method bodies do not contain code that would cause a runtime error. □ if (xianth.blurg == 10) ■ Zeeg zarg = new Zeeg(); ■ while (yebi != null && yebi.mianate()) □ int zel = da[10]; □ Dylth id = (Dylth) xeonite; ■ Charp vir = gion; b. For each of the following propositions, fill in true or false completely. If the proposition is false, provide a counterexample 𝑓 𝑛 and 𝑔(𝑛). Assume 𝑓 𝑛 and 𝑔(𝑛) are positive, strictly increasing functions. ● True / ○ False: If 𝑓 𝑛 ∈ Ω 𝑔 𝑛 , then 𝑔(𝑛) must be in 𝑂(𝑓 𝑛 ) 𝑓 𝑛 : ____________ 𝑔 𝑛 : ____________ ○ True / ● False: If 𝑓 𝑛 ∈ 𝑂(𝑔 𝑛 ), then 𝑔(𝑛) cannot be in 𝑂(𝑓 𝑛 ) 𝑓 𝑛 : 1 𝑔 𝑛 : 1 ○ True / ● False: If 𝑓 𝑛 ∈ 𝑂( 2!^!^ ) and 𝑓 𝑛 ∈ Ω( 1 ), then 𝑔(𝑛) must be in Θ( 1 ) 𝑓 𝑛 : 1 𝑔 𝑛 : 𝑛 ● True / ○ False: If 𝑓 𝑛 ∈ 𝑂(𝑔 𝑛 ) and 𝑔 𝑛 ∈ 𝑂(ℎ 𝑛 ), then 𝑓 𝑛 must be in 𝑂(ℎ 𝑛 ) 𝑓 𝑛 : ____________ 𝑔 𝑛 : ____________ ● True / ○ False: If 𝑓 𝑛 ∈ 𝑂(𝑔 𝑛 ), then lg 𝑓(𝑛) must be in 𝑂(lg 𝑔(𝑛)) 𝑓 𝑛 : ____________ 𝑔 𝑛 : ____________ ○ True / ● False: If lg 𝑓(𝑛) ∈ 𝑂(lg 𝑔(𝑛)), then 𝑓 𝑛 must be in 𝑂(𝑔(𝑛)) 𝑓 𝑛 : 𝑛!^ 𝑔 𝑛 : 𝑛
Class ID: _______
a. Consider the SLList class, which represents a singly-linked list. A heavily abridged version of this class appears below: public class SLList { ... /* Construct an empty SLList. / public SLList() { ... } / Adds x to the front of the list. / public void insertFront(int x) { ... } / Returns the index of x in the list, if it exists. Otherwise, returns - 1. */ public int indexOf(int x) { ... } } "Well that's dumb," you observe after reading indexOf's comment, "-1 isn't a real index. That method should produce an error in that case instead." You decide to write the SLListVista class, which must have all of the functionality of SLList, except SLListVista's indexOf method produces a NoSuchElementException in the event that x isn't present in the list. NoSuchElementException is an unchecked exception. In the space provided, fill in the SLListVista class. You may not need all lines. Each line should contain only one statement. import java.util.NoSuchElementException; public class SLListVista extends SLList { @Override public int indexOf(int x) { int index = super.indexOf(x); if (index == - 1) { throw new NoSuchElementException(); } return index; } } Common errors:
Class ID: _______
Complete the expand method in the DLList class, which mutates a circular doubly-linked list with a sentinel node and expands it such that it passes the JUnit tests below. When expand is called on the list (1 3 5), the list is mutated to (1 3 3 3 5 5 5 5 5). That is, the original value of list.get(0) gets repeated list.get(0) times in the output list. This is then followed by the original value of list.get(1) repeated list.get(1) times, et cetera. Your solution should modify the DLList instance that it is called on without constructing any new DLLists. Assume all input list elements are larger than 0. For full credit, the DLList must be well formed (all pointers are correct). Each line should contain only one statement. import static org.junit.Assert.*; import org.junit.Test; public class DLList { private DLNode sentinel; public DLList() { ... } private class DLNode { private int item; private DLNode prev, next; public DLNode(int i, DLNode p, DLNode n) { item = i; prev = p; next = n; } } @Override public boolean equals(Object o) { ... } public static DLList list(int... args) { ... } public int get(int index) { ... } @Test public void testExpand() { DLList d = DLList.list(1, 2, 3); d.expand(); assertEquals(DLList.list(1, 2, 2, 3, 3, 3), d); DLList d2 = DLList.list(2, 1); d2.expand(); assertEquals(DLList.list(2, 2, 1), d2); d 2 .expand(); assertEquals(DLList.list(2, 2, 2, 2, 1), d2); }
Class ID: _______ public void expand() { DLNode curr = sentinel.next; while (curr != sentinel) { DLNode next = curr.next; for (int i = 0; i < curr.item - 1 ; i++) { DLNode newNode = new DLNode(curr.item, curr, next); curr.next = newNode; next.prev = newNode; curr = newNode; } curr = next; } } }
Newsflash: Wild Jarmigon Spotted
Class ID: _______
Gitlit is a version control system that records successive versions of a String. Newly constructed Gitlits have a default initial backup: "Initial version." Fill in the Gitlit class so that the JUnit tests on the next page pass. For full credit, your Gitlit solution must behave according to its comments. You may not need all lines. Each line should contain only one statement. import static org.junit.Assert.; import org.junit.Test; public class Gitlit { public Commit head = new InitialCommit(); public static final String ERR_MSG = "The requested commit doesn't exist."; /* Add a backup to Gitlit. / public void recordBackup(String backup) { head = new Commit(backup, head); } /* Get the backup i backups ago. If i is zero, this method returns the most recent backup. If i is one, this method returns the second most recent backup, etc. If i is invalid (e.g. if it's < 0 or if it's >= the number of backups), returns Gitlit.ERR_MSG. */ public String getBackup(int i) { if (i < 0) return Gitlit.ERR_MSG; return head.getBackup(i); } public class Commit { public String backup; public Commit tail; public Commit(String backup, Commit tail) { this.backup = backup; this.tail = tail; } public String getBackup(int i) { if (i == 0) { return backup; } else { return tail.getBackup(i - 1); } } } ... (continued on next page)
Class ID: _______ public class InitialCommit extends Commit { public InitialCommit () { super("Initial version.", null); } @Override public String getBackup(int i) { if (i == 0) { return backup; } else { return Gitlit.ERR_MSG; } } } @Test public void testGetBackup() { Gitlit g = new Gitlit(); assertEquals("Initial version.", g.getBackup(0)); g.recordBackup("Update 1."); g.recordBackup("Update 2."); assertEquals("Update 1.", g.getBackup(1)); assertEquals("Initial version.", g.getBackup(2)); assertEquals(Gitlit.ERR_MSG, g.getBackup(3)); assertEquals(Gitlit.ERR_MSG, g.getBackup(4)); } } Common Syntax Errors