CS 101 (Fall 2002)
Quiz 1:Expressions

Quiz Posted
(Thursdays)
Given in class
(Thursdays)
5 Sep 12 Sep

Information about quizzes:

The questions are intended to be straightforward. If you keep up with the material in the class, almost no preparation may be necessary. The Collaboration Policy allows you to study in groups for the quizzes, but you are on your own when you take the quiz.

You will fare better on the quiz if you try working the problems before looking at the solutions. If you don't understand the question or its answer, please get help.

  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 hypotenuse of a rectangle with height h
       and width w
    f. the area of a right triangle with height h and width w
     

[ solution ]


Last modified 07:50:30 CST 13 January 1999 by Ron K. Cytron