Java: Loop Quiz

Name ___________________________

OPEN BOOK. g is a Graphics object.

  1. (2 points) Draw the output from the following code:
    for (int i=10; i<=30; i += 10) {
        g.drawRect(i, i, i, i);
    }
    
  2. (3 points) Draw the output from the following code:
    int y = 20;
    int n = 3;
    while (n > 1) {
        g.drawString("n = " + n, 20, y);
        y += 20;
        if (n%2 == 0) {
            n = n / 2;
        }else{
            n = 3*n + 1;
        }
    }
    
  3. (4 points) Draw the output from the following code:
    boolean fill = true;
    for (int col=1; col<=5; col++) {
        fill = (col%2 == 1);
        for (int row=1; row<=4; row++) {
            if (fill) {
                g.fillOval(col*10, row*10, 5, 5);
            }else{
                g.drawOval(col*10, row*10, 5, 5);
            }
            fill = !fill;
        }
    }
    
  4. (4 points) Assume the following value for s before each of the independent questions.
        s = "buenas";
        

    What is the value in s after executing the following code?

    1. __________________ s = s.substring(0,1).toUpperCase() + s.substring(1, s.length()-1);
    2. __________________ s = "" + Character.toUpperCase(s.charAt(3));
    3. __________________ s = s.substring(1).substring(1);
    4. __________________ s.toUpperCase(); // Warning: this is a trick question.