Upload
paul-brebner
View
131
Download
0
Embed Size (px)
Citation preview
Introduction to Programming Quiz 2
1 My name is:
2 In Processing (and Java) there are several different ways of repeating commands (loops).
Are the following “while” and “for” loops are the same: True or False
// a “while” loop
int i = 0;
while (i < 10)
{
ellipse(10,10,i,i);
i++;
}
// a “for” loop
for (int i=0; i < 10; i++)
ellipse(10,10,i,i);
3 mousePressed is a global Boolean variable which has the value true after any mouse button is
pressed. Do these two code examples do the same thing? True or False
// example 1
if (mousePressed)
println(“Kaboom!”);
// example 2
if (mousePressed == true)
println(“Kaboom!”);
4 Remember that the test for equality is “==” (used in example 2 in Qn 3); and assignment (setting
the value of a variable) is “=” (used in example 3 below). Does example 3 do the same as
example 1 above? True or False (Hint: Can mousePressed ever be false?)
// example 3
if (mousePressed = true)
Println(“Kaboom!”);
5 Image file formats. Which of the following is not an image file format?
a. PNG
b. JPEG
c. SVG
d. WWW
e. GIF
6 Which image file format above can compress large photo files the most (i.e. makes them
smallest?)
7 Which image file format above smoothly scales Vector Graphics (shapes and lines) up and down
to any size?
8 The internet (networked computers allowing many different programs to communicate) has
been around since 1960s? 1970s? 1980s? 1990s?
9 The World Wide Web (WWW) was invented in the 1990s? True of False
10 Which of the following protocols and programs are parts of the World Wide Web (WWW)? Circle
them:
a. URLs (user friendly internet addresses)
b. Domain Name Servers (DNS) – a “phonebook” for URLs
c. HyperText Markup Language (HTML) - used to make web pages
d. HyperText Transfer Protocol (HTTP) - so web browsers and servers can communicate
e. Web Servers
f. Web Browsers (e.g. Chrome, Firefox, etc).
11 Complete the following Truth Table (fill in the gaps) for the Logical Operation:
a. NOT (true if p is false, false if p is true). T is True, F is False:
p NOT p
T F
F
12 Complete the following Truth Table for the Logical Operation:
a. AND (true if both p and q are true). T is True, F is False:
p q p AND q
T T
T F F
F T
F F
13 Complete the following Truth Table for the Logical Operation:
a. NAND (NOT AND). false if both p and q are true, else true.
b. Hint: Just the same as NOT (p AND q):
p q p NAND q
T T
T F T
F T
F F
14 In Processing, which of these Logical Operators are: AND, OR, NOT?
|| ________?
! ________?
&& ________?
15 In the following Processing example, what value is dontdrinkanddrive? true or false
// remember, boolean variables are either true or false
boolean drink = true;
boolean drive = true;
boolean dontdrinkanddrive = !(drink && drive);
16 In the following Processing example, what value is dontdrinkanddrive? true or false
// remember, “||” means one or more are true
boolean beer = false;
boolean wine = false;
boolean spirits = false;
// is drink true?
boolean drink = (beer || wine || spirits);
boolean bike = true;
boolean car = false;
boolean truck = false;
// is drive true?
boolean drive = (bike || car || truck);
boolean dontdrinkanddrive = !(drink && drive);
17 Are these two “signs” the same (assuming that a value of true is obeying the sign):
Don’t Drink and Drive
Drink NAND Drive
18 Can you work out what this program does? What happens when the ball hits the edges of
the screen? Does it wrap around or bounce? There is a bug! The ball vanishes off the top of the
screen. How can you fix it?
float diameter = 40;
float radius = diameter/2;
float x;
float y;
float xspeed = 5;
float yspeed = 7;
void setup() {
size(500, 500);
x = width/2;
y = height/2;
}
void draw() {
background(0);
x += xspeed;
y += yspeed;
if ( (x + radius) > width || (x - radius) < 0 )
xspeed = -xspeed;
if ( (y + radius) > height )
yspeed = -yspeed;
ellipse(x, y, diameter, diameter);
}