DOC PREVIEW
UB CSE 115 - exam 2 sample questions

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

["CSE115"–"EXAM"#2"SOLUTIONS"TO"SAMPLE"QUESTIONS"]!!1!!Here!are!some!possible!questions.!!They!are!not!necessarily!indicative!of!the!questions!you!will!get!on!the!exam,!nor!are!they!necessarily!representative!of!the!range!of!topics!to!be!tested!on!the!exam.!!THESE!QUESTIONS!REPRESENT!JUST!A!SMALL!SAMPLING!OF!THE!KINDS!OF!QUESTIONS!THAT!MAY!BE!ENCOUNTERED!ON!THE!EXAM.!!IT!IS!YOUR!RESPONSIBILITY!TO!PREPARE!TO!ANSWER!QUESTIONS!FROM!ALL!THE!MATERIAL!COVERED!FOR!THIS!EXAM,!INCLUDING!THOSE!CONCEPTS!COVERED!IN!LABS.!!!• Identify!parts!of!code.! See answer on page 3. !• Write!code!for!relationship!–!see!sample!question!on!page!3.!!Relevant!relationships!are!association!(via!constructor!only,!via!accessor!and!mutator!only,!or!via!constructor,!accessor!and!mutator),!composition,!and!realization.! See answer on page 4. • Given!a!small!bit!of!code,!draw!an!object!diagram!to!show!the!resulting!program!state.!!For!example,!given!this!class!definition:!public class ShoppingCart { private Item _item; public ShoppingCart() { _item = null; } public void addItem(Item i) { _item = i; } public Item removeItem () { Item temp = _item; _item = null; return temp; } } draw!an!object!diagram!to!show!the!resulting!program!state ShoppingCart cart1 = new ShoppingCart(); cart1.addItem(new Item()); ShoppingCart cart2 = new ShoppingCart(); cart2.addItem(new Item());["CSE115"–"EXAM"#2"SOLUTIONS"TO"SAMPLE"QUESTIONS"]!!2!! cart1 cart2 • Be!prepared!to!write!a!small!amount!of!code.!!For!example,!suppose!you!are!given!the!above!class!definition!for!ShoppingCart,!and!the!following!variable!declarations:!ShoppingCart cart1 = new ShoppingCart(); cart1.addItem(new Item()); ShoppingCart cart2 = new ShoppingCart(); cart2.addItem(new Item()); Write!code!to!interchange!the!items!in!the!two!ShoppingCart!objects!referred!two!by!cart1!and!cart2.!!Do!NOT!just!interchange!the!value!of!cart1!and!cart2.!! here’s one possibility – there are others: Item temp = cart1.removeItem(); cart1.addItem(cart2.removeItem()); cart2.addItem(temp); !• You!must!understand!the!difference!between!a!local!variable!and!an!instance!variable,!in!the!following!ways:!o you!must!know!how!to!declare!each!kind!of!variable!o you!must!know!the!differences!in!scope!and!lifetime!between!them!Here’s!a!possible!question:!assuming!the!following!declaration!has!been!done,!ShoppingCart cart = new ShoppingCart(); draw!a!memory!diagram!showing!a!possible!snapshot!of!memory!during!the!execution!of!the!following!method!call:!cart.addItem(new Item());! By “possible” here I am referring to the fact that the absolute location in memory of the objects/variables is not important, but rather how they are related. Assume that 401 and 402 are part of the invocation record of the cart.addItem(…) call, but that 400 is not. 400 1000 cart 401 1000 this 402 800 i ShoppingCart _item Item ShoppingCart _item Item["CSE115"–"EXAM"#2"SOLUTIONS"TO"SAMPLE"QUESTIONS"]!!3!! !• In the code below, clearly circle!and!identify!by!number!one (and only one) example of each of the following. If you believe no example exists in the given code, write \no!example"!next to that item. 1. fully qualified class name 2. class body 3. class header 4. constructor header 5. (local) variable declaration 6. method call 7. Java reserved word (any one will do) 8. access control modifier 9. an expression which instantiates a class 10. an assignment statement package sample; public class EcoSystem { public EcoSystem() { chapter1.Terrarium myTerrarium; myTerrarium = new chapter1.Terrarium(); myTerrarium.add( new chapter1.Ant() ); } } ! ! 800 1000 800 _item 1 2 3 4 5 6 7, 8 9 10["CSE115"–"EXAM"#2"SOLUTIONS"TO"SAMPLE"QUESTIONS"]!!4!!• Write out the minimal code for the source class needed to show the following relationship: package cse115; public class Whole { private Part _p; public Whole() { _p = new Part(); } } • Demonstrate!an!understanding!of!how!control!statements!work!(if,!if-else,!while,!enhanced!for),!and!also!how!collections!function.!!For!example,!state!what!is!printed!when!the!following!code!executes:!! HashSet<String> set = new HashSet<String>(); set.add("all"); set.add("cows"); set.add("eat"); set.add("grass"); for (String s : set) { if (s.length() == 3) { System.out.println("Passes: "+s); } else { System.out.println("Fails: "+s); } } !!Passes: all Fails: cows Fails: grass Passes:


View Full Document

UB CSE 115 - exam 2 sample questions

Download exam 2 sample questions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view exam 2 sample questions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view exam 2 sample questions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?