DOC PREVIEW
UW CSE 142 - Study Notes

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:

1 of 6 CSE 142 Sample Midterm Exam #3 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 constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in "quotes"). Expression Value 1 + 2 * 3 - 4 * 5 5 / 2 + 9.0 / 2.0 - 2 * 1.25 29 % 2 % 5 + 34 % 3 8 + 6 * -2 + 4 + "0" + (2 + 5) 31 / 2 / 10.0 + 10 / (5 / 2.0) _____________________________ _____________________________ _____________________________ _____________________________ _____________________________ 2. Parameter Mystery (20 points) At the bottom of the page, write the output produced by the following program, as it would appear on the console. public class ParameterMystery { public static void main(String[] args) { String a = "felt"; String b = "saw"; String c = "drew"; String saw = "sue"; String drew = "b"; mystery(a, b, c); mystery(b, a, saw); mystery(drew, c, saw); mystery("a", saw, drew); mystery(a, a, "drew"); } public static void mystery(String b, String a, String c) { System.out.println(c + " " + a + " the " + b); } }2 of 6 3. While Loop Simulation (15 points) For each call below to the following method, write the output that is printed, as it would appear on the console: public static void mystery(int a, int b) { while (b != 0) { if (a > b) { System.out.print(a + " "); a = a - b; } else { System.out.print(b + " "); b = b - a; } } System.out.println(a); } Method Call Output mystery(42, 0); mystery(6, 12); mystery(18, 27); mystery(24, 60); mystery(50, 15); ____________________________________________ ____________________________________________ ____________________________________________ ____________________________________________ ____________________________________________3 of 6 4. Assertions (15 points) For each of the five points labeled by comments, identify each of the following assertions as being either always true, never true or sometimes true / sometimes false. public static int assertions(int n) { int x = 2; // Point A while (x < n) { // Point B if (n % x == 0) { n = n / x; x = 2; // Point C } else { x++; // Point D } } // Point E return n; } Fill in each box of the the table below with one of the following words: ALWAYS, NEVER or SOMETIMES. (You may abbreviate these choices as A, N, and S as long as you write legibly.) x > 2 x < n n % x == 0 Point A Point B Point C Point D Point E4 of 6 5. Programming (15 points) Write a static method named enoughTimeForLunch that accepts four integers hour1, minute1, hour2, and minute2 as parameters. Each pair of parameters represents a time on the 24-hour clock (for example, 1:36 PM would be represented as 13 and 36). The method should return true if the gap between the two times is long enough to eat lunch: that is, if the second time is at least 45 minutes after the first time. Otherwise the method should return false. You may assume that all parameter values are valid: the hours are both between 0 and 23, and the minute parameters are between 0 and 59. You may also assume that both times represent times in the same day, e.g. the first time won't represent a time today while the second time represents a time tomorrow. Note that the second time might be earlier than the first time; in such a case, your method should return false. Here are some example calls to your method and their expected return results: Call Value Returned enoughTimeForLunch(11, 00, 11, 59) enoughTimeForLunch(12, 30, 13, 00) enoughTimeForLunch(12, 30, 13, 15) enoughTimeForLunch(14, 20, 17, 02) enoughTimeForLunch(12, 30, 9, 30) enoughTimeForLunch(12, 00, 11, 55) true false true true false false5 of 6 6. Programming (15 points) Write a static method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. Here are some example calls to your method and their expected results: Call printGrid(3, 6); printGrid(5, 3); printGrid(4, 1); printGrid(1, 3); Output 1, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18 1, 6, 11 2, 7, 12 3, 8, 13 4, 9, 14 5, 10, 15 1 2 3 4 1, 2, 3 You may assume that both parameters passed to your method are greater than 0.6 of 6 7. Programming (10 points) 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 of input as their response to each prompt. Your code should be case-sensitive; for example, if the favorite letter is lowercase a, you should not stop prompting if the user types words that start with an uppercase 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: !!! 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 - Study Notes

Download Study Notes
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 Study Notes 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 Study Notes 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?