15-110 Sections M-Q / Fall 2009 / Quiz 2   (version A)
30 Minutes

 

·         Only write your final answer in the answer column.  Place scratch work outside the answer boxes.

·         You may not use conditionals or loops or String methods except length() and charAt().

·         All code examples compile and run without errors.

·         Unicode ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.

 

1.  (10 pts) Writing Code
Assuming the variables x and y hold int values, write one line of code that declares and initializes a boolean b to true if the ratio of x over y is a good approximation of π, and false otherwise.  You may use 3.14 as the value of π, and “good” means within 0.01 (inclusive) of that.  For example, if x is 313 and y is 100, the ratio is 3.13, and so b should be true.  Your code may not crash in any case.

Answer #1




 

 


2.  (10 pts) Writing Code
Write a method that takes one parameter, a Graphics2D “page”, and paints an unfilled cobalt oval that just fits in the top-right quadrant of the window.  The RGB values for cobalt are 0, 71, and 171.

Answer #2









 

 

3.  (20 pts) Tracing
Indicate what the following will print.
public static void main(String[] args) {

  int x = 11, y = 6;

  System.out.println("result=" + 9/y + 9.0/y);

  System.out.println(x%y+y%x);

  System.out.println(x%y*y%x);

  x%=y;

  y++;

  System.out.println(x + "," + y);

}

 

Answer #3

 

4.  (10 pts) Mystery Method
State, in 10 words or less, what this method does in general.  You may ignore cases where the method might crash.

public static boolean g(int x) {

  return (x/10 == x%10);

}

Answer #4

 

 

5.  (10 pts) Mystery Method
State, in 10 words or less, what this method does in general.  You may ignore cases where the method might crash.
public static boolean h(char c) {

  String score = "Cubs 10, Pirates 9";

  char c1 = score.charAt(6);

  char c2 = score.charAt(score.length()-1);

  return ((c < c1) || (c > c2));

}

Answer #5

 

6.  (20 pts) Tracing
Indicate what the following will print.
public static void main(String[] args) {

  int x = 10;

  double d = x;

  int z = 3*1000*1000*1000; // 3 billion

  System.out.println(z<z/2);

  System.out.println(Math.pow(Math.sqrt(17),

                              x/4));

  String s = "a\"\t\\b\n\td";

  System.out.println(s.length() + s);

  char c = "Cat".charAt(1);

  System.out.println(c + ("c+1") + (c+1));

}

 

Answer #6


7.  (10 pts) De Morgan’s Law
State one of De Morgan’s Laws, and then prove it using truth tables.  You must use appropriate helper columns.

Answer #7









 

 

8.  (10 pts) Div and Mod
Sketch each graph for integer values of x in [-10,+10].
                               
8a.     f(x) = (3+x)/4  (integer division)



                                       
8b.    g(x) = (3-x)%4

Answer #8a:

Answer #8b:

 

       

 

 

9. Bonus/Optional:  (5 pts) Mystery Method
State, in 10 words or less, what this method does in general.  You may ignore cases where the method might crash.

  public static int mysteryBonus(int n) {

    assert(n > 0);

    int x = 2;

    int answer = 0;

    int counter;

    for (counter=1; counter<=n; counter++) {

      if ((counter+1)/x == (counter+1)%x) {

        x*=2;

        answer++;

      }

    }

    return answer;

  }


 

Answer #9 (Bonus)