chapter5Exercises

chapter5Exercises - 5. Self Check 5.4 Which Comparisons are...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 5 questions 1. Find the errors in the following if statements: a. if quarters> 0 the System.out.println( quarters + “ = quarters”); b. if (x & y == 0) { x = 1; y = 7;} c. if ( 1 <= x <= 10) System.out,println(x ); d. if ( input.equalsIgnoreCase(“N”) || “NO”) return; 2. Self Check 5.2 What is Logically Wrong with this if Statement? What is logically wrong with the statement if (amount <= balance) newBalance = balance - amount; balance = newBalance; and how do you fix it? 3. Self Check 5.3 What is the value of s.length() if s is a. the empty string “”? b. the string “ ” containing a space? c. null ? 4. Explain the difference between s = 0; if ( x > 0 ) s++; if (y > 0) s++; and s = 0; if( x > 0 ) s++; else if (y > 0) s++;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5. Self Check 5.4 Which Comparisons are Syntactically Incorrect? Logically Questionable? Which of the following comparisons are syntactically incorrect? Which of them are syntactically correct, but logically questionable? String a = &amp;quot;1&amp;quot;; String b = &amp;quot;one&amp;quot;; double x = 1; double y = 3 * (1.0 / 3); a. a == 1 b. a == null c. a.equals() d. a == b e. a == x f. x == y g. x - y == null h. x.equals(y) 6. Self Check 5.8 When does the statement System.out.println(x &amp;gt; 0 || x &amp;lt; 0); print false? 7. Self Check 5.9 Rewrite the following expression, avoiding the comparison with false : if (Character.isDigit(ch) == false) . . . 8. Explain the difference between r == s and r.equals(s) when r and s are strings....
View Full Document

Page1 / 3

chapter5Exercises - 5. Self Check 5.4 Which Comparisons are...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online