wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

JavaScript and Graphics

Total questions: 15

Worksheet time: 30mins

Name
Class
Date
1.

A store has 20 apples in its inventory. How can you store this information in a JavaScript variable?

a)

var numApples == 20;

b)

20 = numApples;

c)

var numApples = 20;

d)

var num apples = 20;

2.

You want to read input from the user to know how many apples they would like to buy. Which statement should you use to read in a number from the user?

a)

var applesToBuy = readLine("How many apples would you like? ");

b)

var applesToBuy = println("How many apples would you like? ");

c)

var applesToBuy = readInt("How many apples would you like? ");

d)

var applesToBuy = nextInt("How many apples would you like? ");

3.

You are splitting up all of your apples equally between 3 people. Which statement below will calculate how many apples will be left over?

a)

var leftOver = numApples / 3;

b)

var leftOver = 3 / numApples;

c)

var leftOver = numApples % 3;

d)

var leftOver = 3 % numApples;

4.

In a graphics canvas, what are the coordinates of the bottom right corner of the window?

a)

0, 0

b)

0, getWidth()

c)

getHeight(), getWidth()

d)

getWidth(), getHeight()

5.

What are the coordinates of the center of the window?

a)

getWidth(), getHeight()

b)

getWidth() / 2, getHeight() / 2

c)

getHeight() - getWidth(), getWidth() - getHeight()

d)

getHeight() / 2, getWidth() / 2

6.

In the following code:

var size = 20;

var x = 100;

var y = 200;

var ball = new Circle(size);

ball.setPosition(x, y);

What is the meaning of the x variable?

a)

The x coordinate of the center of the circle

b)

The x coordinate of the left edge of the circle

c)

The radius of the circle

d)

The position of the circle

7.

The following program should draw a circle on the screen

1 function start(){

2 var circle = new Circle(40);

3 circle.setPosition(getWidth() / 2, getHeight() / 2);

4 circle.setColor(Color.blue);

5 }

But when we run this code, we don’t see the circle. What is missing from our start function?

a)

We never set the circle’s radius.We need to add the linecircle.setRadius(40);After line 4

b)

We are setting the position of the ball to be off the canvas.

We need to change line 3 to be

circle.setPosition(0, 0);

c)

We are creating the circle incorrectly. We need to specify the width and height.

We need to change line 2 to be

var circle = new Circle(40, 40);

d)

We create the circle and position it correctly on the screen, but we never add it to the screen.

We need to add the line

add(circle);

after line 4

8.

What is the output of the following program: (Assume the user enters ‘Florence’ then ‘Fernandez’.)

var first_name = readLine("What is your first name? ");

var last_name = readLine("What is your last name? ");

var whole_name = first_name + last_name;

println (whole_name);

a)

Fernandez Florence

b)

Florence

Fernandez

c)

FlorenceFernandez

d)

Florence Fernandez

9.

Which of the following choices is a properly formed JavaScript variable name, meaning it is both legal in the JavaScript language and considered good style?

a)

user_age

b)

UserAge

c)

userAge

d)

User age

10.

If you wanted to use the following code to draw a circle to the canvas with the radius, and x and y coordinates given by the user, how would you ask for this information?

var circle = new Circle(radius);

circle.setPosition(x, y);

circle.setColor(color);

add(circle);

a)

var x = readLine(“What is the x coordinate? “);

var y = readLine(“What is the y coordinate? “);

var radius = readLine(“What is the radius of the circle? “);

b)

var x = readInt(“What is the x coordinate? “);

var y = readInt(“What is the y coordinate? “);

var radius = readInt(“What is the radius of the circle? “);

c)

var x = readLine(“What is the x coordinate? “);

var y = readLine(“What is the y coordinate? “);

var radius = readInt(“What is the radius of the circle? “);

d)

var x = readInt(“What is the x coordinate? “);

var y = readInt(“What is the y coordinate? “);

var radius = readLine(“What is the radius of the circle? “);

11.

If you were given the following variables:

var distance = 2;

var time = 30;

What line of code would print the speed in km/hr if the distance is given in km and the time is given in minutes? (Speed = distance/time)

a)

println(distance/(time/60));

b)

println(distance/time);

c)

println(distance*(time*60));

d)

println(distance*time);

12.

What is printed to the screen when the following program is run?

var num = 13 % 3;

println(num);

a)

1

b)

2

c)

3

d)

4

13.

What is printed to the screen if the user enters ‘5’ when the following program is run?

var userNum = readInt("Enter your favorite number: ");

var calculatedNum = (userNum * userNum) + userNum;

println(calculatedNum);

a)

5

b)

25

c)

30

d)

50

14.

What does the following program do?

var radius = readInt("What is the radius of the circle? ");


var circle = new Circle(radius);

circle.setPosition(radius, radius);

add(circle);

a)

Draws a circle to the center of the canvas with size based on user inputted value

b)

Draws a circle on the canvas with the center positioned at the coordinate (0,0)

c)

Draws a circle that is positioned in the bottom right corner of the canvas

d)

Draw a circle on the canvas that is always tangential to the top and left sides of the canvas

15.

If a user enters a diameter value in the variable diameter, how would we use this to draw the correct sized circle?

a)

var circle = new Circle(diameter);

circle.setPosition(100,100);

add(circle);

b)

var circle = new Circle(diameter*2);

circle.setPosition(100,100);

add(circle);

c)

var circle = new Circle(diameter/2);

circle.setPosition(100,100);

add(circle);

d)

var circle = new Circle(diameter%2);

circle.setPosition(100,100);

add(circle);