CSCI A201/A597

Takehome Quiz Two

Second Summer 2000


Second set of questions due Tuesday June 27, on paper. Answers will be posted Wednesday at noon.

Note: should you find any typos please check with the book, as mentioned in class these questions are a selection of the questions in the book (pp. 92-94).

Questions:

  1. Write the following mathematical expressions in Java.




  2. Write the following Java expressions in mathematical notation.
    dm = m * ((Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c)) - 1);
    volume = Math.PI * r * r * h;
    volume = 4 * Math.PI * Math.pow(r, 3) / 3; 

  3. What is wrong with this version of the quadratic formula?
    x1 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a; 
    x2 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;

  4. Give an example of integer overflow. Would the same example work correctly if you used floating-point? Give an example of a floating-point roundoff error. Would the same example work correctly if you used integers? When using integers, you would of course need to switch to a smaller unit, such as cents instead of dollars or milliliters instead of liters.

  5. Let n be an integer and x a floating-point number. Explain the difference between
    n = (int)x;
    and
    n = (int)Math.round(x);
    For what values of x do they give the same result? For what values of x do they give different results? What happens if x is negative?

  6. Find at least five syntax errors in the following program.
    public class WarmUpSix
    { public static void main(String[] args) 
      { System.out.print("This program adds two numbers.),
        x = 5; 
        int y = 3.5;
        System.out.print("The sum of " + x + " and " + y " is: "); 
        System.out.println(x + y)
      }
    }

  7. Find at least three logic errors in the following program.
    public class WarmUpSeven 
    { public static void main(String[] args) 
      { ConsoleReader console = new ConsoleReader(System.in);
        int total = 1; 
        System.out.println("Please enter a number:"); 
        int x1 = Integer.parseInt(console.readLine()); 
        total = total + x1; 
        System.out.println("Please enter another number:"); 
        int x2 = Integer.parseInt(console.readLine()); 
        total = total + x1; 
        double average = total / 2;
        System.out.println("The average of two numbers is " 
          + average); 
      }
    }

  8. Explain the difference between 2, 2.0, "2", and "2.0".

  9. Explain what each of the following two program segments computes:
    x = 2; 
    y = x + x;
    and
    s = "2";
    t = s + s; 

  10. Uninitialized variables can be a serious problem. Should you always initialize every variable with zero? Explain the advantages and disadvantages of such a strategy.

  11. True or false? (x is an int and s is a String)

  12. Give two ways for converting a number to a string. What is the advantage of each of these ways?

  13. How do you get the first character of a string? The last character? How do you remove the first character? The last character?

  14. How do you get the last digit of a number? The first digit? That is, if n is 23456, how do you find out 2 and 6? Do not convert the number to a string. Hint: %, Math.log

  15. What is a final variable? Can you define a final variable without supplying its value?

  16. What are the values of the following expressions? In each line, assume that
    double x = 2.5;
    double y = -1.5;
    int m = 18;
    int n = 4;
    String s = "Hello";
    String t = "World"; 


Last updated: June 22, 2000 by Adrian German for A201