30 Questions (with solutions)ΒΆ

  1. How many bits are in a byte?

    Solution: 8

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

    Solution: It marks the end of a statement.

  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?

    Solution: (0, 199)

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

    Solution: quad connects four points as inputs and connects them with lines. A quad could be any four-sided shape, including squares, rectangles, trapezoids, and bow ties. In contrast, a rect only draws rectangles whose sides are parallel to the axes of the coordinate system.

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

    Solution: (0, 0, 255, 127) (127 or 128 is okay here)

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

    Solution: 0.08

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

    Solution: PImage

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

    Solution: filter, e.g. filter(THRESHOLD, 0.5)

  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
    

    Solution: Literals: 5, 7. Variables: a, b, temp

  10. Suppose that n is a Processing variable of type int. Write four different statements that add 1 to n.

    Solution: n += 1, ++n, n++, n = n + 1

  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.

    Solution:

    if (y >= height) {
        dy = -dy;
    }
    
  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.

    Solution: (5.0 < x && x < 10.0) && (5.0 < y && y < 10.0)

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

    Solution: n is the maximum possible int, i.e. \(2^{32}-1 = 2147483647\)

  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?

    Solution: keyPressed, mousePressed (mouseClicked and mouseReleased are also acceptable answers for the mouse clicks)

  15. What, exactly, does this print?

    println("a\nb" + 3 + "2".length());
    

    Solution:

    a
    b31
    
  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)
    }
    

    Solution: This code causes a run-time error, specifically a null- pointer exception. The error is due to the fact that s has the value null, and so the expression s.x causes an a null-pointer exception.

  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.

    Solution:

    //
    // This is one possible solution. There are others.
    //
    void hello(String name) {
        println("Hello " + name + "!")
    }
    
  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.

    Solution:

    //
    // This is one possible solution. There are others.
    //
    boolean inCircle(float a, float b,
                     float centerX, float centerY,
                     float radius)
    {
      return dist(a, b, centerX, centerY) < radius;
    }
    
  19. What are the conditions for the mouse to be dragged?

    Solution: The mouse must be moving, and, at the same time, at least one button on it is pressed.

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

    Solution: It sets the origin of the coordinate system to be the point (200, 150) (in the current coordinate system).

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

    Solution: pushMatrix() saves the current state of the coordinate system (i.e. the location of its origin and how much it is rotated), while popMatrix() restores the coordinate system to be state it was in just before the most recent call to pushMatrix.

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

    Solution: Sprite is the super class of Ball, and Ball is a subclass of Sprite.

  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.

    Solution:

    class Point {
        float x, y;
    
        Point() {  // default constructor
            x = 0;
            y = 0;
        }
    
        Point(Point other) {  // copy constructor
            x = other.x;
            y = other.y;
        }
    }
    
  24. Write a fragment of code that creates a new ArrayList of strings that contains “apple”, “orange”, and “cherry”.

    Solution:

    ArrayList<String> fruit = new ArrayList<String>();
    fruit.add("apple");
    fruit.add("orange");
    fruit.add("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
    

    Solution:

    //
    // This is one possible solution. There are others.
    //
    int i = 5000;
    while (i >= 0) {
        println(i);
        i -= 5;
    }
    
  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.

    Solution:

    particles = new ArrayList<Particle>();
    
  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).

    Solution:

    int rand(int hi) {
        return rand(0, hi);
    }
    
  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.

    Solution:

    t.forward(100)
    t.right(120)
    t.forward(100)
    t.right(120)
    t.forward(100)
    t.right(120)  // this line is optional
    
  29. What is a recursive function?

    Solution: A function that calls itself.

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

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

    Solution:

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