EXAMINATION INSTRUCTIONS

advertisement
UnitExam#1
CSE116IntroductiontoComputerScienceI
Spring2016
EXAMINATIONINSTRUCTIONS
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
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.
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.
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?
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
Download