15-100 Sections U-V-AA / Spring 2009 / Quiz 0
15 Minutes
Pass/Fail (this quiz only!)

SHOW YOUR WORK!


1.     Mystery Code / Arithmetic
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);

2.     Sum from 0 to n
Assuming that the variable n holds a non-negative integer value, write a code snippet that prints out the sum of the integers from 0 to n.  For example, if n equals 3, your code should print out 6 because 0 + 1 + 2 + 3 = 6.

Note:  To solve this, you may not use loops (even if you know how!).  Instead, you should use GaussÕs  Formula:  0 + 1 + 2 + É + n = (1/2)n2 + (1/2)n

To verify, if n equals 3, GaussÕs Formula says:
    0 + 1 + 2 + 3 = (1/2)32 + (1/2)3 = 9/2 + 3/2 = 12/2 = 6.  Yep.

Hint: be sure to properly handle integer division and truncation

3.     Flag of Colombia
Write the body of a paint method for a program that displays the flag of Colombia, using built-in colors and entirely filling the window, even when the window is resized:
  (Flag of Colombia Ð Yellow stripe on top half; blue and red stripes on bottom half)


  public void paint(Graphics page) {
  }

4.     Bonus/Optional:

a) For positive ints a, b, and c, if (a / b / c) equals (a / (b / c)), what must be true about c?  Prove this (briefly).

b)  For positive ints x and y, where y>1, under what conditions will ((x Ð 1) % y) / (y Ð 1)  be non-zero?