EXAMINATION INSTRUCTIONS

advertisement
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
EXAMINATIONINSTRUCTIONS
NAME:___________________________
PERSONNUMBER:___________________________
SIGNATURE:___________________________
Thisexaminationhas6pages.Checkthatyouhaveacompletepaper.
Eachcandidateshouldbepreparedtoproduce,uponrequest,hisorherSUNY/UBcard.
Thisexaminationhas5questions.Answerallquestions.
Youhave60minutestocompletethisexamination.Useyourtimeaccordingly.
READANDOBSERVETHEFOLLOWINGRULES:
► Namesarepre-printedontheexambooklets.EnsurethatyouhaveYOURexam.
► Sign,usingyourusualsignature,inthespaceprovidedonthebackcover.
► Allofyourwritingmustbehandedin.Thisbookletmustnotbetornormutilatedinanyway,
andmustnotbetakenfromtheexaminationroom.
► Showallofyourworkinarrivingatananswer,unlessinstructedotherwise.Partialcreditwillbe
awardedasappropriate.
► Candidates are not permitted to ask questions of the invigilators, except in cases of supposed
errorsorambiguitiesinexaminationquestions.
► CAUTION – Candidates guilty of any of the following, or similar, dishonest practices shall be
immediatelydismissedfromtheexaminationandshallbeliabletodisciplinaryaction.
♦ Making use of any books, papers or memoranda, calculators or computers, audio or
visual cassette players, or other memory aid devices, other than those explicitly
authorisedbytheexaminers.
♦ Speakingorcommunicatingwithothercandidates.
♦ Purposelyexposingwrittenpaperstotheviewofothercandidates.Thepleaofaccident
orforgetfulnessshallnotbereceived.
---------------------------DONOTWRITEBELOWTHISLINE!---------------------------
Q1
/10
Q2
/10
Q3
/10
Q4
Q5
/10
/10
TOTAL
/50
%
/100
1
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question1[10pointstotal:2pointseach]
Inthecodeontheright,clearlycircleandidentifybynumberone(andonlyone)exampleofeachof
thefollowing.Beaspreciseasyoucaninyouridentification(ifyoucircletoomuchortoolittleyou
willnotgetcredit)!Ifyoubelievenoexampleexistsinthegivencode,write“noexample"nextto
thatitem.
1. expression of type int
package tests.multiset;
2. argument list
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import multiset.MultiSet;
3. Java annotation
public class ConstructionTests {
@Test
public void test1() {
MultiSet<String> ms = new MultiSet<String>();
int expected = 0;
int actual = ms.size();
assertTrue("...", expected == actual);
}
4. local variable declaration
5. name of a reference type
@Test
public void test2() {
MultiSet<String> ms = new MultiSet<String>();
String s = "Fred";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test3() {
MultiSet<String> ms = new MultiSet<String>();
String s = "";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test4() {
MultiSet<String> ms = new MultiSet<String>();
String s = null;
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
}
2
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question1[10pointstotal:2pointseach]
Inthecodeontheright,clearlycircleandidentifybynumberone(andonlyone)exampleofeachof
thefollowing.Beaspreciseasyoucaninyouridentification(ifyoucircletoomuchortoolittleyou
willnotgetcredit)!Ifyoubelievenoexampleexistsinthegivencode,write“noexample"nextto
thatitem.
1. expression of type int
package tests.multiset;
2. argument list
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import multiset.MultiSet;
3. Java annotation
public class ConstructionTests {
@Test
public void test1() {
MultiSet<String> ms = new MultiSet<String>();
int expected = 0;
int actual = ms.size();
assertTrue("...", expected == actual);
}
4. local variable declaration
5. name of a reference type
@Test
public void test2() {
MultiSet<String> ms = new MultiSet<String>();
String s = "Fred";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test3() {
MultiSet<String> ms = new MultiSet<String>();
String s = "";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test4() {
MultiSet<String> ms = new MultiSet<String>();
String s = null;
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
}
2
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question1[10pointstotal:2pointseach]
Inthecodeontheright,clearlycircleandidentifybynumberone(andonlyone)exampleofeachof
thefollowing.Beaspreciseasyoucaninyouridentification(ifyoucircletoomuchortoolittleyou
willnotgetcredit)!Ifyoubelievenoexampleexistsinthegivencode,write“noexample"nextto
thatitem.
1. expression of type int
package tests.multiset;
2. argument list
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import multiset.MultiSet;
3. Java annotation
public class ConstructionTests {
@Test
public void test1() {
MultiSet<String> ms = new MultiSet<String>();
int expected = 0;
int actual = ms.size();
assertTrue("...", expected == actual);
}
4. local variable declaration
5. name of a reference type
@Test
public void test2() {
MultiSet<String> ms = new MultiSet<String>();
String s = "Fred";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test3() {
MultiSet<String> ms = new MultiSet<String>();
String s = "";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test4() {
MultiSet<String> ms = new MultiSet<String>();
String s = null;
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
}
2
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question1[10pointstotal:2pointseach]
Inthecodeontheright,clearlycircleandidentifybynumberone(andonlyone)exampleofeachof
thefollowing.Beaspreciseasyoucaninyouridentification(ifyoucircletoomuchortoolittleyou
willnotgetcredit)!Ifyoubelievenoexampleexistsinthegivencode,write“noexample"nextto
thatitem.
1. expression of type int
package tests.multiset;
2. argument list
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import multiset.MultiSet;
3. Java annotation
public class ConstructionTests {
@Test
public void test1() {
MultiSet<String> ms = new MultiSet<String>();
int expected = 0;
int actual = ms.size();
assertTrue("...", expected == actual);
}
4. local variable declaration
5. name of a reference type
@Test
public void test2() {
MultiSet<String> ms = new MultiSet<String>();
String s = "Fred";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test3() {
MultiSet<String> ms = new MultiSet<String>();
String s = "";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test4() {
MultiSet<String> ms = new MultiSet<String>();
String s = null;
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
}
2
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question1[10pointstotal:2pointseach]
Inthecodeontheright,clearlycircleandidentifybynumberone(andonlyone)exampleofeachof
thefollowing.Beaspreciseasyoucaninyouridentification(ifyoucircletoomuchortoolittleyou
willnotgetcredit)!Ifyoubelievenoexampleexistsinthegivencode,write“noexample"nextto
thatitem.
1. expression of type int
package tests.multiset;
2. argument list
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import multiset.MultiSet;
3. Java annotation
public class ConstructionTests {
@Test
public void test1() {
MultiSet<String> ms = new MultiSet<String>();
int expected = 0;
int actual = ms.size();
assertTrue("...", expected == actual);
}
4. local variable declaration
5. name of a reference type
@Test
public void test2() {
MultiSet<String> ms = new MultiSet<String>();
String s = "Fred";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test3() {
MultiSet<String> ms = new MultiSet<String>();
String s = "";
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
@Test
public void test4() {
MultiSet<String> ms = new MultiSet<String>();
String s = null;
boolean expected = false;
boolean actual = ms.contains(s);
assertTrue("...", expected == actual);
}
}
2
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question2[10points:10/7/3/0]
[QUESTION2andQUESTION3arerelated.READEACHFULLYBEFOREANSWERING!]
Defineamethodnamedten2xthatacceptstwointsasinput,andreturnsaStringrepresentingthe
firstint(abase10number)expressedinthebasegivenbythesecondint.Forexample,
ten2x(13,2)mustreturn“1101”.Youmayassumethatthebasewillrangeonlyfrom2to10,
inclusive,andthatallinputswillbenon-negative.
BeingagoodsoftwaredeveloperyoufollowaTestDrivenDevelopmentapproach.WriteaJUnit
testthatverifiesoneaspectofthefunctionalityoftheten2xmethod.Assumethattheten2x
methodisdefinedinaclassnamedQ2,whichisinapackagenamedexam1.Youmayassumethis
classhasano-argumentconstructor.
WriteONLYatestmethod.Thismethodmustbeself-contained:ifyouneedanyobjecttodefinethe
method,createitinthemethod.
@Test
public void test01() {
exam1.Q2 su = new exam1.Q2();
String expected = "1101";
String actual = su.ten2x(13,2);
assertTrue("...", expected.equals(actual));
}
3
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question3[10points:10/7/3/0]
[QUESTION2andQUESTION3arerelated.READEACHFULLYBEFOREANSWERING!]
Defineamethodnamedten2xthatacceptstwointsasinput,andreturnsaStringrepresentingthe
firstint(abase10number)expressedinthebasegivenbythesecondint.Forexample,
ten2x(13,2)mustreturn“1101”.Youmayassumethatthebasewillrangeonlyfrom2to10,
inclusive,andthatallinputswillbenon-negative.
Hint:repeatedlydividethenumberbythebase:theremaindersarethedigitsoftheresult,inorder
fromrighttoleft.
Define this method (and only the method – do not give the surrounding class definition). Use the String
concatenation operator (+) to build up the answer.
public String ten2x(int input, int base) {
String answer = "";
do {
int quotient = input / base;
int remainder = input % base;
input = quotient;
char ch = digit2char(remainder);
answer = ch + answer;
} while (input > 0);
return answer;
}
4
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question4[10points:10/7/3/0]
Characterize the efficiency of the remove methods on the MultiSet and the java.util.ArrayList, as constant
time, linear time, n-squared time, or exponential time. Explain why remove on the MultiSet that we
defined is more efficient than remove on the java.util.ArrayList class in the standard Java library. Are we
just smarter than the folks who wrote the library, or is there something else going on?
There are two steps involved in removing an item from a
collection. First, the location of the item in the
underlying array must be determined (assuming it is
even present in the collection). This step requires
linear time in the size of the data structure
regardless of whether we are looking at the MultiSet or
the ArrayList.
Assuming the the item to be removed exists in the data
structure the second step involves actually removing
the datum. It is here the two data structures differ.
Because the MultiSet does not need to maintain the
relative order of elements removal can be done in
constant time. The ArrayList, on the other hand, needs
to preserve the relative order of elements, and so when
an item is removed data at higher indices must be
shifted to lower indices. This is a linear time
operation.
The overall big-oh analysis of removal (including the
search step) is linear in both cases.
5
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
Question5[10points:10/7/3/0]
Considerthefollowingcodeandthefeedbackfromtheunittests.Thecodecompilesandruns.
IdentifyandcorrecttheONLYerrorinthecode.Theerrorisinoneorbetweentwoofthelines
numbered/*1*/through/*8*/.TofixthebugyoumayEDITonelineorINSERTasmallblockof
code.
public class WriteUp {
/* Returns a new String which has exactly same characters as input, in the same order,
* except that any punctuation character ( . , ! ? : ; ) is doubled.
* You may assume that the input String is not null.
* For example: solution("Hi, there!") must return "Hi,, there!!"
*
solution("Hi there") must return "Hi there"
*/
public String solution(String input) {
/* 1 */
String answer = "";
/* 2 */
for (int i=0; i<input.length(); i=i+1) {
/* 3 */
char ch = input.charAt(i);
/* 4 */
if (ch == '.' || ch == ',' || ch == ':' || ch == ';' || ch == '!' || ch == '?') {
/* 5 */
answer = answer + ch + ch;
/* 6 */
}
/* 7 */
}
/* 8 */
return answer;
}
public void common(String input, String expected) {
String actual = new WriteUp().solution(input);
assertTrue("Expected: >"+expected+"<, actual: >"+actual+"<.",
expected.equals(actual));
}
@Test public void test1() { common("Hi, there!","Hi,, there!!"); }
@Test public void test2() { common("",""); }
@Test public void test3() { common("Hi there","Hi there"); }
}
TESTFEEDBACK:
test1: Expected: >Hi,, there!!<, actual: >,,!!<.
test2: passes
test3: Expected: >Hi there<, actual: ><.
Line(s)# Descriptionofproblemandbugfix
6,7
Non-punctuation characters from input String are
not added to answer String. To fix: insert an
else clause:
else {
answer = answer + ch;
}
6
Download