Uploaded by liu20051026

CS105 Final Exam F17 v07 ANSWERS.pdf

advertisement
CS105 F’17 Final Exam
University of Waterloo
Instructor: Kevin Harrigan
Thursday, December 14, 9:00 a.m. – 11:30 a.m. (150 minutes)
20 pages, CLOSED BOOK
Instructions
1. Before you begin, make certain that you have
one exam booklet with 20 pages.
2. All solutions must be placed in this booklet.
3. If you need more space for an answer, you
may be writing too much. Regardless, you
may use the blank pages at the end of this
booklet for overflow answers – clearly
Indicate if you did this in the question.
4. A Processing reference and multiple squares
of 100 by 100 graph paper for rough work are
provided on a separate sheet.
5. You may not use statements or functions not
covered in lecture or labs, for example:
2d arrays, rotate(), translate(), or
scale().
6. Processing defaults: 100 by 100 canvas size,
light grey background, white fill, black stroke, 1
pixel strokeWeight, ROUND strokeCap, SQUARE
strokeJoin, CORNER rectMode, CENTER
ellipseMode.
7. You get 1 bonus mark for putting an “X” in
one or both of the white circles on page 19.
Question
Marks
1
22
2
6
3
15
4
6
5
13
6
7
Total
69
Question 1: For this exam, unless otherwise stated, always assume the size of
the canvas is 100x100.
a) Show what gets drawn in the canvas by the
following code. [1 mark]
Commented [k1]: No part marks.
strokeWeight(4);
point(30, 70);
b) Show what gets drawn in the canvas by the
following code. Again, assume the size of the
canvas is 100x100 unless stated otherwise. [1
mark]
Commented [k2]: No part marks.
line(10, 30, 90, 30);
c) Show what gets drawn in the canvas by the
following code. [1 mark]
rect(10, 10, 80, 80);
Commented [k3]: No part marks. Note that there is a border
of 10 pixels on all sides of the rectangle.
d) Show what is printed by println on the
first three frames? [1 mark]
int i = 0;
void draw(){
i = i + 1;
println(i);
}
Commented [k4]: No part marks.
Answer is:
1
2
3
e) What is printed by the two println
statements in draw after the mouse has
been pressed two times? [2 marks]
float midX = 0.0;
void draw() {
println(width);
println(midX);
}
void mousePressed() {
midX = width / 2.0;
}
f) What is printed by println in the following
code? [1 mark]
void setup() {
int y = 0;
while (y < 30) {
println(y);
y = y + 10;
}
}
g) What is printed by println in the following
code? [1 mark]
void setup() {
for(int y = 0; y <= 30; y = y + 10){
println(y);
}
}
h) Show what gets drawn in the canvas by
the following code. [2 marks]
Commented [k5]:
Answer is:
100.0
50.0
1 Mark for printing width is: 100
1 Mark for printing midX is: 50
It doesn’t matter if they include a decimal point or not. So 100.0
and 50.0 is gets 2 marks as well as 100 and 50 gets 2 marks. Also
100.0 and 50, and also 100 and 50.0. All get 2 marks.
Commented [k6]: No part marks.
Answer is:
0
10
20
Answer of “10, 20, 30” does not get any marks.
It doesn’t matter if they are on one line or multiple lines.
Commented [k7]: No part marks.
Answer is:
0
10
20
30
Answer of “10, 20, 30, 40” does not get any marks.
It doesn’t matter if they are on one line or multiple lines.
void setup() {
myFunc();
}
void myFunc() {
strokeWeight(20);
point(50, 50);
}
Commented [k8]:
1 mark for point in the middle of the canvas
1 mark for size of the point which is 10 pixels to the left, right,
top, and bottom. This means it should touch the lines on the
graph paper that are at x= 40, x=60, y=40, and y =60).
i) What is printed by println in the following code? [1 mark]
int answer = 0;
Commented [k9]: No park marks.
Answer is: 3
void setup() {
answer = myFunction();
println(answer);
}
j)
int myFunction() {
return 3;
}
What is printed by println in the following
code? [1 mark]
int myArr[] = {20, 30, 40, 50};
println(myArr[2]);
k) What is printed by println in the following
code? [2 marks]
Commented [k10]: No park marks.
Answer is: 40
Commented [k11]: No part marks.
The answer is 90.
int myA[] = {20, 30, 40};
int answer = 0;
void setup() {
for (int i = 0; i < myA.length; i = i + 1) {
answer = answer + myA[i];
}
println(answer);
}
We will also accept 20, 50, 90.
It doesn’t matter if they are on one line or multiple lines.
l) In the following code the variable ‘ar’ represents the Aspect Ratio. Fill in
the blank line to correctly calculate the Aspect Ratio. Assume the file
‘man.png’ loads correctly without error. [2 mark]
PImage img;
float ar;
void setup() {
img = loadImage("man.png");
Commented [k12]: Answer is: float(width)/height
If they don’t use “float” the answer they still get 2 marks.
We will give 1 mark for: height/width (again, it doesn’t matter if
they use float or not).
ar = float(_________________________________________________);
}
m) What is printed by println in the
following code? [2 marks]
Thing t = new Thing();
void setup() {
println(t.x, t.y);
}
Commented [k13]: Answer is: 88,99
1 Mark for 88
1 Mark for 99
It doesn’t matter if they are on one line or 2 lines.
class Thing {
int x = 88;
int y = 99;
}
n) What is printed by println in the following
code after the mouse has been pressed
once? [1 mark]
boolean on = true;
void draw() {
println(on);
}
void mousePressed() {
on = !on;
}
Commented [k14]: No part marks.
Answer is: false
o) Answer the following three question about the code here. [3 marks]
Commented [k15]: For each answer there is no part marks.
So the mark for this question is either: 0, 1, 2, or 3.
import processing.video.*;
Capture camera;
void setup() {
size(320, 240);
camera = new Capture(this,320, 240);
camera.start();
}
void draw() {
image(camera, 0, 0);
}
void captureEvent(Capture camera) {
camera.read();
}
What does the following code do: import processing.video.*;
___________________________________________________________
Commented [k16]: No part marks.
Answer is: Imports (makes available) all the functionality in the
video library.
___________________________________________________________
What does the function captureEvent do?
___________________________________________________________
Commented [k17]: No part marks.
Answer is: Responds to a camera event.
___________________________________________________________
What does the following code do:
Capture camera;
___________________________________________________________
___________________________________________________________
Commented [k18]: No part marks.
Answer is: Defines (but does not create) a variable “camera” of
type Capture.
Question 2: For this exam, unless otherwise stated, always assume the size of
the canvas is 100x100.
a) What is printed by println in the following code? Circle the best answer. [1
mark]
float x = map(50, 0, 100, 0, 255);
println(x);
Commented [k19]: Answer is: D – 127.5
No part marks.
Circle one:





50.0
0.0
150.0
127.5
255.0
b) What is printed by println in the following code? Circle the best answer. [ 1
mark ]
float x = map(20, 0, 10, 200, 300);
println(x);
Commented [k20]: Answer is: 400
No part marks.
Circle one:





20.0
0.0
300.0
400.0
600.0
c) Consider the following code. What are all the possible values of the variable
x? Circle the best answer. [1 mark]
int x = int(random(5));
Circle one:




0,
1,
0,
1,
1,
2,
1,
2,
2,
3,
2,
3,
3, 4, 5
4, 5
3, 4
4
Commented [k21]: Answer is:
0, 1, 2, 3, 4
No part marks.
d) What is printed by println in the following code? Circle the best answer. [1
mark]
Commented [k22]: Answer is “four”.
No part marks.
float a = 0.5;
float b = 10.0;
if (a > 1.0 && b >
println("one");
} else if (a > 1.0
println("two");
} else if (a > 1.0
println("three");
} else if (a > 1.0
println("four");
}
10.0) {
&& b > 1.0) {
|| b > 10.0) {
|| b > 1.0) {
Circle one:




one
two
three
four
e) What is printed by println in the following code? Circle the best answer. [1
mark]
int[] x = {0, 20, 300, 55, 50};
boolean[] increase = {true, false, false, true, true};
void setup() {
for (int i = 0; i < x.length; i = i + 1) {
if (increase[i]) {
x[i] = x[i] * 2;
}
println(x[i]);
}
}
Commented [k23]: Answer is: 0, 20, 300, 110,
100
No part marks.
Circle one:




0,
0,
0,
0,
20,
40,
20,
40,
300,
600,
300,
600,
55, 50
110, 100
110, 100
55, 50
f) Circle the following statement that complies with our Style Guide. Circle only
one. Circle the best answer. [1 mark]




for
for
for
for
(int
(int
(int
(int
i
i
i
i
=
=
=
=
0;
0,
0;
0,
i
i
i
i
<
<
<
<
5;
5,
5;
5,
i
i
i
i
=
=
=
=
i
i
i
i
+
+
+
+
1) {
1) {
1); {
1) {
Commented [k24]: Answer is the first one:
for (int i = 0; i < 5; i = i + 1) {
No part marks.
Question 3:
a) Complete the code so it will create screenshots like these for any canvas size.
There are four columns separated by three lines. In the screen captures
below, the canvas is the white area only. [3 marks]
Commented [k25]:
float spacing;
float a;
int cols = 4;
void setup() {
size(130, 150); // can be any canvas size
spacing = width / cols;
background(255);
strokeWeight(4);
}
size(100, 100)
size(100, 150)
size(13, 150)
void draw() {
background(255);
a = spacing;
for (int i = 1; i < cols; i = i + 1) {
println(i, a);
line(a, 0, a, height);
a = a + spacing ;
}
}
float spacing;
float a;
int cols = 4;
void setup() {
size(130, 150); // can be any canvas size
spacing = width / cols;
background(255);
strokeWeight(4);
}
void draw() {
background(255);
a = spacing;
for(______________________________________________________________) {
line(___________________________________________________________);
a = ____________________________________________________________ ;
}
}
1 mark for each correct line of code.
b) Complete the code so it will create screenshots like these for any square
canvas size (that is, the width and the height are always the same such as
size(100,100) or size(200,200)). The background is set to white
background(255). As the user moves the mouse, the triangular area
containing the mouse becomes black. In the sample screen captures below,
the canvas is the white area only. The arrow in each image below represents
the approximate location of the mouse (i.e. mouseX, mouseY). [3 marks]
Commented [k26]:
void setup(){
size(100,100);
}
void draw() {
background(255); // white
fill(0); // black
if (mouseX > mouseY) {
triangle(0, 0, width, 0, width, height);
} else {
void setup(){
size(100,100);
}
void draw() {
background(255); // white
fill(0); // black
if (____________________________________________) {
triangle(_________________________________________;
} else {
triangle(___________________________________________);
}
}
triangle(0, 0, 0, height, width, height);
}
}
1 mark for each correct line of code.
c) Complete the code so it would create screenshots like these below. The array
arr holds the size (width and height) of each square to be drawn. It is ok if
some squares do not fit on the canvas (you do not need to check for that). In
the screen captures below, the canvas is the white area only. [5 marks]
Commented [k27]:
int[] arr = {10, 20, 40, 25};// the array
could be any length
int x = 0;
int y;
void setup() {
size(100, 100); // the canvas can be any
size
y = height / 2;
for (int i = 0; i < arr.length; i = i + 1) {
rect(x, height /2, arr[i], arr[i] );
x = x + arr[i];
{25, 20, 30, 10}
{10, 20, 40, 25}
{20, 10, 40, 25, 55, 30}
int[] arr = {10, 20, 40, 25};// the array could be any length
int x = 0;
int y;
void setup() {
size(100, 100); // the canvas can be any size
y = height / 2;
for (int i =______; ________________________; _____________________) {
rect(_____________________________________________________________);
x = x + ___________________________________;
}
}
}
}
1 mark for each blank answered correctly.
d) The parameters for the function checkLine() represent the ends of a line
(x1,y1 is one end of the line and x2,y2 is the other end of the line). Complete
the code so that checkLine() returns true if the line is completely within the
canvas and false otherwise. The canvas can be any size. [4 marks]
boolean checkLine(int x1, int y1, int x2, int y2) {
if(_______________________________________________
Commented [k28]:
void setup() {
println(checkLine(10, 10, 40, 40));
}
boolean checkLine(int x1, int y1, int x2, int
y2) {
if (x1 < 0 || x2 < 0 ||
x1 > width || x2 > width ||
y1 < 0 || y2 < 0 ||
y1 > height || y2 > height) {
return false;
} else {
return true;
}
________________________________________________
________________________________________________
________________________________________________){
}
return _______________________________________________;
} else{
return _______________________________________________;
}
}
2 marks for correct IF condition
2 mark for returning either true or false. Even if te IF condition is
incorrect the code does have a return value of true and a return
value of false.
Question 4:
a) What is the range of possible values that
could be printed by println in the
following code segment? [2 marks]
loadPixels();
color pixelColour = pixels[50];
float r = red(pixelColour);
println(r);
updatePixels();
Commented [k29]: Answer is range of: 0-255
1 mark for 0
1 mark for 255
Also 1 mark is possible if the student put 1-256 (they are both
wrong but both off by one and we give 1 mark for this)
b) The following code has an error. [2 marks]
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i <= arr.length; i = i + 1) {
println(arr[i]);
}
What is the error? ________________________________________
___________________________________________________________
What type



of error is it?
Syntax
Logic
Runtime
Commented [k30]: Because of the “<=” rather than just “<”
there will be an array out of bounds error as it will try to access
arr[5] which doesn’t exist.
1 mark (no part marks)
Circle one:
Commented [k31]: Correct answer is: Runtime
No part marks.
c) The following code has an error. [2 marks]
int i = 0;
while (i < 3); {
println(i);
i = i + 1;
}
What is the error? ________________________________________
___________________________________________________________
What type



of error is it?
Syntax
Logic
Runtime
Circle one:
Commented [k32]: There should not be a “;” after the
condition “(i < 3)”.
1 mark
No part marks
Commented [k33]: Correct answer: Logiv
1 mark.
No part marks.
Question 5:
a) What is drawn by the following code after 1,000,000 frames? [4 marks]
void setup() {
background(255); // white
}
void draw() {
stroke(0); // black
float b = random(0, width);
point(b, b);
point(b, height - b);
}
Commented [k34]:
4 marks for correct.
2 marks for almost correct.
b) What is drawn by the following code? [4 marks]
Ball ball1;
Ball ball2;
void setup() {
size(100, 100);
ball1 = new Ball(40, 20, 20);
ball2 = new Ball(80, 60, 60);
}
Commented [k35]:
void draw() {
ball1.drawb();
ball2.drawb();
}
class Ball {
float x;
float y;
float s;
Ball(float sIn, float xIn, float yIn) {
s = sIn;
x = xIn;
y = yIn;
}
void drawb() {
fill(255); //white
ellipse(x, y, s, s);
}
}
4 marks for correct
2 marks for almost correct.
c) What is drawn by the following code? [2 marks]
Commented [k36]: Answer is:
void setup(){
loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
1 mark for any of these:
int i = x + y * width;
if (y < height /2) {
pixels[i] = color(0); // black
} else {
pixels[i] = color(255);//white
}
}
}
updatePixels();
}
d) What is drawn in the canvas
after three frames. [3 marks]
Commented [k37]: Answer is:
boolean on = true;
int y = 10;
void setup() {
size(100, 100);
}
void draw() {
if (on) {
line(0, y, width, y);
}
y = y + 10;
on = !on;
}
1 mark out of 3 for answers that are close but not correct.
Question 6:
a) Write a function checkOrder to check a global array of type int to determine
if the elements in the global array are sorted from lowest to highest. The
function returns true if the array is sorted and false otherwise. You only
need to write the function checkOrder. You do not need to write setup() or
draw().[4 marks]
For example, if the array is declared as:
int[] arr = {-20, 10, 20, 30, 40};
then checkOrder returns true.
If the array is declared as:
int[] arr = {-20, 99, 20, 30, 0};
then checkOrder returns false.
Commented [k38]:
int[] arr = {-20, 10, 20, 30, 40};
void setup() {
println(checkOrder());
}
boolean checkOrder() {
for (int i = 0; i < arr.length - 1; i = i +
1) {
if (arr[i] > arr[i + 1]) {
return false;
}
}
return true;
}
1 mark for function defined properly including being of type
Boolean and having no parameters.
2 marks for function’s returning the correct result
b) Consider the following code that we have seen in class. Assume it displays
the image of the bird on the canvas. Assume it has no errors. [3 marks]
PImage img;
void setup() {
// load image from the data folder of this sketch
img = loadImage("bird.jpg");
// set canvas size to image size
surface.setResizable(true);
surface.setSize(img.width, img.height);
}
void draw() {
// anything drawn here will be processed
image(img, 0, 0);
// process canvas pixels
loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int i = x + y * width;
color pixelColour = pixels[i];
// extract red, green, and blue parts
float r = red(pixelColour);
float g = green(pixelColour);
float b = blue(pixelColour);
// image processing here
pixels[i] = color(r, g, b);
}
}
updatePixels();
}
Question 6b continued on the next page.
Write code below to replace the line: pixels[i] = color(r, g, b);
Your code should do the following. If the current pixel, pixels[i], is within 20
pixels of the mouse (mouseX, mouseY) then the current pixel becomes white.
Otherwise the current pixel keeps its original colour. For example, the canvas might
look like one of the following.
Commented [k39]:
PImage img;
void setup() {
// load image from the data folder of this
sketch
img = loadImage("bird.jpg");
// set canvas size to image size
surface.setResizable(true);
surface.setSize(img.width, img.height);
}
void draw() {
// anything drawn here will be processed
image(img, 0, 0);
// process canvas pixels
loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int i = x + y * width;
color pixelColour = pixels[i];
// extract red, green, and blue parts
float r = red(pixelColour);
float g = green(pixelColour);
float b = blue(pixelColour);
// image processing here
// (try replacing the code below with
code shown in lecture)
if (dist(mouseX, mouseY, x, y) < 20) {
pixels[i] = color(255);
} else {
pixels[i] = color(r, g, b);
}
}
}
updatePixels();
}
1 mark for calculating distance properly
either using dist() or not.
2 marks for correcting coloring the circle
white
2 marks for correctly having the remainder of
the image available (I.e. the part that is not
white))
Blank Page for Overflow Solutions
Download