DOC PREVIEW
UW CSE 142 - Exam Guide

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 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 10 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 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 of 1 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 (3 < 5 < 10) && (3 > 1) 2.8 + 1.2 – 3 % 6 + 2 * 3 % 3 2 error 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); int[] a2 = {7, 1, 1}; mystery(a2); int[] a3 = {5, 4, 3, 2, 1}; mystery(a3); int[] a4 = {1, 2, 3, 4, 5}; mystery(a4); {8, 9} {7, 9, 1} {5, 22, 13, 6, 1} {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)2 of 2 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 [1, 2, 3, 4, 5] 2 [1, 12, 56, 4, 5] 1 [1, 12, 56, 4, 5] 3 [1, 12, 34, 56, 5] 2 [1, 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)3 of 3 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 nothing4 of 4 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 String toString() { return "Loo loo loo"; } } public class Wendy extends South { public void methodB() { System.out.print("Wendy B "); } public String toString() { return "Wendy"; } } public class South { public void methodA() { System.out.print("South A "); methodB(); } public void methodB() { System.out.print("South B "); } 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 South A South B South B South Park South A Stan B Wendy B Stan B Wendy B Wendy 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 newlines5 of 5 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("= " + sum); if (sum > 21) { System.out.print(" Busted!"); return false; } else if (sum == 21) { System.out.print(" 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)6 of 6 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.


View Full Document

UW CSE 142 - Exam Guide

Download Exam Guide
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 Guide 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 Guide 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?