sample CSCI midterm
sample CSCI midterm
Duration: 60 minutes
Name:
NetID:
Instructions:
• Make sure that the students sitting next to you have different icons printed next to the Exam 1 above. These indicate different versions of the exam.
• Write your full name and your NetID on the front of this exam.
• Make sure that your exam is not missing any sheets. There should be six (5) double sided sheets, or 10 pages, in the exam (including this cover page and scrap
paper pages at the end).
• Write your answers in the space provided below each problem. If you make a mess, clearly indicate your final answer.
• If you have any questions during the exam: 1) reread the question to make sure you are not missing any part of the instructions, 2) check any corrections and
clarifications listed on the projector, 3) walk to the end of your row and walk down to the front of the room to ask your question.
• At the end of the exam, there is a page with the code and scap paper. You can detach these pages from the rest of the exam. You do not need to return these pages
with your completed exam. You have to return pages 1 through 7.
• This exam is closed books, closed notes, closed computers, closed phones (or really anything other than your own brain power).
• You need to stay in your seat until the exam is finished. You should not leave the room even if you finish the exam. This distracts other students who are still
working.
Good luck!
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
What is the line number of the return statement that is executed when the following comparison runs?
Foo f = new Foo (5, 10 );
f.equals("foobar");
What is the largest number of stack frames on the function stack at any moomemt in time when the above program executes? Make sure to count all the stack
frames including the one for main function. Enter your answer below.
Page 2
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
3. head is a reference to the first node in the list. Start with the following list:
----- ----- ----- ------
head--->| 5 | -->| 3 | -->| 7 | -->| 12 | --> null
----- ----- ----- ------
What are the values stored in the nodes (in order from beginning to the end) of the list immediately after the following lines of code have been executed?
Node p = head.next;
Node n = new Node(4);
n.next = p.next;
p.next = n;
Enter a comma separated list of numbers in order that they appear in the list after the operations are performed. For example, the original list would be entered as
5, 3, 7, 12
4. head is a reference to the first node in the list. Start with the linked list given below:
----- ----- ----- ------
head--->| 5 | -->| 3 | -->| 7 | -->| 12 | --> null
----- ----- ----- ------
What are the values of p.data and c.data (assuming that data is the data field in the node that stores the number) immediately after the following lines of
code have been executed?
Node p = head.next;
Node c = p.next;
p.next = c.next;
p.data = c.data =
6. Assume that an array starts at the memory location 100. The array stores elements of type int. What is the memory address of the element at index 5?
Page 3
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
Page 4
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
1. The size method in that implementation has a bug, but it compiles. De- 3. Consider the following sequence of instructions
termine what the output of the following code fragment is when using this
buggy method. LinkedList list = new LinkedList();
try {
LinkedList list = new LinkedList(); list.add(35);
list.add(3); list.add(8);
list.add(4); list.add(17);
list.add(5); list.add(3);
System.out.println(list.size() ); while( list.swapLastTwo() ) {
System.out.println(list.toString() ); list.removeBack();
}
□ 3 System.out.println(list.toString() );
3 4 5 }
catch (NullPointerException ex ) {
System.out.println( "exception");
□ 3 }
5 4 3
What is the output produced by the above code fragment?
□ 3
□ 3
3 4. Consider the following sequence of instructions
LinkedList list = new LinkedList();
list.add(3);
□ 3
list.add(7);
5
list.addFront(1);
list.removeBack();
□ some other output will be produced list.add(4);
list.toString();
□ nothing because the code has an infinite loop in the size method
Which string is returned by the call to toString() on the last line of the
□ nothing because the code will crash when executing the size method above code fragment?
□ nothing because the code will crash when executing the toString
□ "4 3 1 "
method
□ "4 1 7 "
□ "4 7 3 1 "
2. Consider the following sequence of instructions
□ "1 3 4 "
LinkedList listA = new LinkedList();
LinkedList listB = new LinkedList(); □ "1 3 7 4 "
□ true 6. The removeBack method is missing one of the conditions. What should
this condition be so that the method functions properly? You cannot alter the
□ false rest of the method. You should not use the size method since it has a bug.
Page 5
This page intentionally blank.
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
Page 7
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
Page 8
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
1 /*
2 A linked list of Integer objects . Does not allow null elements .
3 */
4 public class LinkedList {
5 private Node head;
6 private Node tail;
7 // define internal Node class
8
9 private class Node {
10 public Integer data;
11 public Node next;
12
13 public Node (Integer data) {
14 this.data = data;
15 this.next = null;
16 }
17 }
18 /* Adds an element ’ it ’ to the end of this list .
19 * @param it the element to be added to this list
20 * @throws NullPointerException if it == null
21 */
22 public void add (Integer it) throws NullPointerException {
23 if (it == null ){
24 throw new NullPointerException("it should not be null");
25 }
26 Node n = new Node(it);
27 // add the very first value
28 if ( head == null ) {
29 head = n;
30 tail = n;
31 }
32 else { / / a d d t o t h e e n d
33 tail.next = n;
34 tail = tail.next;
35 }
36 }
37 /* Adds an element ’ it ’ to the beginning of this list .
38 * @param it the element to be added to this list
39 * @throws NullPointerException if it == null
40 */
41 public void addFront (Integer it) throws NullPointerException {
42 // i m p l e m e n t a t i o n not shown but should be assumed to be correct
43 }
44 /* Removes the last node in this list and returns the value that
45 * was stored in it .
46 * @return value that was stored in the last node
47 * or null if this list is empty
48 */
49 public Integer removeBack () {
50 if (head == null ) / / l i s t i s e m p t y
51 return null;
52 if (head.next == null ) { / / l i s t w i t h one element
53 Integer tmp = head.data;
54 head = null;
55 tail = null;
56 return tmp;
57 }
58 Node cur = head;
59
60 while ( _______________________){
61 cur = cur.next;
62 }
63 Integer tmp = cur.data;
64 tail = cur;
65 tail.next = null;
66 return tmp;
67
68 }
69
70 // CODE CONTINUES ON THE NEXT PAGE
Page 9
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu
71
72
73
74
75
76 /* BUGGY METHOD
77 * Computes and returns the number of elements in this list .
78 * @return the number of elements in this list
79 */
80 public int size () {
81 int counter = 0;
82 while (head != null ) {
83 head = head.next;
84 counter++;
85 }
86 return counter;
87 }
88 /* Computes a string representation of this list .
89 * @return the string containing all values separated by spaces ,
90 * or an empty string if the list is empty
91 */
92 public String toString () {
93 String str = "";
94 Node current = head;
95 while (current != null ) {
96 str = str + " " + current.data;
97 current = current.next();
98 }
99 return str;
100 }
101
102 /* Determines if this list is equal to list o. Two lists
103 * are equal if they are both the same length and contain
104 * the same elements in the same order .
105 * @param object to be compared to this list
106 * @return true if the lists are equal , false otherwise
107 */
108 public boolean equals (Object o ) {
109 if (o == null) return false;
110 if (o == this ) return true;
111 if (! (o instanceof LinkedList ) ) return false;
112 LinkedList other = (LinkedList) o;
113
114 Node cur1 = this.head;
115 Node cur2 = other.head;
116 while (cur1 != null && cur2 != null ) {
117 if ( !cur1.data.equals(cur2.data))
118 return false;
119 cur1 = cur1.next;
120 cur2 = cur2.next;
121 }
122 if (__________ ??? _____________ ) return true;
123 return false;
124 }
125 /* Exchange the values of the last two elements in the list .
126 * For example , if the list is
127 * head -> 35 -> 15 -> 10 -> 2 -> null
128 * and swapLastTwo is called , the list should become
129 * head -> 35 -> 15 -> 2 -> 10 -> null
130 * @return false , if the list has fewer than 2 elements ,
131 * true otherwise
132 */
133 public boolean swapLastTwo () {
134
135 // TODO : implement this method
136
137 }
138
139 }
Page 10