CSE 131 Module 1: Methods

Practice Problems


Important note: Most CSE131 modules will have practice problems section with a quiz. However, do not wait until after the quiz to begin working on the project. You should go a head and start working on the project as soon as you've finished and understood the practice problems.

The purpose of the practice problems is to help you check your understanding of material covered in class. You do not need to turn in your solutions, but should do the practice problems in preparation for the quiz. Solutions to selected exercises are available, but you will benefit most by doing the exercises before you look at the solutions.

You will learn more if you do the problems before attending a help session.

  1. Arithmetic Expressions: Draw an expression tree for each of the following Java expressions. Provide the value of each expression. Remember that Java computes left to right, and that negation takes precedence over multiplication and division, which take precedence over addition and subtraction.
  2. a.  (50 - 5 * 4) / 10 - 7
    
    b.  12 + -5 * 2 + 6 / 3
    
    c.  1 + 2 + 3 + 4 + 5 + 1 * 2 * 3 * 4 * 5
  3. Expressions Containing Variables: Draw an expression tree for each of the following Java expressions. Note that each variable is represented here by a single character.
  4. a.  (-b - Math.sqrt(b * b - 4*a*c)) / (2*a)
    
    b.  a + (b * c) - ((d * f) / e) - g
    
    c.  a + b * c - d * f / e - g
  5. String Expressions: Provide the string value resulting from evaluation of each expression, or if the expression would result in an error, explain the error.
  6. a.  "this " + "string " + "has " + "spaces "
    
    b.  "this" + "string" + "has" + "no" + "spaces"
    
    c.  "the sum of " + 6 + " + " + 10 + " is " + (6 + 10)
    
    d.  "the sum of " + "6 + 10" + " is " + (6 + 10)
    
    e.  6 + 10 + " equals " + (6 + 10)
    
    f.  "" + 6 + "+" + 10 + " equals " + 6 + 10
  7. Boolean Expressions: Provide the boolean value (true or false) resulting from evaluation of each expression. (Remember that "&&" represents AND, "||" represents OR, and "!" represents NOT.)
  8. a.  3 > 7
    
    b.  (3 > 7) || (4 < 9)
    
    c.  135 == 100 + 35
    
    d.  (true && false) || (true || false)
    
    e.  (3 > 7) || ((10 < 9) == (3 == 8))
    
    f.  false || !(3 > 7)
  9. Data Type Identification: Identify the type (int, double, boolean, or String) of the value that would result from evaluating each Java expression, or if the expression would result in an error, explain the error. If there is no error, provide the value of the expression as well as the type.
  10. a. 7 / 2
    
    b. 7.0 / 2
    
    c. ((7 / 2) == (7.0 / 2))
    
    d. "x = " + 6
    
    e. 6 + " = x"
    
    f. ((6 * 3) / 80) / (35 - 7 * 5)
    
    g. 1 / 2
  11. Writing Java Expressions for Arithmetic Formulae: Write Java expressions for each of the following arithmetic formulae. In these expressions, use variable names r for radius, w for width, and h for height.
  12. a. the area of a circle with radius r
    
    b. the circumference of a circle with radius r
    
    c. the perimeter of a rectangle with height h and width w
    
    d. the volume of a cylinder with radius r and height h
    
    e. the length of the diagonal of a rectangle with height h
    
       and width w
    
    f. the area of a right triangle with height h and width w
  13. A truth table is a table that shows the value of a boolean expression for all possible assignments to the variables of that expression. Each row of the table corresponds to a possible assignment of the variables and the resulting value of the expression.  Complete the following truth table for the logical operators AND (&&) and OR (||). (Fill in the missing values on each line of the table.)
  14. x y x AND y x OR y
    true true
    true false true
    false true
    false false
     

Solutions