30 QuestionsΒΆ

Solutions are here.

  1. How many bits are in a byte?

  2. What is the purpose of a ; in a Processing program?

  3. What, exactly, are the coordinates of the lower-left most pixel on a Processing screen that is 300 pixels wide and 200 pixels high?

  4. What is the difference between Processings quad function and its rect function?

  5. What is the RGB triple for the color (pure) blue that is 50% transparent?

  6. What does map(8, 0, 100, 0, 1) evaluate to?

  7. What is the name of the type of an image variable in Processing?

  8. What is the name of the Processing function you would use to convert an image to, for instance, black and white?

  9. In the following code fragment, make a list of all the literals, and then make a list of all the variables.

    int a = 5;
    int b = 7;
    
    int temp = a;
    a = b;
    b = temp;
    
    println(a);  // prints 7
    println(b);  // prints 5
    
  10. Suppose that n is a Processing variable of type int. Write four different statements that add 1 to n.

  11. Suppose that an animated ball uses x and y to represent its center, and dx and dy to represent its velocity. Write an if- statement that makes the ball reverse its y direction when its center hits the bottom of the screen.

  12. Suppose x and y are variables of type float. Write a logical expression that evaluates to true just when both x and y are between 5.0 and 10.0, and false otherwise. Your expression should return false if either x or y is equal to either 5.0 or 10.0.

  13. Suppose n is an int, and n + 1 < 0. What is the value of n?

  14. What is the name of the function that you use to run some code when the user presses a key? What is the name of the that you use to run some code when the user clicks a mouse button?

  15. What, exactly, does this print?

    println("a\nb" + 3 + "2".length());
    
  16. What does this code fragment print?

    class Sprite {
      float x;
      float y;
      float dx;
      float dy;
    }
    
    void setup() {
        Sprite s;
        println(s.x + s.y + s.dx + s.dy)
    }
    
  17. Write a void function that takes a name as input (as a String) and then prints “Hello <name>!” on the console window. Of course, <name> should be the name passed into the function. Don’t forget the ”!” at the end.

  18. Write a boolean function called inCircle(a, b, centerX, centerY, radius) that returns true if the point (a, b) is in the circle whose center is (centerX, centerY) and radius is radius.

  19. What are the conditions for the mouse to be dragged?

  20. What does the function translate(200, 150) do?

  21. What do pushMatrix() and popMatrix() do?

  22. In the following code, what is the super class and what is the subclass?

    class Sprite {
      float x;
      float y;
      float dx;
      float dy;
    
      void update() {
        x += dx;
        y += dy;
      }
    }
    
    class Ball extends Sprite {
      float radius;
      color fillColor;
    }
    
  23. Write a class called Point that represents the (x, y) by storing x and y as floats. Include a sensible default constructor, and a sensible copy constructor.

  24. Write a fragment of code that creates a new ArrayList of strings that contains “apple”, “orange”, and “cherry”.

  25. Write a fragment of code that uses a while-loop to print the numbers 5000 down to 0 by 5s, e.g.:

    5000
    4995
    4990
    4985
    ...
    10
    5
    0
    
  26. Suppose particles has just been created by this statement:

    ArrayList<Particle> particles;
    

    Write a fragment of code that makes particles refer to a new ArrayList<Particle> of size 0.

  27. Suppose the function rand(lo, hi) returns a random int in the range lo to hi (inclusive). For example, rand(1, 3) returns either 1, 2, or 3 at random.

    Using rand(lo, hi), write a new function called rand(hi) that returns a random number in the range 0 to hi (inclusive).

  28. Suppose t is a turtle graphics object, and t.forward(n) makes the turtle move forward n pixels (drawing a line), and t.right(d) makes the turtle rotate to the right (i.e. clockwise) d degrees in place (without drawing a line).

    Write a fragment of code using t that draws an equilateral triangle, i.e. a triangle whose sides are all the same length.

  29. What is a recursive function?

  30. Re-write the following code using a while-loop:

    int i = 5;
    while (i < 100) {
        println(i * i);
        i += 2;
    }