CSE 142 SPRING 2011 FINAL EXAM SOLUTIONS 1 Expressions 6 points For each expression in the left hand column indicate its value in the right hand column Be sure to list a constant of appropriate type e g 7 0 rather than 7 for a double Strings in quotes If the expression is illegal then write error Expression Value 1 2 4 2 1 2 3 5 10 3 1 error 2 8 1 2 3 6 2 3 3 1 0 2 points each for the first two all or nothing 2 points for the last one 1 if wrong type 2 Array Mystery 10 points Consider the following method public static void mystery int array for int i array length 2 i 0 i array i array i array i 1 array i 1 Indicate in the right hand column what values would be stored in the array after the method mystery executes if the integer array in the left hand column is passed as a parameter to mystery Array Final Contents of Array int a1 8 9 mystery a1 8 9 int a2 7 1 1 mystery a2 7 9 1 int a3 5 4 3 2 1 mystery a3 5 22 13 6 1 int a4 1 2 3 4 5 mystery a4 1 20 17 12 5 1 points per array for first two all or nothing 4 points per array for second two 2 per wrong missing max 4 per array 1 of 1 3 Reference Semantics Mystery 10 points What does the following program print import java util public class ReferenceMystery public static void main String args int a 1 2 3 4 5 int x 0 System out println x Arrays toString a x a x 12 mystery a x System out println x Arrays toString a x a x 34 mystery a x System out println x Arrays toString a public static void mystery int a int x x a x 56 System out println x Arrays toString a 0 2 1 3 2 1 1 1 1 1 2 3 4 5 12 56 4 5 12 56 4 5 12 34 56 5 12 34 56 5 2 points per line 1 point for left number 1 point for array 1 instead of 2 if lines 2 and 3 are wrong but the same same goes with lines 4 and 5 2 of 2 4 Mystery 3 points What does the following program print public class Mystery public static void main String args int a 3 int b 7 mystery a b System out println a b public static void mystery int a int b int temp a a b b temp 3 7 3 points all or nothing 3 of 3 5 Inheritance Mystery 10 points Assume that the following classes have been defined public class Butters extends South public void methodB System out print Butters B public class South public void methodA System out print South A methodB public String toString return Loo loo loo public void methodB System out print South B public class Wendy extends South public void methodB System out print Wendy B public String toString return Wendy public String toString return South Park public class Stan extends Wendy public void methodB System out print Stan B super methodB Given the classes above what output is produced by the following code South park new Wendy new Butters new South new Stan for int i 0 i park length i park i methodA System out println park i methodB System out println System out println park i System out println South A Wendy B Wendy B Wendy South A Butters B Butters B Loo loo loo 1 point for 4 separate sections 2 points per section for first three 1 per wrong word max 2 3 points for last section 1 per wrong word max 3 2 for quotes consistent extra newlines South A South B South B South Park South A Stan B Wendy B Stan B Wendy B Wendy 4 of 4 6 Programming 16 points Write a static method blackjack that prints random numbers in the range of 1 10 until the sum of all integers generated are greater than or equal to 17 The sum is subsequently printed If the sum is greater than 21 then the method prints out Busted if the sum is equal to 21 then the method prints out BLACKJACK The method returns whether the sum was less than or equal to 21 Call Prints Returns blackjack blackjack blackjack blackjack blackjack 2 4 6 10 22 Busted 2 2 1 5 7 17 9 5 7 21 BLACKJACK 5 6 3 2 10 26 Busted 10 5 4 19 false true true false true As this method has an element of randomness to it you are to copy the format of the output not the exact output of the sample calls public static boolean blackjack Random rand new Random int sum 0 while sum 17 int card rand nextInt 10 1 System out print card sum card System out print if sum 21 System out print return false else if sum 21 System out print sum Busted BLACKJACK return true header 1 return value 1 name random 1 creates Random object 1 calls nextInt 1 calls nextInt with proper range cards retrieves cards until 17 1 attempt 1 correct 2 proper cumulative sum printout 2 conditionally prints BLACKJACK and Busted 1 prints and spaces return 1 has a return 1 returns true and false somewhere 2 works can still get this point if minor error above 5 of 5 7 Programming 10 points Assume that the following method exists returns the order number of each letter case insensitive public static int charToIndex char letter For example Call Returns charToIndex a charToIndex A charToIndex e charToIndex Z 1 1 5 26 Write a static method isAnagram that accepts two words as parameters and returns whether the words are anagrams of each other An anagram of a word is another word that uses the exact same letters For example asleep and please are anagrams as are dad and add Call Returns isAnagram asleep please isAnagram dad add isAnagram yes no isAnagram yes yea true true false false Hint Tally letters public static boolean isAnagram String word1 String word2 int letterCount1 new int 26 int letterCount2 new int 26 if word1 length word2 length return false for int i 0 i word1 length i char letter1 word1 charAt i char letter2 word2 charAt i letterCount1 charToIndex letter1 1 letterCount2 charToIndex letter2 1 return Arrays equals letterCount1 letterCount2 header 1 return value 1 name params arrays 1 creates arrays of the right range 26 1 properly indexes array 1 compares arrays loop 1 correct bounds charToIndex 1 calls charToIndex 1 uses return value header if used nested for loops 1 return value 1 name params counting 1 counts compare counts 1 attempt 1 correct loop 1 correct bounds compares letters 1 attempt 1 correct case insensitive 2 works can still get this point if minor error above lose 2 works points 6 of 6 8 Programming 18 points a 11 points …
View Full Document
Unlocking...