CSE12F22Exam1

 CSE12F22Exam1 :-)

1
CSE 12 F22 Exam 1
Do not begin until instructed.
This exam is closed book, closed notes. No study aids or electronic devices are allowed. Turn off your 
phone and have no bags open.
You must hand in all sheets, including scratch paper, at the end of the exam.
This exam is designed to assess your understanding of topics from CSE12. You cannot communicate 
with other students during the exam. You cannot discuss this exam with anyone from now until you 
receive your grade on the exam. This includes posting about the exam on Piazza or elsewhere online.
Please write “I excel with integrity” in the box below:
Sign your name: _______________________________________________
Print your name: _______________________________________________
Print your email: _______________________________________________
PID: _______________________________________________
You have 45 minutes to complete the exam. Work to maximize points. Write all your answers in the 
provided answer sheet on the back of this page. If you get stuck, work through other problems, and come 
back to it.
Once the exam starts, you can rip off/remove the answer sheet.
In general, if you think you've spotted a typo in the exam, do your best to answer in the spirit of the 
question. Keep in mind that some questions have interesting code examples with intentional bugs for you 
to find as part of the question. If you ask a question during the exam, we may decline to answer. In 
general, assume that any necessary libraries (JUnit, ArrayList, List, and so on) have been imported.
Stay calm – you can do this!
Do not begin until instructed.
CSE12F22Exam1 :-)
2
Write all answers below. There are 31 total points
Question 1 2 points each for implementation, 1 point each for true/false questions
A: implementation of maybePush()
B: implementation of pop()
C D
Question 2 (6 points, 1 point each) write (Both, Neither, Three, or Four)
A B C D E F
Question 3 (7 points, 1 each) Write TRUE or FALSE
A B C D E F G
Question 4 (2 points each for implementation, 1 point each for C/D)
A. implementation of prepend()
B. test for replace()
C D
Question 5 (3 points, 1 per correct)
A B C
Question 6 (3 points, 1 per correct)
A B C
CSE12F22Exam1 :-)
3
Reference
Module java.base
Package java.util
Class ArrayList
Type Parameters:
E - the type of elements in this list
All Implemented Interfaces:
Serializable, Cloneable, Iterable, Collection, List, RandomAccess
Module java.base
Package java.util
Interface List
void add(int index, E
element)
Inserts the specified element at the specified position in 
this list.
boolean add(E e) Appends the specified element to the end of this list.
E get(int index) Returns the element at the specified position in this list.
int indexOf(Object o) Returns the index of the first occurrence of the specified 
element in this list, or -1 if this list does not contain the 
element.
E remove(int index) Removes the element at the specified position in this 
list.
boolean remove(Object o) Removes the first occurrence of the specified element 
from this list, if it is present.
int size() Returns the number of elements in this list.
CSE12F22Exam1 :-)
4
Question 1
Consider implementing the interface FixedLengthStack:
interface FixedLengthStack {
 int getMaxElements();
 int size();
 void maybePush(E elt);
 E pop();
}
A FixedLengthStack is like a regular Stack, but it has some fixed number of elements it can hold. If elements are 
pushed (by maybePush) when the maximum number is already stored, maybePush has no effect and no element is 
added. Pop should throw some exception (any exception is allowed) if the stack is empty.
class ALFLStack implements FixedLengthStack {
 ArrayList contents;
 final int maxElements; // the fixed maximum number of elements the stack can hold
 public ALFLStack(int maxElements) {
 this.contents = new ArrayList();
 this.maxElements = maxElements;
 }
 public int size() { return this.contents.size(); }
 public int getMaxElements() { return this.maxElements; }
 public E pop() {
 // make sure to answer on the answer sheet!
 }
 public void maybePush(E elt) {
 // make sure to answer on the answer sheet!
 }
}
A. Provide an implementation of maybePush() that matches the description above (write the method body 
directly in the answer sheet).
B. Provide an implementation of pop() that matches the description above (write the method body directly in 
the answer sheet).
C. True or false: If we change the interface FixedLengthStack to the version below, we will need to edit 
ALFLStack to fix compile errors:
interface FixedLengthStack {
 int getMaxElements();
 int size();
 void maybeEnqueue(Element elt);
 Element dequeue();
}
D. True or false: Changing the type of the contents field from ArrayList to List would cause a compile 
error.
CSE12F22Exam1 :-)
5
Question 2
Consider the following IntegerList interface, implementation, and tests:
1 interface IntegerList {
2 void add(Integer s);
3 int sumAll();
4 }
5 public class IntegerListImplementation implements IntegerList {
6 Integer[] elements;
7 int size;
8 public IntegerListImplementation() {
9 this.elements = new Integer[2];
10 this.size = 0;
11 }
12 public int sumAll() {
13 Integer toReturn = 0;
14 for(int i = 0; i < this.elements.length; i += 1) {
15 toReturn += this.elements[i];
16 }
17 return toReturn;
18 }
19 public void add(Integer item) {
20 expandCapacity();
21 this.elements[this.size] = item;
22 this.size += 1;
23 }
24 private void expandCapacity() {
25 int currentCapacity = this.elements.length;
26 If (this.size < currentCapacity) { return; }
27 Integer[] expanded = new Integer[currentCapacity * 2];
28 for(int i = 0; i < this.elements.length; i += 1) {
29 expanded[i] = this.elements[i];
30 }
31 this.elements = expanded;
32 }
1 public class TestIntegerList {
2 @Test
3 public void testSumThree() {
4 IntegerList iList = new IntegerListImplementation();
5 iList.add(1); iList.add(2); iList.add(3);
6 assertEquals(6, iList.sumAll());
7 }
8 @Test
9 public void testSumFour() {
10 IntegerList iList = new IntegerListImplementation();
11 iList.add(1); iList.add(2); iList.add(3); iList.add(4);
12 assertEquals(10, iList.sumAll());
13 }
14 }
CSE12F22Exam1 :-)
6
You run the tests, and see that testSumFour passes, and testSumThree fails with the following message:
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.elements[i]" is null
Consider each of the following changes independently: each change applied in isolation, without any of the others. 
For each, write Both if after the change both tests would pass, Neither if after the change neither test would pass, 
Three if after the change just testSumThree would pass, and Four if after the change just testSumFour would pass.
A. Change line 9 to this.elements = new Integer[3];
B. Change line 9 to this.elements = new Integer[4];
C. Change line 14 to use i < this.size instead of i < this.elements.length
D. Add this new line after line 29: this.size += 1; //(inside the loop)
E. Change line 27 to Integer[] expanded = new Integer[currentCapacity + 10];
F. Change line 28 to use i < this.size instead of i < this.elements.length
CSE12F22Exam1 :-)
7
Question 3
Consider this class and interface hierarchy:
interface SomeContainer {
 void someMethod(String s);
 int someOtherMethod();
}
class AlphaContainerImpl implements SomeContainer {
 /* questions about this class body below */
}
class BetaContainerImpl implements SomeContainer {
 /* questions about this class body below */
}
Answer True or False for each of the following:
A: Assuming both classes compile, this statement also compiles successfully: 
SomeContainer b = new BetaContainerImpl ();
B: Assuming both classes compile, this statement also compiles successfully: 
SomeContainer a = new AlphaContainerImpl ();
C: Assuming both classes compile, this statement also compiles successfully: 
SomeContainer s = new SomeContainer();
D: Assuming both classes compile, this statement also compiles successfully: 
AlphaContainerImpl a = new SomeContainer();
E: Any field declared in AlphaContainerImpl must be declared in BetaContainerImpl with the same name and type
F: AlphaContainerImpl and BetaContainerImpl must define at least two methods with the same name and 
parameters
G: Any method declared in AlphaContainerImpl must be declared in BetaContainerImpl with the same name and 
parameters
CSE12F22Exam1 :-)
Question 4
Consider these classes and tests:
class Node {
 String s;
 Node next;
 public Node(String s, Node n) { this.s = s; this.next = n; }
}
class SList {
 public final Node front;
 public SList() {
 this.front = new Node(null, null);
 }
 public void prepend(String s) {
 // Write your answer in the answer sheet!
 }
 public void removeLast() {
 Node current = this.front;
 while(current.next.next != null) {
 current = current.next;
 }
 current.next = null;
 }
 public String getLast() {
 Node current = this.front;
 while(current.next != null) {
 current = current.next;
 }
 return current.s;
 }
 public void replace(String old, String s) {
 Node current = this.front;
 while(current.next != null) {
 if (old.equals(current.next.s)) { current.next.s = s; }
 current.next = current.next.next;
 }
 }
}
@Test
public void testSList() {
 SList sl = new SList();
 sl.prepend("a");
 sl.prepend("b");
 sl.prepend("c");
 assertEquals("a", sl.getLast());
 sl.removeLast();
 assertEquals("b", sl.getLast());
 sl.removeLast();
 assertEquals("c", sl.getLast());
}
@Test
public void testReplace() { 
 // Write your answer in the answer sheet!
}
8
CSE12F22Exam1 :-)
9
A. Fill in an implementation of prepend() that passes the given test (and should pass other related 
tests as well). You can assume that prepend can be called when the list is empty as well as when 
there are elements present in the list.
B. The given version of replace() is buggy. Write the body of a test that demonstrates that replace()
is incorrectly implemented in the answer sheet. Do not use null as an input to replace, or an 
element of a list, in your test.
C. Given the implementation of SList with the methods presented, would SList be more appropriate 
for implementing a Stack or a Queue? Write your answer as either Stack or Queue directly in 
the answer sheet.
D. How many total Node objects are created in testSList (including in all the methods it calls) before 
the first use of assertEquals? Write your answer as a number directly in the answer sheet.
CSE12F22Exam1 :-)
10
Question 5
The maze below was solved with a stack worklist/DFS. The asterisks mark the path from the finish back to the start 
by following previous references.
A
* start
*
*
*
finish
B
finish
* * *
*
start *
C
finish *
*
*
*
start *
A-C: In these three cases, various orders have been chosen for adding the available neighbors to the worklist. You 
will answer which order matches each maze solution. For each, choose from the answers below. You may use the 
answers more than once, write the four-letter abbreviation directly in the answer sheet. A reminder that on the page, 
West is left, East is right, North is up, and South is down
● ESNW for East then South then North then West
● NSEW for North then South then East then West
● WSEN for West then South then East then North
● NWSE for North then West then South then East
initialize wl to be a new empty worklist (stack _or_ queue)
add the start square to wl
mark the start as visited
while wl is not empty:
 let current = remove the first element from wl (pop or dequeue)
 if current is the finish square
 return current
 else
 for each neighbor of current that isn't a wall and isn't visited IN SOME ORDER
 mark the neighbor as visited
 set the previous of the neighbor to current
 add the neighbor to the worklist (push or enqueue)
if the loop ended, return null (no path found)
Question 6
For each maze A-C in question 5, could it have been the solution from a queue worklist/BFS for any of the given 
orderings? Write Yes or No directly in the answer sheet for each of A-C.
CSE12F22Exam1 :-)
Scratch Paper
11

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图