15-110 Sections M-Q / Fall 2009 / Quiz 1   (version A)
5 Parts / 25 Minutes
 

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

·         All code examples compile and run without errors (except where explicitly noted otherwise in Part 5).

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

 

1a.  (10 pts) Writing Code
Assuming the variable x holds a non-negative integer value between 1 and 99 inclusive, write one line of code that declares and initializes the integer y to hold the reverse of that number (the same digits, but in the opposite order).  For example, if x is 32, then y should be set to 23.  If x is 8, this is the same as 08, so y should be set to 80.  And if x is 80, then y should be set to 8.

Answer #1a

 



 

1b.  (10 pts) Writing Code
Assuming the variables x and y both hold non-negative (but possiby zero) integers, write one line of code that declares and initializes a boolean b to be true if the remainder when x is divided by y equals 3, and false otherwise.  Your code must not crash in any case.

Answer #1b

 

 

 

2.  (20 pts) Tracing
Indicate what the following will print.
 
public static void main(String[] args) {
   int x = 10, y = 3;

   System.out.println(99/x + 99.0/x);

   System.out.println(x/99);

   System.out.println(4+99%x);

   x++;

   y*=2;

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

 

Answer #2



 

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

   int x = 10;
   double d = x;
   int z = 1000*1000*1000; // 1 billion
   System.out.println(z+z+z > z);
   System.out.println("x+d=" + x +
                      Math.pow(d,2));
   String s = "ab\nc\td";
   System.out.println(s + s.length());
   char c = "ABC".charAt(1);
   x = c;
   System.out.println(x + "," + c);

  }

 

Answer #3


 

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

 public static boolean f(String s) {
    char c = s.charAt(0);
    return (((c >= 'a') && (c <= 'z')) ||
            ((c >= 'A') && (c <= 'Z')));
 }

Answer #4a

 

 

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

  public static boolean g(int x, int y) {

    assert((x >= 0) && (y >= 0));

    double d1 = Math.sqrt(x);

    double d2 = Math.pow(y, 2);

    return (Math.abs(d1 - d2) < 0.000001);

  }

Answer #4b

 

 

5.  (20 pts) Short Answers (Answer in 5 words or less.)
5a.  State the line # and cause of the compile-time error in this code:
1:  public static void main(String[] args) {

2:    int x = Integer.parseInt("uh oh");

3:    double d = x;

4:    char c = 'a'+x;

5:    String s = d + "" + c;

6:  }

5b.  State the line # and cause of the runtime error in this code:
1:  public static void main(String[] args) {

2:    int x = 5, y = 11;

3:    String s = "ack";

4:    boolean b = ((x > y/2.0) &&

5:                 (x/Integer.parseInt(s)==3));

6:    x %= x/y;

7:    System.out.println("x = " + x); 

8:  }


5c. 
State the line # and cause of the logical error in this code:
1:  public static void main(String[] args) {

2:    int root = (int)Math.pow(9, 1/2);

3:    System.out.println("Square root of 9 = " +
4:                       root);

5:  }


5d.  True/False:  When Java programs overflow, they crash with an OverflowException.


5e.  True/False:  Variables of any primitive type can never be assigned the value null.

5f.  True/False:  The zero-length char constant, '', has Unicode value 0.

 

5g  For y>0, if (x%y) > x, then we know that x __________.

Answer #5

5a.







5b.

 

 

 

 

 

 

 

 

5c.

 

 

 

 

 

 

5d.

 

5e.

 

5f.

 

5g.