Honors Computer Programming 1-2 Chapter 5 Exam Prep
1. True or False: curly braces When writing an if statement, you MUST use if { }. false: you must use parenthesis if ( )
2. In Java, to implement a choice between alternatives use the if/ statement. or if (...)...... switch statement switch (...) { case 1:... case 2:...... default:... }
3. State what the following symbols in Java code translate to in English: A >= greater than or equal to C or B && and D = = is equal to E!= not equal to
4. Compare two primitive values: are both double values, then is the best way to test for equality. If distance1 and distance2 (distance1 = = distance2) true: primitive values can be compared using the = = operator.
5. Compare two String objects: Given the code shown, String title1 = "mineral"; String title2 = "mine"; String title3 = title1.substring(0, 4); (title2 = = title3) is the best way to test for equality. title1 title2 m i n e r a l 0 1 2 3 4 5 6 m i n e 0 1 2 3 title3 m i n e 0 1 2 3 false: you must use the equals method to test for equality of String objects (title2.equals(title3))
6. Compare two Rectangle objects: Rectangle objects, for equality. then (box1 = = box2) If box1 and box2 are both is the best way to test false: you must use the equals method to test for equality of Rectangle or Rectangle2D objects (box1.equals(box2))
7A 7B Give What an is the example output: of how to use the compareto method? String System.out.println("Hello".compareTo( Cowboy")); str1 = "car"; String str2 = "apple"; if (str1.compareto(str2) < 0) System.out.println(str1 + " " + str2); System.out.println(str2 + " " + str1); In the alphabet, the letter H which begins the word Hello comes 5 letters after the letter C which begins the word Cowboy. if (str1.compareto("pear") < 0) System.out.println(str1 + " pear"); System.out.println("pear " + str1); Therefore, the answer is 5 if ("apple".compareto(str2) = = 0) System.out.println("equal strings"); System.out.println("unequal strings"); str1.compareto("pear") "apple".compareto(str2) str1.compareto(str2) define returns two returns string -132 0 since objects str1 str2 comes is also before after "apple" str2 "pear" and since and -13 2 since > < 0 0 "apple 0 "car = = 0 pear" car" "equal is printed strings" (lexicographic is printed order)
8. Given the code fragment shown, what will be printed? boolean answer = 6 < 10; System.out.println("The result is " + answer); answer is set equal to true answer: The result is true
9. Given the code fragment shown, what will be printed? int count = 3; if(count > 2) System.out.println("The result is " + count); System.out.println(0); count is assigned a value of 3 since count = 3 and 3 > 2 the condition is true... answer: The result is 3
10. Use DeMorgan s Laws to restate each of the following:!(a && b) is equivalent to!a!b!(a b) is equivalent to!a &&!b A!(number < 5 && string1.equals("harry")) number >= 5!string1.equals("Harry") B!(month = = 3 month = = 4) month!= 3 && month!= 4
11. Give an example of a short circuit evaluation and explain what happens when the expression is evaluated. if (str!= null && str.equals(str2))... First the expression str!= null is evaluated. If str!= null, then the expression str.equals(str2) is evaluated. But if str = = null, then the test evaluates to false and the second condition is not evaluated. Note that str.equals(str2) would result in a run-time error if str were equal to null.
12. If your income was $20,000 then your bracket should be the 15% bracket. What percent is returned by each method for this income of $20,000: Method I 0.28 Method II 0.15 Method III 0.28 Method III only method II worked for the 15% bracket if (income <= > 51900) 21450) bracket = 0.15; 0.31; test test true true false bracket undefined bracket = = 0.15 if (income if (income <= 51900) 21450) <= 21450) test test true true bracket bracket = = 0.28 0.15 bracket = 0.28; 0.15; not executed if (income > <= 51900) bracket still 0.15 test test false true bracket = 0.28 bracket = 0.31; 0.28; bracket still 0.28 return return bracket; return return 0.28 0.28 0.15
12. Continued... If your income was $40,000 then your bracket should be the 28% bracket. What percent is returned by each method for this income of $40,000: Method I 0.28 Method II 0.15 Method III 0.28 methods I and III worked for the 28% bracket Method II Method III if (income <= 21450) 51900) if (income > 51900) bracket = 0.15; bracket = 0.31; if (income if (income <= 51900) <= 21450) if (income <= 21450) bracket = 0.28; bracket = 0.15; if (income > 51900) if (income <= 51900) bracket = 0.31; bracket = 0.28; test false true test false bracket undefined = 0.15 bracket undefined not test executed true test false bracket = still 0.28 0.15 bracket still undefined not test executed false test true bracket still 0.28 0.15 return bracket; return 0.28 return bracket; 0.15 return 0.28 bracket = 0.28
12. Continued... If your income was $80,000 then your bracket should be the 31% bracket. What percent is returned by each method for this income of $80,000: Method I 0.31 Method II 0.31 Method III 0.31 all methods worked for the 31% bracket Method III Method II if (income <= > 51900) 21450) if (income <= 51900) bracket = 0.15; 0.31; bracket = 0.15; if (income <= 51900) 21450) if (income 0.28; <= 21450) bracket = 0.15; bracket > = 51900) 0.28; if (income <= 51900) bracket 0.31; bracket = = 0.28; 0.31; test false true test false bracket undefined = 0.31 bracket undefined test false test false bracket undefined still 0.31 bracket undefined test true false bracket = 0.31 bracket still 0.31 executed bracket = 0.31 return bracket; return bracket; return return 0.31 0.31
12. Summary... Method I worked for the 28% and 31% brackets. Method II worked for the 15% and 31% brackets. worked for the 28% and 31% brackets. Method III None of these methods worked all the time. proper style but needs to fixed up. Method II is the original Method II if (income <= 51900) bracket = 0.15; if (income <= 21450) bracket = 0.28; bracket = 0.31; return bracket; corrected Method II if (income > 51900) bracket = 0.31; if (income <= 21450) bracket = 0.15; bracket = 0.28; return bracket;
13. Refer to the code at the right. Scanner console = new Scanner(System.in); System.out.print("Enter a month number: "); int month = console.nextint( ); String str = ""; // empty string B Write if- code that would correctly implement the intent of the switch statement shown. switch(month) { case 9: case 4: case 6: case 11: str = "30 days"; case 2: str = "28 days"; default: str = "31 days"; } System.out.println("Number of days: " + str); A What is the output for the switch statement shown if the input number is 6? If you are not sure of your answer, test the code. if(month = = 9 month = = 4 month = = 6 month = = 11) str = "30 days"; if(month = = 2) Since str there = "28 are days"; no break statements, str is first set to "30 days" then changed to "28 days" then finally changed to "31 days". str = "31 days"; answer Number of days: 31 days