DOC PREVIEW
UB CSE 115 - exam 2 version 2 solved

This preview shows page 1-2 out of 6 pages.

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

Unformatted text preview:

Unit%Exam%#2% CSE115%Introduction%to%Computer%Science%I%–%VERSION%2% Fall%2016%%1%%!EXAMINATION%INSTRUCTIONS%%%%%This%examination%has%6%pages.%Check%that%you%have%a%complete%paper.%Each%candidate%should%be%prepared%to%produce,%upon%request,%his%or%her%SUNY/UB%card.%This%examination%has%5%questions.%Answer%all%questions.%You$have$60$minutes$to$complete$this$examination.$$Use$your$time$accordingly.$%%READ%AND%OBSERVE%THE%FOLLOWING%RULES:%%► Names%are%pre-printed%on%the%exam%booklets.%%Ensure%that%you%have%YOUR%exam.%► Sign,%using%your%usual%signature,%in%the%space%provided%on%the%back%cover.%► All%of%your%writing%must%be%handed%in.%This%booklet%must%not%be%torn%or%mutilated%in%any%way,%and%must%not%be%taken%from%the%examination%room.%► Show% all%of% your%work% in%arriving%at% an%answer,%unless% instructed%otherwise.% Partial%credit% will% be%awarded%as%appropriate.%► Candidates%are%not%permitted%to%ask%questions%of%the%invigilators,%except%in%cases%of%supposed%errors%or%ambiguities%in%examination%questions.%► CAUTION% –% Candidates% guilty% of% any% of% the% following,% or% similar,% dishonest% practices% shall% be%imme d iat ely %d ismissed%from %th e%e x amination%and %s h all%b e %lia b le%t o%d is cip lin a ry %ac tio n .%¨ Making% use% of% any% books,% papers% or% memoranda,% calculators% or% computers,% audio% or%visual% cassette% players,% or% oth er% memo ry% aid% device s,% o the r% than% tho se% explicitly%authorised%by%the%examiners.%¨ Speaking%or%communicating%with%other%candidates.%¨ Purposely%exposing% w ritten% papers%to%the%view%of%other%candidates.% The%plea%of%accident%or%forgetfulness%shall%not%be%received.%%%---------------------------%DO%NOT%WRITE%BELOW%THIS%LINE!%---------------------------%%%%%Q1%Q2%Q3%Q4%Q5%TOTAL%%$/10%/10%/10%/10%/10%/50%/100$%!Unit%Exam%#2% CSE115%Introduction%to%Computer%Science%I%–%VERSION%2% Fall%2016%%2%%Question!1![10!points,!2!points!each] The code given below is correct: it compiles without errors. I have added some extra spacing to make this question easier to answer. Circle, and identify by number, one and only one example of each of the following items in the code below. If you believe no example exists, write “no example” next to that item in the list. To show you how I want the question answered, the first one is done for you. 1. access control modifier 2. mutator method definition 3. conditional statement (entire statement) 4. looping statement (entire statement) 5. the name of an interface 6. a boolean literal package code.model; import java.util.ArrayList; import java.util.Random; import code.ui.UI; public class Model implements Observable { private UI _observer; private Random _rand; private ArrayList<String> _imageFileNames; private ArrayList<String> _spinnerCurrentValues; public Model() implements Observable { ß this was an error, but if they circled it’s fine _rand = new Random(); _imageFileNames = new ArrayList<String>(); _imageFileNames.add("Red.png"); _spinnerCurrentValues = new ArrayList<String>(); for(int i=0; i<_spinnerCurrentValues.size(); i=i+1) { _spinnerCurrentValues.add(i, _imageFileNames.get(_rand.nextInt(_imageFileNames.size()))); } } public boolean jackpot() { for (int i=1; i<_spinnerCurrentValues.size(); i=i+1) { if ( ! _spinnerCurrentValues.get(i-1).equals(_spinnerCurrentValues.get(i)) ) { return false; } } return true; } public void addObserver(UI ui) { _observer = ui; } public String getImageFileName(int i) { return _spinnerCurrentValues.get(i); } } ! !1Unit%Exam%#2% CSE115%Introduction%to%Computer%Science%I%–%VERSION%2% Fall%2016%%3%%Question!2![!!]!10!points:!perfect!!!! ! ! ! !!!![!!]!7!points:!essentially!correct!but!with!small!mistakes![!!]!!!3!points:!clearly!wrong,!some!correct!elements!!!![!!]!0!points:!for!anything !else !!Study!the!following!code,!then!answer!the!question!which!follows.! public String whatDoesThisDo(int x) { String result = ""; if (x < 55) { return "Rock"; } else if (x < 70) { result = "Sock"; x = x - 55; } else if (x < 85) { result = "Mock"; x = x - 70; } else { result = "Flock"; x = x - 85; } if (x < 5) { result = result + "$$$"; } else if (x < 10) { result = result + "%%%"; } else if (x < 15) { result = result + "###"; } return result; } [5!points]!What!does!the!following!statement!print?! System.out.println("Answer is "+ whatDoesThisDo(88)); Write!your!answer!below:!The answer is Flock$$$ [5!points]!Give!a!value!for!x!such!that!whatDoesThisDo(x)!returns!“Sock$$$”?!Write!your!answer!below:!55, 56, 57, 58, or 59. Any one of these is OK – they don’t need to give all.Unit%Exam%#2% CSE115%Introduction%to%Computer%Science%I%–%VERSION%2% Fall%2016%%4%%Question!3![!!]!10!points:!perfect!!!! ! ! ! !!!![!!]!7!points:!essentially!correct!but!with!small!mistakes![!!]!!!3!points:!clearly!wrong,!some!correct!elements!!!![!!]!0!points:!for!anything !else !!Define a method which takes two parameters, an int and an ArrayList<String>, which prints (using System.out.println) all the Strings from the ArrayList whose length is greater than or equal to the int. For example, if the method is named printer and is defined in a class named Question3, then new Question3().printer(4, null) must not produce any runtime errors and must print nothing, whereas ArrayList<String> list = new ArrayList<String>(); list.add("foo"); list.add("fluffy"); list.add("pi"); list.add(null); list.add("cake"); list.add("rho"); new Question3().printer(4, list); must not produce any runtime errors and must produce the following output: fluffy cake Write!your!answer!below: Here’s one possible solution – there are others. Any code that solves the problem in the general case is fine: [PARAMETER ORDER IS DIFFERENT IN VER 1 AND VER 2] public void printer(int len, ArrayList<String> list) { if (list != null) { for (String s : list) { if (s != null) { if (s.length() >= len) { System.out.println(s); } } } } }Unit%Exam%#2% CSE115%Introduction%to%Computer%Science%I%–%VERSION%2% Fall%2016%%5%%Question!4![!!]!10!points:!perfect!!!! ! ! !


View Full Document

UB CSE 115 - exam 2 version 2 solved

Download exam 2 version 2 solved
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 version 2 solved 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 version 2 solved 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?