Computer Science 15-110 (Lecture 4), Spring 2010
Quiz 1 Practice


  1. Short Answers
  2. Graphics Short Answers
  3. Tracing
  4. Mystery Code
  5. Console Programs
  6. Graphics Programs

Remember:  You may not use Java constructs that we have not yet covered in class, including String methods, doubles, loops, and conditionals.


  1. Short Answers
    In the written portion of your submission, answer each of the following questions.  Be very brief, but show your work where appropriate.
    Unless stated otherwise, assume that "x" and "y" are non-negative integer values, and ignore any errors (such as dividing by zero):
     
    1. Assuming "x" and "y" are boolean values, fill in the blanks with one of “always”, “sometimes”, or “never”.
      1. (!!x)   ________________  equals x
      2. (x && !y)   ________________  equals (!x || !y)
      3. (x && !y)   ________________  equals !(x || !y)
      4. (x == !y)  _________________  equals ((!x && y) || (x && !y))
         
    2. In just a few words, what is short-circuit evaluation of boolean expressions?
       
    3. 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.
       
    4. 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.
       
    5. If (x % 2) is less than zero, then we know that x is ____________.
       
    6. True or False:   (x % y % x) always equals (x % y)?
       
    7. If (x/y) equals (y/x), then what must be true about x and y?
       
    8. Assuming that variables x, y, and z all hold non-negative integer values, under what conditions in general will the following line of code crash?
        int mystery = x / y / z - x / (y / z);
       
    9. For positive ints a, b, and c, if (a / b / c) equals (a / (b / c)), what must be true about c?
       
    10. What is overflow?  Write a few lines of Java that demonstrate overflow.
       

  2. Graphics Short Answers

    Sample exercise:  What will this code draw (answer both in words and with a sketch):
            page.drawRect(a,b,c,d);
            page.drawRect(a-10,b,c,d/2);

    Sample solution:  two rectangles, the second 10 pixels to the left of, and half as tall as, the first, like this:

    For clarity, it is preferable that you shade the original rectangle like this:

    1. [Rectangles] For each of the following, what will the code draw (answer both in words and with a sketch, and shade the original rectangle).

      1. page.drawRect(a,b,c,d);
        page.drawRect(a-10,b,c+10,d);
         

      2. page.drawRect(a,b,c,d);
        page.drawRect(a-10,b,c+20,d);
         

      3. page.drawRect(a,b,c,d);
        page.drawRect(a-e,b,c+(2*e),d);
         

      4. page.drawRect(a,b,c,d);
        page.drawRect(a-c,b,c,d);
         

      5. page.drawRect(a,b,c,d);
        page.drawRect(a,b+d,c,d)
         

      6. page.drawRect(a,b,c,d);
        page.drawRect(a+e,b+f,c-(2*e),d-(2*f))
         

      7. page.drawRect(a,b,c,d);
        page.drawRect(0,0,a+c/2,b);
         

    2. Write a few lines of code that draws each of the following (assume the rectangle's left-top point is at (a,b) and its width is c and height is d):

      1. An oval centered inside a rectangle.

      2. An oval centered inside a rectangle, but with 20 pixels of margin on each side.

      3. A 3x3 red-and-black checkerboard that fills the rectangle (the top-left square should be red).
         

    3. Explain how to draw the "mirror image" of a picture containing rectangles.  Provide a precise answer that clearly explains the process in general.  Using your technique, provide a mirror image of the flag of Benin.
       

    4. Explain how to "zoom" a picture containing rectangles, say to make it 2 times larger.  Demonstrate your technique by drawing a simple picture that draws (and does not fill!) a rectangle all in black, then redraws the zoomed version all in orange.
       

    5. Repeat the previous problem, but zoom out to make the orange picture 2 times smaller.
       

    6. Explain how to "pan" a picture containing rectangles, say to move it 10 pixels to the right and 20 pixels down.  Once again, demonstrate your technique by drawing a simple picture that draws (and does not fill!) a rectangle all in black, then redraws the panned version all in orange.
       

    7. Explain how to rotate a picture (containing rectangles) by 90 degrees counterclockwise (note that the right edge of the original picture will become the top edge of the rotated picture).  Using your technique, provide a rotated image of the flag of Benin.
       

  3. Tracing
    In the written portion of your submission, indicate what each of the following will print or (if it is graphical) draw.  Do not run these programs.  Figure this out by hand.  Remember to show your work.
     
    a)




     
      public static void main(String[] args) {
      int x = 17 % 6;
      int y = x - 8;
      x++;
      y--;
      int z = Math.max(y/x, Math.abs(x/y));
      System.out.println(x + "," + y +
                         "," + z);
      }
    b)



     

      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);
      }

    c)




     
    public static void main(String[] args) {
         int x = 10, y = 3;
         System.out.println(4 + x % y);
         System.out.println(99 / x);
         System.out.println(x / 99);
         System.out.println(99 % x);
         System.out.println(x % 99);
         System.out.println(7 % 7 / 6 % 6);
      }

     

  4. Mystery Code
    In the written portion of your submission, answer the following question in general, and in just a few words of plain English.

    What does each of these programs do (in general)?

    Hint:  while you should definitely trace the code in this problem by hand to make some sense out of it, you may also wish to actually test the code in a compiler with samples of your choosing, at least to verify that it works as you think it does!
     
    1.   public static boolean mysteryMethodB(int x, int y) {
          return (((x == 0) && (y == 0)) ||
                  ((x != 0) && (y != 0) && ((x/y) + (y/x) == 2)));
        }

      Hint:  there are several cases to consider!  What if x>y?  x==y?  x<y?
       
  5. Console Programs
    1. Console Programs in the Class Notes
      Be sure you can very easily and quickly write any of the console programs we have already covered in the class notes (in class, in recitation, in hw practice, or in assigned hw).  These include:
      1. triangleArea
      2. onesDigit
      3. eggCartons
      4. hundredsDigit
      5. nearestBusStop
      6. lineIntersection
      7. thousandsDigit
      8. numberOfPoolBalls
      9. buyingFabric
      10. square
      11. printSquare
      12. isPositive
      13. areaUnderLine
      14. isEvenPositive
      15. minSquared and testMinSquared
      16. isOdd and testIsOdd
      17. enoughEggCartons
      18. reverseNumber
      19. isPalindromeNumber
         
    2. More Console Programs
      These programs appeared in previous semesters' quizzes, hw's, or notes.  Be sure you can easily and quickly write any of these, too!
      1. maxOf3
      2. medianOf3
      3. medianOf4
      4. isPerfectSquare
      5. isPerfectCube
      6. letterGrade
      7. dayOfWeek
      8. printTime (see #5)
      9. duplicateDetector
      10. sumFromZeroToN (see #2)
      11. xor
      12. modlessRemainder
      13. xIntercept
      14. celsiusToFahrenheit (see #3)
      15. makingChange (see #20-22)
      16. feetAndInches (see #16)
      17. nonZeroDigits (see bonus #5)
      18. sumOfOddDigits (see bonus #6)
         
  6. Graphics Programs
    1. Graphics Programs in the Class Notes
      Be sure you can very easily and quickly write any of the graphics programs we have already covered in the class notes (in class, in recitation, in hw practice, or in assigned hw).  These include:
      1. Center a 200x100 blue rectangle in the window
      2. Center a yellow oval inside the blue rectangle with the given "margin"
      3. Flag of Mali
      4. Flag of Ghana
      5. Flag of Belgium
      6. Flag of Burkina Faso
      7. Flag of the Central African Republic
      8. Flag of the Netherlands Antilles
      9. Flag of Mauritania
         
    2. More Graphics Programs
      These programs appeared in previous semesters' quizzes, hw's, or notes.  Be sure you can easily and quickly write any of these, too!
      1. Flag of Benin (see #5.1)
      2. Flag of Panama (see #5.2)
      3. Flag of Romania
      4. Flag of Azerbaijan
      5. Flag of Tonga
      6. Flag of Togo
      7. Flag of Columbia (see #3)

Carpe diem!