DOC PREVIEW
UW CSE 142 - Sample Midterm Exam

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

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

Unformatted text preview:

1 of 8 CSE 142 Sample Midterm Exam #1 1. Expressions 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, true/false for a boolean). Expression Value 3 * 4 + 5 * 6 + 7 * -2 _________________________ 1.5 * 2.0 + (5.5 / 2) + 5 / 4 _________________________ 23 % 5 + 31 / 4 % 3 - 17 % (16 % 10) _________________________ "1" + 2 + 3 + "4" + 5 * 6 + "7" + (8 + 9) _________________________ 345 / 10 / 3 * 55 / 5 / 6 + 10 / (5 / 2.0) _________________________ 1 / 2 > 0 || 4 == 9 % 5 || 1 + 1 < 1 - 1 _________________________2 of 8 2. Parameter Mystery At the bottom of the page, write the output produced by the following program. public class ParameterMystery { public static void main(String[] args) { String x = "java"; String y = "tyler"; String z = "tv"; String rugby = "hamburger"; String java = "donnie"; hamburger(x, y, z); hamburger(z, x, y); hamburger("rugby", z, java); hamburger(y, rugby, "x"); hamburger(y, y, "java"); } public static void hamburger(String y, String z, String x) { System.out.println(z + " and " + x + " like " + y); } }3 of 8 3. If/Else Simulation For each call of the method below, write the value that is returned: public static int mystery(int a, int b) { int c; if (a > b) { c = a; } else if (b % a == 0) { c = b; } else { c = b + (a - (b % a)); } return c; } Method Call Value Returned mystery(4, 2) _______________________________ mystery(5, 4) _______________________________ mystery(5, 13) _______________________________ mystery(5, 17) _______________________________ mystery(4, 8) _______________________________4 of 8 4. While Loop Simulation For each call of the method below, write the output that is printed: public static void mystery(int i, int j) { while (i != 0 && j != 0) { i = i / j; j = (j - 1) / 2; System.out.print(i + " " + j + " "); } System.out.println(i); } Method Call Output mystery(5, 0); _______________________________ mystery(3, 2); _______________________________ mystery(16, 5); _______________________________ mystery(80, 9); _______________________________ mystery(1600, 40); _______________________________5 of 8 5. Assertions For the following method, identify each of the three assertions in the table below as being either ALWAYS true, NEVER true or SOMETIMES true / sometimes false at each labeled point in the code. (You may abbreviate these choices as A/N/S respectively.) public static int mystery(int x) { int y = 1; int z = 0; // Point A while (y <= x) { // Point B y = y * 10; z++; // Point C } // Point D z--; // Point E return z; } y > x z < 0 z > 0 Point A Point B Point C Point D Point E6 of 8 6. Programming Write a static method named hasMidpoint that accepts three integers as parameters and returns true if one of the integers is the midpoint between the other two integers; that is, if one integer is exactly halfway between them. Your method should return false if no such midpoint relationship exists. The integers could be passed in any order; the midpoint could be the 1st, 2nd, or 3rd. You must check all cases. Calls such as the following should return true : hasMidpoint(4, 6, 8) hasMidpoint(2, 10, 6) hasMidpoint(8, 8, 8) hasMidpoint(25, 10, -5) Calls such as the following should return false : hasMidpoint(3, 1, 3) hasMidpoint(1, 3, 1) hasMidpoint(21, 9, 58) hasMidpoint(2, 8, 16)7 of 8 7. Programming Write a static method named sequenceSum that prints terms of the following mathematical sequence: ...61514131211 ++++++ ( also written as ∑∞=11ii ) Your method should accept a real number as a parameter representing a limit, and should add and print terms of the sequence until the sum of terms meets or exceeds that limit. For example, if your method is passed 2.0, print terms until the sum of those terms is at ≥ 2.0. You should round your answer to 3 digits past the decimal point. The following is the output from the call sequenceSum(2.0); 1 + 1/2 + 1/3 + 1/4 = 2.083 (Despite the fact that the terms keep getting smaller, the sequence can actually produce an arbitrarily large sum if enough terms are added.) If your method is passed a value less than 1.0, no output should be produced. You must match the output format shown exactly; note the spaces and pluses separating neighboring terms. Other sample calls: Calls sequenceSum(0.0); sequenceSum(1.0); sequenceSum(1.5); Output 1 = 1.000 1 + 1/2 = 1.500 Call sequenceSum(2.7); Output 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 = 2.7188 of 8 8. Programming Write a static method named favoriteLetter that accepts two parameters: a Scanner for the console, and a favorite letter represented as a one-letter String. The method repeatedly prompts the user until two consecutive words are entered that start with that letter. The method then prints a message showing the last word typed. You may assume that the user will type a single-word response to each prompt. Your code should be case-sensitive; for example, if the favorite letter is a, you should not stop prompting if the user types words that start with an A. For example, the following logs represent the output from two calls to your method: (User input is underlined.) Call Scanner console = new Scanner(System.in); favoriteLetter(console, "y"); Scanner console = new Scanner(System.in); favoriteLetter(console, "A"); Output Looking for two "y" words in a row. Type a word: hi Type a word: bye Type a word: yes Type a word: what? Type a word: yellow Type a word: yippee "y" is for "yippee" Looking for two "A" words in a row. Type a word: I Type a word: love Type a word: CSE142! Type a word: AND Type a word: PROGRAMS Type a word: are Type a word: always Type a word: Absolutely Type a word: Awesome "A" is for


View Full Document

UW CSE 142 - Sample Midterm Exam

Download Sample Midterm Exam
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 Sample Midterm Exam 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 Sample Midterm Exam 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?