CSE142 Midterm Autumn 2015 This exam is divided into eight questions with the following points: # Problem Area Points Score 1 Expressions 10 og 2 Parameter Mystery 12 f/else Simulation 12 \1^ \1^ 4 While Loop Simulation 12 5 Assertions 15 i-u 6 Programming 15 1 7 Programming 15 8 Programming 9 H Total 100 This is a closed-book/closed-note exam. Space is provided for your answers. There is a "cheat sheet" at the end that you can use as scratch paper or to write answers. You can also request scratch paper from a TA. You are not allowed to access any of your own papers during the exam. The exam is not graded on style and you do not need to include comments, although you are limited to the constructs included in chapters 1 throuqh 5 of the textbook. You are allowed to abbreviate "Always", "Never," and "Sometimes" as "A", "N", and "S" for the assertions question, but you should otherwise NOT use any abbreviations on the exam. You are NOT to use any electronic devices while taking the test, including calculators. Anyone caught using an electronic device will receive a 10 point penalty. Do not begin work on this exam until instructed to do so. Any student who starts early or who continues to work after time is called will receive a 10 point penalty. f you finish the exam early, please hand your exam to the instructor and exit quietly.
1. Expressions, 10 points. For each expression in the left-hand column, indicate its value in' the right-hand column. Be sure to list a consta appropriate type (e.g., 7.0 rather than 7 for a double. Strings in quot Expression 1 2 * 5 / v a.5 2.5*2*5/ 101.5 85--%^ 4-%-±0 - -17-^-^..2--K- (^ K-4.) -2-^^ 482-7^<Lj / 5/ 2.0*2 14/5 16 Value 4,0 i( t( s,76 [ (.0 <1 ^ 0 -j ^ 2. Parameter Mystery, 12 points. Consider the following program. public class Mystery { public static void main(strihg[] args) { String she = "it"; String it = "her"; String her = "you"; String yoo_5_ lija^; saying(yoh^ i^, you); saying(it, her, she); saying(she, "you", her); saying(it, "him", "fred"); public static void saying(string it. String her. String she) { System.out.println(she " can't take " it " with " her); List below the output produced by this program. <yf\ -V hike '^(Xj C<5ti'4 h \A!\^h ycu {^(5^ co/im ^(\ki \f\^ 1^1 n)
Simulation, 12 points. Consider the following method public static void ifelsemystery(int a, int b) { if (a < b) { a = a 2; tj b,= b - ; U. else if (a == b) { a = a * 2; O b = b 5; else { O a = a - ; 5 > if (a == b) { b = b 10; System.out.println(a " " b); For each call below, indicate what output is produced. Method Call ifelsemystery(2, 5) ifelsemystery(5, 5) ifelsemystery(6, ) ifelsemystery(4, 4) ifelsemystery(, 8) ifelsemystery(9, 5) Output Produced g 10 20 4. While Loop Simulation, 12 points. Consider the following method: 5 G 5 public static void mystery(int x) { int y = 1; int z = while (x > z) { if (x % z == 0) { X = X- / z; y; else { Z; System.out.println(y " z); For each call below, indicate what output is produced: Method Call mystery(2); mystery(5); mysteryj^; ^ mystery(12); Output Produced a -2. g a 1 -z a 6 5 5 15 Y
5. Assertions, 15 points. You will identify various assertions as bein always true, never true or sometimes true/sometimes false at various in program execution. The comments in the method below indicate the of interest. lah public static void mystery(scanner console) { int y = 42; int z = 0; // Point A while (y!= 0) { ^^ ^ // Point B y = console.nextint0; if (y % 2 == 0) { Z; // Point C wc /)d<^ // Point D Od 0^^ o y--; // Point E System.out.printlnC'z = z); A A, ro 0/^ '0 P po Fill in the table below with the words ALWAYS, NEVER or SOMETMES. Point Point Point Point Point A B C D E y == 0 y % 2 == 0 z == 0 K/ \ f\ \...... i...i....^a...1......rsl...i- '.. y......i- --S...
L u 0 ' 'j ^ points. Write a. static method called spinwheel that takes a id an'integer n as parameters and that simulates the spinning the number 20 comes up n times in a row. On the wheel are 0, 40, 50, and 60 and each number should be equally likely the wheel is spun. Your method should report the individual 5 indicating how many times it takes to get n occurrences of Dr example, below are two sample calls: = new Random 0; Mr, 2); Mr, ); should produce two lines of output like this: 40, 50, 20, 50, 50, 40, 20, 0, 40, 50, 20, 20 after 1 spins should produce two lines of output like this: 50, 50, 20, 40, 20, 40, 20, 20, 20 after 10 spins spin values ape separated by commas and that the method las seen n occurrences of the value 20 in a row. You are to ice the format of these logs. You may assume that the value n method is greater than or equal to 1. void ^ non p /fii O' 5 piw;; > rn 0 y P<(\^j C6^K\\ ;, (c J ^ J f 2)$ i-f- Covm \ \ J 4-2)^/0 a H 6 Covn-^ 0\ ' / V Sy5 (fna,(vi"'pr''l4lri -t
ng, 15 points. Write a static method called spinwheel that takes a,m object and an integer n as parameters and that simulates the spinning f' a wheel until the number 20 comes up n times in a row. On the wheel are the numbers 20, 0, 40, 50, and 60 and each number should be equally likely to come up when the wheel is spun. Your method should report the individual spins as well as indicating how many times it takes to get n occurrences of 20 in a row. For example, below are two sample calls: c * ' S' ^ ^ Random r = new RandomO; spinwheel(r, 2); spinwheel(r, ); P P The first call should produce two lines of output like this: spins: 40, 40, 50, 20, 50, 50, 40, 20, 0, 40, 50, 20, 20 2 in a row after 1 spins The second call should produce two lines of output like this: spins: 50, 50, 50, 20, 40, 20, 40, 20, 20, 20 in a row after 10 spins Notice that the spin values are separated by commas and that the method stops when it has seen n occurrences of the value 20 in a row. You are to exactly reproduce the format of these logs. You may assume that the value n passed to your method is greater than or equal to 1. 1a pri^viws^j/ J T "5 W '^S i/^hi (c s>.'^a1^.' ( f 2)^ Covm ) 2))elD a H 1 N S 'C 0VV''' O' <r ^ u ^ r^p'iwsft; f nvwac);^'^' Svsfew.(vTiPrHn [r(aa\ gpivs sj iav-s 0/ ^
Programming, 15 points. Write a static method called balancecheckbd takes a console Scanner as a parameter and that prompts a user for information about transactions for a bank account, reporting the new b after each transaction and the minimum balance at the end and returning whether or not the account was ever overdrawn (true if it was, false if i was not). The user is prompted for an initial balance, the number of transactions to process, and the individual transaction amounts. Deposits to the account are entered as positive numbers and checks and ATM withdrawals are entered as negative numbers. A new balance is reported after each transaction. For example, the method would be called as follows: Scanner console = new Scanner(System.in); balancecheckbook(console); Below are two sample logs of execution that might be produced: initial balance? 48.50 how many transactions? 4 1/4 amount? -20.00 new balance = $28.5 2/4 amount? -5.75 new balance = $22.75 /4 amount? 18.20 new balance = $160.95 4/4 amount? -20.00 new balance = $140.95 minimum balance = $22.75 initial balance? 9.75 how many transactions? 5 1/5 amount? -18.50 new balance = $21.25 2/5 amount? -7.20 new balance = $14.05 /5 amount? -2.10 new balance = $-9.05 4/5 amount? 50.00 new balance = $40.95 5/5 amount? -8.45 new balance = $2.5 minimum balance = $-9.05 n the log to the left, the user enters 4 different transactions and the minimum balance is not negative, so the method would return false to indicate that the account was never overdrawn. n the log to the right, the user enters 5 transactions and the minimum balance is negative, so the method would return true to indicate that the account was overdrawn. You are to exactly reproduce the format of these logs. You may assume that the number of transactions entered by the user is at least 1. Space is provided on the opposite page for your answer.
y.. provide your answer to balancecheckbook below. r ys<rn.aaf)i'f)^'ir),-l,'^( Marcel j>\ VJ np^- Aojiole ohn - ^ ''-y fisi' Cm ^ JL' ' i tt'- JU -^/ ' <-n M _ ^(mv t'l ;,s - '«n fr\m - d ''i ( i {!-r\ t V
8, or- Programming, 9 points. Write a static method called samedashes th two strings as parameters and that returns whether or not they have d in the same places (returning true if they do and returning false otherwise). For example, below are four pairs of strings of equal lengt that have the same pattern of dashes. Notice that the last pair has no dashes at all. Stringl: stringz: "hi--there-you." "12--(14)-759" "-15-89" -xy-zzy "criminal-plan" "(206)555-184" "ab,c" "9.8" To be considered a match, the strings must have exactly the same number of dashes in exactly the same positions. The strings might be of different length. For example, the following calls should each return true: samedashes("lst-has-more characters", "2nd-has-less") samedashes("lst-has-less", "2nd-has-more characters") because the strings each have two dashes and they are in the same positions. But the following calls should each return false because the longer string has a third dash where the shorter string does not: samedashes("1st-has-more-characters", "2nd-has-less") samedashes("1st-has-less", "2nd-has-more-characters") n solving this problem you may use only methods listed ui L 1C Cl ica L 'a s4.ica»t,ri ^ Y-= i,, 1-0/i ^ X' i)5( si. r,
C\jr^