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
quadfunction and itsrectfunction?Solution:
quadconnects four points as inputs and connects them with lines. Aquadcould be any four-sided shape, including squares, rectangles, trapezoids, and bow ties. In contrast, arectonly 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:
PImageWhat 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,tempSuppose that
nis a Processing variable of typeint. Write four different statements that add 1 ton.Solution:
n += 1,++n,n++,n = n + 1Suppose that an animated ball uses
xandyto represent its center, anddxanddyto 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
xandyare variables of typefloat. Write a logical expression that evaluates totruejust when bothxandyare between 5.0 and 10.0, andfalseotherwise. Your expression should returnfalseif eitherxoryis equal to either 5.0 or 10.0.Solution:
(5.0 < x && x < 10.0) && (5.0 < y && y < 10.0)Suppose
nis anint, andn + 1 < 0. What is the value ofn?Solution:
nis 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(mouseClickedandmouseReleasedare 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
shas the valuenull, and so the expressions.xcauses an a null-pointer exception.Write a
voidfunction 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
booleanfunction calledinCircle(a, b, centerX, centerY, radius)that returnstrueif 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:
Spriteis the super class ofBall, andBallis a subclass ofSprite.Write a class called
Pointthat represents the (x,y) by storingxandyasfloats. 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
ArrayListof 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
particleshas just been created by this statement:ArrayList<Particle> particles;
Write a fragment of code that makes
particlesrefer to a newArrayList<Particle>of size 0.Solution:
particles = new ArrayList<Particle>();
Suppose the function
rand(lo, hi)returns a randomintin the rangelotohi(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
tis a turtle graphics object, andt.forward(n)makes the turtle move forwardnpixels (drawing a line), andt.right(d)makes the turtle rotate to the right (i.e. clockwise)ddegrees in place (without drawing a line).Write a fragment of code using
tthat 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); }