cs115_f09_x1

cs115_f09_x1 - CS115Exam1,Fall2009 Yourname Rules...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS115Exam1,Fall2009 Yourname:____________________________________________ Rules Youmayuseonehandwritten8.5x11"cheatsheet(frontandback).Thisis theonlyresourceyoumayconsultduringthisexam. Youmustshowyourwork/explainyouranswersinordertoreceivepartial creditforincorrectanswers. Allsnippetsofcodecanbeassumedtobeenclosedwithinint main(). Youcanassumethattheiostream,fstream,iomanip,string, algorithm,andcmathlibrarieshavebeenincludedatthebeginningofthe program. Whenyouareaskedtowriteasnippetofcode,youmayalsoassumethatitis enclosedwithinint main()andthatanynecessarylibrarieshavebeen included. Whenyouareaskedtowriteacompleteprogram,youmustwritethe #includestatements,theint main(),etc.inyoursolutiontoreceivefull credit. Alineconsistingsolelyof"..."representsoneormoreunspecifiedC++ statements,someofwhichmaychangethevaluesofprogramvariables. Grade(instructoruseonly) Problem1 Problem2 Problem3 Problem4 Total Problem1:24points. Whatdoeseachofthefollowingsnippetsofcodeprinttothescreen? (a) string a = "b"; string b = "a"; cout << a << endl; (b) string a = "b"; string b = "a"; cout << a << b << endl; (c) int a = 5; cout << ++a << endl; CS115Midterm1,Spring2009 Page2of7 (d) for (int i=0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << j << endl; } } (e) bool b = true; if (b) { b = b && false; } cout << b; (f) int i = 4; while (i > 0) { cout << i << endl; i -= 2; } CS115Midterm1,Spring2009 Page3of7 Problem2:16points. StatewhethereachsegmentofcodeisvalidC++,meaningthatitwillcompile. Ifitisvalid,write"valid."Ifitisinvalid,write"invalid"andfixitsothatitwill compile. (a) int a; cin >> a; if (sqrt(a)) { cout << "Moo!" << endl; } (b) int i = 5; while (i--) { cout << i << endl; } (c) int q = 2; if (q > 0); { cout << "Baa!" } else { cout << "Grrr!" } (d) int i = 25; int j = 100; i *= j; CS115Midterm1,Spring2009 Page4of7 Problem3:30points. Writeshortsnippetsofcodetoaccomplishthefollowingtasks: (a) Assume: o AnintegervariableNhasalreadybeendeclaredanddefined. Yourtask: o Printtheword"Hello!"Ntimes(ifNisnegative,donotprinttheword atall). o Each"Hello!"shouldbeonaseparateline. (b) Repeatedlyasktheusertoenteraninteger.Youshouldstopaskingwhen either o Theuserenterssomethingthatcouldnotberead,or o Theuserentersanegativenumber Otherthanpromptingtheuserforaninteger,yourprogramshouldnotprint anythingtothescreen. CS115Midterm1,Spring2009 Page5of7 (c) Assume:Floatvariablesa,b, and c havebeendeclaredanddefined. Yourtask: o Print"Yes!"ifa,b,andccouldbethesidelengthsofatriangle.For thistobethecase,eachnumbermustbelessthanthesumofthe othertwonumbers. o Print"No!"otherwise. (d) Assume:Floatvariablesaandbhavealreadybeendeclaredanddefined. Yourtask:Printa,b,andab(thatis,araisedtothepowerofb)asshownin theexamplebelowfora=2andb=3.5: 2 to the 3.5th power = 11.3137 Youdonotneedtoworryaboutformattingtheanswertoaspecificnumber ofdecimalplaces. CS115Midterm1,Spring2009 Page6of7 Problem4:30points. Forthisproblem,youmustwriteacompleteprogramthatdoesthefollowing: Promptstheusertoenter100integers.Itshouldpromptforeachinteger individually. Iftheuserentersaninvalidinput,theprogramshouldstopaskingfor integersandshouldnotprintanythingelse. Otherwise,aftertheuserhasenteredall100integers,theprogramshould printthemaximumofthe100integers. CS115Midterm1,Spring2009 Page7of7 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online