0% found this document useful (0 votes)
24 views10 pages

sample CSCI midterm

This document outlines the instructions and content for CSCI 102 Exam 1, including guidelines for taking the exam, problem types, and specific questions related to programming concepts. The exam consists of short answer, multiple choice, and code reading questions, covering topics such as class definitions, stack frames, linked lists, and algorithm analysis. Students are instructed to follow specific protocols during the exam, including not using any external resources and ensuring they have the correct version of the exam.

Uploaded by

rishikagautam13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

sample CSCI midterm

This document outlines the instructions and content for CSCI 102 Exam 1, including guidelines for taking the exam, problem types, and specific questions related to programming concepts. The exam consists of short answer, multiple choice, and code reading questions, covering topics such as class definitions, stack frames, linked lists, and algorithm analysis. Students are instructed to follow specific protocols during the exam, including not using any external resources and ensuring they have the correct version of the exam.

Uploaded by

rishikagautam13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Csci 102: Exam 1 o

Duration: 60 minutes

Name:

NetID:

Student to your left: Student to your right:

DO NOT OPEN THIS EXAM UNTIL INSTRUCTED

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

Problem A (30 points) Short Answer / Fill-in The Blank.


For each question below enter the answers in space indicated by . Your answer should be concise and follow the format indicated in the question. Any
numbers should be written as numerals, not as words.

1. Class Foo is defined as follows:


1 public class Foo {
2 int x;
3 int y;
4
5 public Foo (int x, int y) {
6 this.x = x;
7 this.y = y;
8 }
9
10 public boolean equals(Object obj) {
11 if (obj == null ) return false;
12 if (this == obj ) return true;
13 if (! (obj instanceof Foo ) )
14 return false;
15 Foo other = (Foo) obj;
16 if (this.x == other.x && this.y == other.y)
17 return true;
18 return false;
19 }
20 }

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");

2. Consider the following program


public class StackExample {
public static void main (String[]args){
zoo();
}
public static void zoo(){
land(13);
sea(55);
}
public static void land(int x){
mammals(x);
birds(x);
}
public static void sea(int x){
... / / n o f u n c t i o n c a l l s
}

public static void mammals(int x){


... / / n o f u n c t i o n c a l l s
}
public static void birds(int x){
... / / n o f u n c t i o n c a l l s
}
}

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

content of list afte the above operations:

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;

Enter two numbers as your answers.

p.data = c.data =

5. In the following classes, class B inherits from class A.

class A { class B extends A {


public int num; public A (int n ) {
public A (int n ) { super(n);
num = n; }
} public void update (int n) {
public void update (int n) { num = num * n;
num = num + n; }
} }
}

What is the output, when the following code is executed?


A a1 = new A(4);
A a2 = new B(9);
B b1 = (B)a2;
a1.update(10);
a2.update(10);
b1.update(10);
System.out.print(a1.num + " " + a2.num + " " + b1.num );

Enter the output below:

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

Problem B (15 points) Multiple Choice/Select.


Fill in the box next to each correct answer. For some questions there may be more than one correct answer. (For this question to be properly graded, you need to fill in
the box, not put a checkmark next to it or write ’true’ somewhere next to the question.)

1. Consider the following algorithm }


create an empty queue for ( ___________________________________) {
queue.enqueue(0) words[index+1] = words[index];
queue.enqueue(1) }
for (int i=0; i< n; i++) words[pos] = word;
a = queue.dequeue() size++;
b = queue.dequeue() }
queue.enqueue(b)
queue.enqueue(a + b)
print a
□ int index = pos; index < size; index++

What is printed when n is equal to 4?


□ int index = pos; index <= size; index++

□ int index = pos-1; index < size; index++


□ 0123 □ 01123
□ int index = size-1; index > pos; index--
□ 0112 □ 12358
□ int index = size-1; index >= pos; index--
□ 1235 □ none of the above
□ int index = size; index >= pos; index--
□ 01234
4. Consider the following function that searches in an ArrayList of Square
objects. Assume that Square class is defined and that it properly overrides
2. Assume that initial capacity of an array in an array-based implementation of equals() method.
a list is 5. Furthermore, assume that add(String s) adds s to the end of
the list, and remove() removes the last element in the list. Finally, when 1 boolean find( ArrayList<Square> list, Square sq) {
the size of the list reaches the capacity, the array’s size is doubled (this hap- 2 for (int i = 0; i < list.size(); i++) {
pens when a new element is supposed to be added but there is no room for it). 3 if (sq.equals(list.get(i))) {
What is the capacity of the array after we execute a sequence of operations 4 return true;
as follows: 5 }
6 }
add("hello"); □ 5 7 return false;
remove(); 8}
add("hello"); □ 6
remove(); What will happen, when the above function is called with list equal to
add("hello"); □ 10 null and sq not equal to null?
remove(); □ 11
add("hello"); □ the function would throw an exception on line 2
remove(); □ 20
add("hello"); □ the function would throw an exception on line 3
remove(); □ the actual capacity cannot be
□ the function would not compile
add("spring"); determined
remove(); □ the function would return false
add("spring");
add("spring"); □ the function would return true
add("spring");
add("spring");
add("spring"); 5. Which of the following operations are not O(1) (constant time operations)
for an array of numerical values sorted from smallest to largest. You may as-
sume that there are no duplicates. You may assume that the number of actual
3. You are working on an array based list implementation. The class stores values in the array is known and that the smallest value is always at index
String elements in an array called words with initial capacity of 100. 0. Remember that the values in an array are stored in consecutive locations
The class uses size to keep track of the actual number of words stored in starting at index zero and empty locations (if any) are at high indexes towards
the list. The method growArray() is used to allocate a larger array and the back of the array.
copy all the data from old to new array when size reaches capacity
Select all the answers that could be used to complete the add() method □ find the largest value
below. The method should add a new word at a specified position. □ find the second smallest value
public void add (int pos, String word ) {
if (pos < 0 || pos > size ) □ delete smallest element
throw new IndexOutOfBoundsException( □ calculate the average/mean value
"invalid position");
if (word == null ) □ calculate the median value
throw new IllegalArgumentException(
"word should not by null"); □ delete largest element
if (size == capacity ) {
growArray();
□ determine if a particular value is in the array or not

Page 4
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu

Problem C (30 points) Code Reading and Understanding.


Answer the questions in this part based on the implementation of a linked list given at the end of this exam. Assume that all methods are correctly implemented (even the
ones for which you need to complete yourself), except for the size method which has an intentional bug.

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

(the second line of output is intentionally blank)

□ 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 "

for (int i = 0; i < 5; i++) { □ none of the above


listA.add(i);
listB.addFront(5-i); 5. The equals method is missing one of the conditions. What should this con-
} dition be so that the method functions properly? You cannot alter the rest of
listB.removeBack(); the method. You should not use the size method since it has a bug.
listB.addFront(0);

What is returned by the call listA.equals(listB) after the above code


fragment is executed?

□ 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.

□ an exception will be thrown while executing the above code fragment

□ an exception will be thrown while executing the equals method

Page 5
This page intentionally blank.
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu

SCRAP PAPER NAME


(Enter your name only if you need to hand this page in with the rest of the exam.)

Page 7
CSCI-UA 102 ©2024, Joanna Klukowska, All Rights Reserved
Exam 1 o joannakl@cs.nyu.edu

SCRAP PAPER NAME


(Enter your name only if you need to hand this page in with the rest of the exam.)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy