30 Questions (with solutions)ΒΆ
How many bits are in a byte?
Solution: 8
What is the purpose of a ; in a Processing program?
Solution: It marks the end of a statement.
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)
What is the difference between Processings
quad
function and itsrect
function?Solution:
quad
connects four points as inputs and connects them with lines. Aquad
could be any four-sided shape, including squares, rectangles, trapezoids, and bow ties. In contrast, arect
only draws rectangles whose sides are parallel to the axes of the coordinate system.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)
What does
map(8, 0, 100, 0, 1)
evaluate to?Solution: 0.08
What is the name of the type of an image variable in Processing?
Solution:
PImage
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)
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
Suppose that
n
is a Processing variable of typeint
. Write four different statements that add 1 ton
.Solution:
n += 1
,++n
,n++
,n = n + 1
Suppose that an animated ball uses
x
andy
to represent its center, anddx
anddy
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; }
Suppose
x
andy
are variables of typefloat
. Write a logical expression that evaluates totrue
just when bothx
andy
are between 5.0 and 10.0, andfalse
otherwise. Your expression should returnfalse
if eitherx
ory
is equal to either 5.0 or 10.0.Solution:
(5.0 < x && x < 10.0) && (5.0 < y && y < 10.0)
Suppose
n
is anint
, andn + 1 < 0
. What is the value ofn
?Solution:
n
is the maximum possibleint
, i.e. \(2^{32}-1 = 2147483647\)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
andmouseReleased
are also acceptable answers for the mouse clicks)What, exactly, does this print?
println("a\nb" + 3 + "2".length());
Solution:
a b31
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 valuenull
, and so the expressions.x
causes an a null-pointer exception.Write a
void
function that takes a name as input (as aString
) 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 + "!") }
Write a
boolean
function calledinCircle(a, b, centerX, centerY, radius)
that returnstrue
if the point (a
,b
) is in the circle whose center is (centerX
,centerY
) and radius isradius
.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; }
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.
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).
What do
pushMatrix()
andpopMatrix()
do?Solution:
pushMatrix()
saves the current state of the coordinate system (i.e. the location of its origin and how much it is rotated), whilepopMatrix()
restores the coordinate system to be state it was in just before the most recent call topushMatrix
.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 ofBall
, andBall
is a subclass ofSprite
.Write a class called
Point
that represents the (x
,y
) by storingx
andy
asfloat
s. 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; } }
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");
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; }
Suppose
particles
has just been created by this statement:ArrayList<Particle> particles;
Write a fragment of code that makes
particles
refer to a newArrayList<Particle>
of size 0.Solution:
particles = new ArrayList<Particle>();
Suppose the function
rand(lo, hi)
returns a randomint
in the rangelo
tohi
(inclusive). For example,rand(1, 3)
returns either 1, 2, or 3 at random.Using
rand(lo, hi)
, write a new function calledrand(hi)
that returns a random number in the range 0 tohi
(inclusive).Solution:
int rand(int hi) { return rand(0, hi); }
Suppose
t
is a turtle graphics object, andt.forward(n)
makes the turtle move forwardn
pixels (drawing a line), andt.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
What is a recursive function?
Solution: A function that calls itself.
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); }