EXAMINATION INSTRUCTIONS

advertisement
UnitExam#2
CSE115IntroductiontoComputerScienceI
Spring2016
EXAMINATIONINSTRUCTIONS
Thisexaminationhas6pages,5numberedpagesandthiscoverpage.
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
0
TOTAL
/50
%
/100
UnitExam#2
CSE115IntroductiontoComputerScienceI
Spring2016
Question1[10points,2pointseach]
Thecodegivenbelowiscorrect:itcompileswithouterrors.Clearlycircleandidentifybynumber
one(andonlyone)exampleofeachofthefollowing.Besuretoidentifyeachitemprecisely(circle
nomoreandnolessthannecessary).Ifyoubelievenoexampleexistsinthegivencode,write“no
example"nexttothatitem.
1.intliteral
2.a(non-parameter)localvariabledeclaration
3.Stringconcatenationoperator
4.relationaloperator
5.booleanoperator
package code;
import java.util.ArrayList;
import java.util.HashSet;
public class Model {
private HashSet<String> _words;
private ArrayList<String> _longWords;
public Model(HashSet<String> words, ArrayList<String> longWords) {
_words = words;
_longWords = longWords;
}
public Model(String filename) {
_words = new HashSet<String>();
_longWords = new ArrayList<String>();
readDictionaryFromFile(filename);
}
public void readDictionaryFromFile(String filename) {
CSE115FileReader scan = new CSE115FileReader(filename);
while (scan.hasNext()) {
String word = scan.next();
if (word.length() == 7 && ! _longWords.contains(word)) {
_longWords.add(word);
}
if (word.length() < 7) {
_words.add(word);
}
}
}
// THE REST OF THE CLASS DEFINITION IS OMITTED
}
1
UnitExam#2
CSE115IntroductiontoComputerScienceI–VERSION1
Fall2015
Question2[10pointstotal]
[GRADING:10totalmarks–gradedas10(perfect),7(smallmistakesbutontherighttrack),3(clearlywrong
butadecentattempt),or0(otherwise)]
Studythefollowingcode,thenanswerthequestionbelow.
public int question2(int x, int y) {
System.out.println("x is "+x);
System.out.println("y is "+y);
int answer = 0;
if (2*x < y) {
System.out.println("2*x is "+(2*x));
answer = -1;
}
else if (2*y < x) {
System.out.println("2*y is "+(2*y));
answer = 1;
}
if (x == y) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
return answer;
}
Whatisprintedwhenquestion2(12, 4)isexecuted?
y is 4
2*y is 8
NO
Whatvalueisreturnedwhenquestion2(12, 4)isexecuted?
1
2
UnitExam#2
CSE115IntroductiontoComputerScienceI–VERSION1
Fall2015
Question3[10points,2pointseach]
[GRADING:10totalmarks–gradedas10(perfect),7(smallmistakesbutontherighttrack),3(clearlywrong
butadecentattempt),or0(otherwise)]
TheU.SNationalLibraryofMedicinepublishesguidelinesforbloodsodiumlevels.These
guidelinesaresummarizedinthistable:
Blood sodium level
135 to 145 mEq/L
above 145 mEq/L
below 135 mEq/L
Category
normal
hypernatremia
hyponatremia
DefineamethodnamedsodiumLevelsoithasoneparameter,oftypeint.Definethe
methodsoitreturns,asaString,thecategoryassociatedwiththesodiumvalue(an
integerrepresentingthebloodsodiumlevel,expressedinunitsmEq/L)passedinasan
argument.
Forexample,theresultofrunningthiscode,
mustbe:
System.out.println(sodiumLevel(134));
System.out.println(sodiumLevel(142));
System.out.println(sodiumLevel(153));
hyponatremia
normal
hypernatremia
public String sodiumLevel(int x) {
if (x < 135) {
return "hyponatremia";
}
if (x > 145) {
return "hypernatremia";
}
return "normal";
}
3
UnitExam#2
CSE115IntroductiontoComputerScienceI–VERSION1
Fall2015
Question4[10pointstotal]
[GRADING:10totalmarks–gradedas10(perfect),7(smallmistakesbutontherighttrack),3(clearlywrong
butadecentattempt),or0(otherwise)]
DefineaclassLipoProteinPanelsothataLipoProteinPanelobjectstorestheresultsofa
cholesteroltest:totalcholesterol,LDLcholesterolandHDLcholesterol.Eachofthese
individualresultsisgivenbyanintvalue.Don'tforgettodefineaconstructorthatlets
thesethreevaluesbeprovidedasarguments,asin:
LipoProteinPanel p = new LipoProteinPanel(190, 90,74);
Theorderorargumentsmustbetotalcholesterol,LDLcholesterolandHDLcholesterol.
Finally,defineano-argumentmethodnamed'normal'thatreturnstrueiftotalcholesterol
islessthan200andLDLcholesterolislessthan130andHDHcholesterolisgreaterthanor
equalto40.Themethodmustreturnfalseotherwise.
package exam2;
public class LipoProteinPanel {
private int _total;
private int _ldl;
private int _hdl;
public LipoProteinPanel(int tot, int ldl, int hdl) {
_total = tot;
_ldl = ldl;
_hdl = hdl;
}
public boolean normal() {
return _total < 200 && _ldl < 130 && _hdl >= 40;
}
}
4
UnitExam#2
CSE115IntroductiontoComputerScienceI–VERSION1
Fall2015
Question5[10points]
[GRADING:10totalmarks–gradedas10(perfect),7(smallmistakesbutontherighttrack),3(clearlywrong
butadecentattempt),or0(otherwise)]
AssumethattheLipoProteinPanelclassdiscussedinquestion4hasbeencorrectlydefined.
DefineamethodnamedflagthattakesanArrayList<LipoProteinPanel>andreturnsanew
ArrayList<LipoProteinPanel>.ThereturnedArrayListmustcontainallthe
LipoProteinPanelobjectsintheargumentlistforwhichthenormalmethodreturnsfalse.
Forexample:
ArrayList<LipoProteinPanel> input;
input = new ArrayList<LipoProteinPanel>();
LipoProteinPanel a = new LipoProteinPanel(190,90,74);
LipoProteinPanel b = new LipoProteinPanel(205,90,74));
LipoProteinPanel c = new LipoProteinPanel(190,141,74));
input.add(a);
input.add(b);
input.add(c);
ArrayList<LipoProteinPanel> actual = flag(input);
Actualmustcontainexactlybandc.Inparticular,actualmustnotcontaina.
public ArrayList<LipoProteinPanel> flag(ArrayList<LipoProteinPanel> results) {
ArrayList<LipoProteinPanel> flaggedResults = new ArrayList<>();
for (LipoProteinPanel p : results) {
if (!p.normal()) {
flaggedResults.add(p);
}
}
return flaggedResults;
}
5
Download