wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Interactive Animations and Games

Total questions: 24

Worksheet time: 48mins

Name
Class
Date
1.

What does this program do?

a)

Animates a ball by moving it down and to the right once every 20 milliseconds.

b)

Animates a ball by moving it up and to the right once every 20 milliseconds

c)

Animates a ball by moving it down and to the right every 20 seconds

d)

Animates a ball by moving it up and to the right once every 20 seconds

2.

Which of the following statements are true about ball?

I – ball is a local variable

II – the ball variable in draw is different from the ball variable in start

III – ball is a global variable

IV – ball’s scope includes both start and draw

a)

III and IV

b)

I and II

c)

III only

d)

I, II, III, and IV

3.

The following program animates a ball by setting a timer to move the ball across the screen. We want the animation to stop whenever the user clicks the mouse:


Which statement would stop the animation in this program?

a)

stopTimer(ball.move(2, 2));

b)

stopTimer(draw);

c)

stopTimer(draw());

d)

stopTimer(draw, 20);

4.

We extend our draw function to include:


How many times will the ball be moved before the animation stops?

a)

20

b)

40

c)

2

d)

25

5.

Which of the following are techniques that make our code more reusable?

I – Using constants instead of magic numbers

II – Using specific values in our functions rather than using parameters

III – Writing multiple functions that solve small subproblems rather than one function that solves the entire problem.

IV – Writing as few lines of code as possible

a)

I, II, and III

b)

II and IV

c)

I and III, and IV

d)

I, II, III, and IV

6.

Which function has better reusability?

a)
b)
7.

How many circles of radius 10 will fit vertically stacked in a window that has height 100? The circles should not overlap, the bottom of one circle should touch the top of the next circle.

a)

10

b)

20

c)

5

d)

100

8.

This is a circle on our canvas named ball. What are the coordinates of point A?

a)

ball.getX(), ball.getY()

b)

ball.getRadius(), ball.getY()

c)

ball.getX(), ball.getY() + ball.getRadius()

d)

ball.getX() + ball.getRadius(), ball.getY()

9.

What statement should we use to determine if ball is hitting the bottom edge of the window?

a)
b)
c)
d)
10.

How can we determine if ball’s left edge is hitting a different shape on our canvas named box?

a)
b)
c)
d)
11.

The following code moves a ball across the screen at a rate of dx in the x direction and dy in the y direction.


What do we need to add to line 19 to have the ball bounce off the top of the window?

a)

dy = -dy;

b)

ball.setPosition(ball.getX(), getHeight() - ball.getRadius());

c)

dx = -dx;

d)

dy = -dx;

12.

Which of the following statements will call the function paint every time the mouse is moved?

a)

mouseMoveMethod(paint, e);

b)

mouseMoveMethod(paint);

c)

mouseMoveMethod(paint());

d)

mouseMoveMethod(paint(e));

13.

What is a callback function?

a)

A function called at the bottom of the start function

b)

A function passed to an event handler that is called every time a certain event happens

c)

A function named callback

d)

A function that is called every DELAY milliseconds

14.

In the following statement:


mouseMoveMethod(draw);


Which part is the event handler?

a)

mouseMoveMethod

b)

draw

c)

e

d)

none

15.

In the following statement:


mouseMoveMethod(draw);


Which part is the callback function?

a)

mouseMoveMethod

b)

draw

c)

e

d)

none

16.

In the following code:


function start(){

mouseUpMethod(paint);

}


function paint(e){

//code to paint something on the canvas

...

}


What is the meaning of e?

a)

e is a parameter of paint that determines what exactly will be painted

b)

e is a parameter passed to the callback function paint that contains information about the Mouse Event, such as where on the window it occurred.

c)

e is a variable that paint can store a shape inside of so that start can access it.

d)

e is the callback function of paint

17.

This code creates a new circle on the canvas every time the mouse is pressed down and the circle follows the mouse when the mouse is dragged.


But there is one problem. What is wrong with the following code?


1 var ball;

2

3 function down(e){

4 var ball = new Circle(40);

5 ball.setPosition(e.getX(), e.getY());

6 add(ball);

7 }

8 function drag(e){

9 ball.setPosition(e.getX(), e.getY());

10

11 }

12

13 function start(){

14

15 mouseDownMethod(down);

16 mouseDragMethod(drag);

17 }

a)

Line 9 is updating the location of the ball incorrectly.

b)

Line 6 adds a ball to the screen every time the mouse is down, but we need a ball added before any mouse events happen

c)

Lines 15 and 16 are registering the mouse events incorrectly

d)

Line 4 declares a new local variable var ball. The local variable ball in down is different from the global variable ball in drag, but we need these functions to be affecting the same ball

18.

How can we fix the problem in this code from question 18? The same code is shown again for reference.


1 var ball;

2

3 function down(e){

4 var ball = new Circle(40);

5 ball.setPosition(e.getX(), e.getY());

6 add(ball);

7 }

8 function drag(e){

9 ball.setPosition(e.getX(), e.getY());

10

11 }

12

13 function start(){

14

15 mouseDownMethod(down);

16 mouseDragMethod(drag);

17 }

a)

Change lines 15 and 16 to

mouseDownMethod(down(e));

mouseDragMethod(drag(e));

b)

Move line 6 to line 14 so that the ball is added before any mouse events happen.

c)

Get rid of the word var in line 4 so that the ball in down refers to the same global ball in drag rather than a local variable.

d)

Change line 9 to

ball.move(e.getX(), e.getY());

19.

Describe the result of the following line of code. What will happen in our program when this line is in our start function?


keyDownMethod(moveBall);

a)

The function moveBall will be called every time the down arrow on the keyboard is pressed down.

b)

The function moveBall will be called every time any key on the keyboard is pressed down.

c)

The function moveBall will be called only when a key on the keyboard is held down

d)

The function moveBall will be called once, then the program will be paused until a key is pressed.

20.

In the following program:

var STEP = 5;

var SQUARE_SIZE = 20;

var square;


function start(){

square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE);

square.setPosition(getWidth() / 2, getHeight() / 2);

keyDownMethod(down);

add(square);

}


function down(e){

if(e.keyCode == Keyboard.LEFT){

square.move(-STEP, 0);

}


else if(e.keyCode == Keyboard.RIGHT){

square.move(STEP, 0);

}


else if(e.keyCode == Keyboard.letter('R')){

square.move(0, -STEP);

}


else if(e.keyCode == Keyboard.letter('F')){

square.move(0, STEP);

}


else{

square.setColor(Randomizer.nextColor());

}

}


How can a user move the square up on the screen?

a)

Pressing down the ‘F’ key

b)

Pressing down the UP arrow key

c)

Pressing down the ‘R’ key

d)

Pressing down the LEFT arrow key

21.

In the same program as before:

var STEP = 5;

var SQUARE_SIZE = 20;

var square;


function start(){

square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE);

square.setPosition(getWidth() / 2, getHeight() / 2);

keyDownMethod(down);

add(square);

}


function down(e){

if(e.keyCode == Keyboard.LEFT){

square.move(-STEP, 0);

}


else if(e.keyCode == Keyboard.RIGHT){

square.move(STEP, 0);

}


else if(e.keyCode == Keyboard.letter('R')){

square.move(0, -STEP);

}


else if(e.keyCode == Keyboard.letter('F')){

square.move(0, STEP);

}


else{

square.setColor(Randomizer.nextColor());

}

}


What happens if the user presses the spacebar?

a)

The program crashes because there is no event handler registered for the spacebar

b)

The score increases

c)

The square changes color to a random color

d)

nothing

22.

The following code draws stacks of alternating red and black circles across the window one at a time, filling the window from left to right.

Code:

1 var DELAY = 50;

2 var RADIUS = 20;

3 var NUM_CIRCLES = getHeight() / (2*RADIUS);

4

5 var xPosition;

6

7 function start(){

8 xPosition = RADIUS;

9 setTimer(draw, DELAY);

10 }

11

12 function draw(){

13 drawColumn(xPosition);

14

15 //your code goes here

16 xPosition = ...

17 }

18

19 function drawColumn(x){

20 //draws a column of alternating red and black circles centered at "x"

21 //don't worry about how it's implemented yet

22 ...

23 }


What should the value of xPosition be changed to on line 16 so that the next column drawn is touching the right side of the previous column?

a)

xPosition = RADIUS;

b)

xPosition = NUM_CIRCLES * RADIUS;

c)

xPosition = xPosition + RADIUS;

d)

xPosition = xPosition + 2*RADIUS;

23.

In the code from the last question that draws columns of red and black circles across the window:


1 var DELAY = 50;

2 var RADIUS = 20;

3 var NUM_CIRCLES = getHeight() / (2*RADIUS);

4

5 function start(){

6 setTimer(draw, DELAY);

7 }

8

9 function draw(){

10 drawColumn(xPosition);

11

12 //your code from question 23 here

13 xPosition = ...

14 }

15

16 //draws a column of alternating red and black circles centered at "x"

17 function drawColumn(x){

18 for(var i = 0; i < NUM_CIRCLES; i++){

19 var circle = new Circle(RADIUS);

20 if(i % 2 == 0){

21 circle.setColor(Color.red);

22 } else {

23 circle.setColor(Color.black);

24 }

25

26 //your code goes here

27 var y = ...

28

29 circle.setPosition(x, y);

30 add(circle);

31 }

32 }


What should the value of y be on line 27 so that the first circle added is touching the top of the window, and each subsequent circle is touching the bottom of the previous circle. The circles should not overlap and there should be no space in between circles:

a)

var y = 2 * i * RADIUS;

b)

var y = RADIUS + (2 * i * RADIUS);

c)

var y = 2 * (i + 1) * RADIUS;

d)

var y = getHeight() - (2 * i * RADIUS);

24.

Which of the following are good examples of things that should be stored as global variables?


I – The parameter e to a callback function

II – The ball in the game the user is playing

III – A counter keeping track of how many times the user has pressed the spacebar

IV – A for loop counter

V – The color of a rectangle that is only used in one function

a)

II, III, and V

b)

I, IV, and V

c)

II and III

d)

I, II, and III