wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

JavaScript Basics Quiz

Total questions: 69

Worksheet time: 35mins

Name
Class
Date
1.

What keyword is used to create a variable in JavaScript?

a)

define

b)

let

c)

var

d)

make

2.

Which code correctly assigns the value 5 to the variable score?

a)

score = let 5;

b)

var score = 5;

c)

set score = 5;

d)

score <- 5;

3.

Which block allows a user to type input in a Design tab element?

a)

label

b)

dropdown

c)

text input

d)

button

4.

What is the correct syntax to declare a variable in JavaScript?

a)

create variable x = 5;

b)

int x = 5;

c)

var x = 5;

d)

x := 5;

5.

Which keyword is used to store a number or string in JavaScript?

a)

define

b)

function

c)

var

d)

input

6.

What is the function of setText(id, text)?

a)

Stores text in a variable

b)

Gets text from the screen

c)

Sets text of a UI element

d)

Deletes a text input

7.

Which event type is used to detect a button click in onEvent()?

a)

hover

b)

input

c)

submit

d)

click

8.

What is the purpose of getText("inputId")?

a)

Sets a label's text

b)

Retrieves user input from a text field

c)

Hides a button

d)

Resets the app

9.

What does the onEvent function do?

a)

Sets text in an element

b)

Handles user interaction with a UI element

c)

Defines a variable

d)

Changes a variable's value

10.

In the line

setText("messageLabel", "Hello!");

what does "messageLabel" refer to?

a)

The ID of a screen

b)

A string variable

c)

The text to display

d)

The ID of a UI element

11.

What will happen after this code is run?

var name = getText("nameInput");

setText("greetingLabel", "Hello " + name);

a)

It changes the screen

b)

It adds numbers

c)

It creates a popup

d)

It displays a greeting using the user's input

12.

Why is setProperty("image1", "image", "cat.png"); used?

a)

To make a new image

b)

To change the image displayed in an element

c)

To store a variable

d)

To rename an image

13.

What does getNumber("inputBox"); do?

a)

Creates a number

b)

Stores a string

c)

Retrieves a numeric value from the UI

d)

Creates an alert

14.

What happens when a variable is declared inside a function?

a)

It becomes global

b)

It is stored permanently

c)

It can only be used within that function

d)

It can be used in any file

15.

Which code reads input from a text field and displays it?

a)

inputText("myInput", "displayLabel");

b)

getProperty("myInput");

c)

var name = getText("myInput"); setText("displayLabel", name);

d)

setText("displayLabel", getProperty("myInput"));

16.

What is the result of this code?

var age = getNumber("ageInput");

setText("outputLabel", age + 5);

a)

Adds 5 to the age and displays it

b)

Displays "age + 5" as text

c)

Throws an error

d)

Converts age to text

17.

To change the background color of a screen named "mainScreen", use:

a)

setText("mainScreen", "blue");

b)

setProperty("mainScreen", "background-color", "blue");

c)

setScreen("blue");

d)

getProperty("mainScreen", "color");

18.

How would you hide an element using code.org JavaScript?

a)

hide("elementId");

b)

setProperty("elementId", "visible", false);

c)

setProperty("elementId", "hidden", true);

d)

setProperty("elementId", "hidden", "true");

19.

Given a variable score = 10, what does score = score + 5; do?

a)

Sets score to 5

b)

Adds 5 to score

c)

Resets score to 0

d)

Multiplies score by 5

20.

What will getText("answerInput") = "yes" evaluate to if the user types "NO"?

a)

true

b)

false

c)

undefined

d)

null

21.

What is the error in this code?

onEvent("submitBtn", "click", function() {

  var age = getNumber("ageInput");

  setText("output", age + " years old");

});

a)

getNumber is used wrong

b)

Text is being added to a number

c)

There is no error

d)

setText cannot display variables

22.

Why does this code not update the label correctly?

var userName;

onEvent("button1", "click", function() {

  getText("input1");

  setText("output", userName);

});

a)

userName is never set

b)

button1 doesn't exist

c)

getText is used incorrectly

d)

Output is not connected

23.

What will be displayed?

var a = 2;

var b = 3;

setText("output", a + b + " is the total");

a)

5

b)

"2 + 3 is the total"

c)

"5 is the total"

d)

"23 is the total"

24.

What will happen if you call setText("label1", greeting); before greeting is declared?

a)

Nothing

b)

Runtime error

c)

greeting will be undefined

d)

label1 disappears

25.

Identify the error:

var name = getText("nameInput");

onEvent("greetBtn", "click", function() {

  setText("greetingLabel", "Hi " + name);

});

a)

Variable name used too early

b)

onEvent should be first

c)

getText should be inside the onEvent

d)

greetingLabel is undefined

26.

Why might setText("scoreLabel", total); not display anything?

a)

total is undefined

b)

Label is not visible

c)

setText cannot take numbers

d)

Incorrect label ID

27.

Which is a better way to combine a number and string?

a)

setText("label", 10 + " points");

b)

setText("label", "points" + 10);

c)

Both work

d)

Neither works

28.

Select the best debugging strategy for the following:

onEvent("btn", "click", function() {

  var x = getText("box");

  setText("output", x + 5);

});

a)

Check if "box" has a number

b)

Remove var

c)

Use setProperty instead

d)

Change box to getNumber

29.

Why is this incorrect?

setText("label", getText("input"));

var input = "name";

a)

getText has no value

b)

variable input is misused

c)

Variable declared too late

d)

setText cannot use variables

30.

Which of these code blocks is easiest to maintain?

a)

Using hardcoded values

b)

Using variables and clear IDs

c)

Using global variables only

d)

Using same name for all elements

31.

What makes a program easier to debug?

a)

No comments

b)

Using many global variables

c)

Using clear variable names

d)

Keeping all code in one block

32.

Which is the correct syntax for an onEvent click function?

a)

onClick("button", function() {});

b)

onEvent("button", "click", function() {});

c)

event("button", "click", function);

d)

on("click", "button", function() {});

33.

What is the purpose of getText("inputBox")?

a)

Set text

b)

Retrieve user input

c)

Make a new box

d)

Add numbers

34.

Which line sets the background color of a button to red?

a)

setText("btn", "red");

b)

setProperty("btn", "background-color", "red");

c)

getProperty("btn", "color", "red");

d)

setStyle("btn", "red");

35.

Which event does NOT work with onEvent?

a)

click

b)

change

c)

drag

d)

random

36.

Which function displays the message "Try again"?

a)

getText("Try again");

b)

showText("Try again");

c)

setText("messageLabel", "Try again");

d)

print("Try again");

37.

Choose the correct way to update the text of a label to "Done"

a)

setText("label1", Done);

b)

setText(label1, "Done");

c)

setText("label1", "Done");

d)

label1.setText("Done");

38.

What does setProperty("box", "text-color", "blue"); do?

a)

Changes the color of text inside a box

b)

Changes border color

c)

Changes screen color

d)

Changes font

39.

What happens if you use setText("label1", getNumber("box1")); and box1 is empty?

a)

Shows 0

b)

Shows undefined

c)

Shows empty label

d)

Causes error

40.

How would you connect an event to a button that shows a hidden image?

a)

A. onEvent("btn", "click", function() { show("img1"); });

b)

B. onClick("btn", function() { show("img1"); });

c)

C. btn.onclick = function() { show("img1"); };

d)

D. img1.show();

41.

Which combination of functions retrieves input and updates text?

a)

getText() and setText()

b)

setText() and showText()

c)

setProperty() and alert()

d)

inputText() and outputText()

42.

What does the following code do?

setText("titleLabel", "Welcome!");

a)

Changes the label's color

b)

Changes the label's text

c)

Hides the label

d)

Changes the screen

43.

Which code will change the text color of a label to green?

a)

setProperty("label1", "text-color", "green");

b)

setText("label1", "green");

c)

setTextColor("label1", "green");

d)

textColor("label1", "green");

44.

What type of element does getText("userNameInput") work with?

a)

Image

b)

Button

c)

Text input field

d)

Dropdown

45.

Which line of code sets a button's text to "Submit"?

a)

getText("submitBtn", "Submit");

b)

setText("submitBtn", "Submit");

c)

submitBtn.text = "Submit";

d)

buttonText("submitBtn", "Submit");

46.

What happens in the following code?

onEvent("btnSend", "click", function() {

  var msg = getText("messageInput");

  setText("outputLabel", msg);

});

a)

It stores a message permanently

b)

It sends a message to another app

c)

It displays user input from a text field in a label

d)

It creates a new variable

47.

Which code sets a dropdown's selected item to "High"?

a)

setText("dropdown1", "High");

b)

setProperty("dropdown1", "value", "High");

c)

getText("dropdown1");

d)

dropdown1.value = "High";

48.

Which code shows how to respond to a user clicking a button named "submitBtn"?

a)

submitBtn.on("click", function() {})

b)

onEvent("submitBtn", "click", function() { });

c)

submitBtn.addEvent("click", function() {});

d)

onClick("submitBtn", function() {});

49.

How can you use input from a text field to customize a greeting message?

onEvent("greetBtn", "click", function() {

  var name = getText("nameInput");

  setText("greetingLabel", "Hello " + name);

});

a)

It alerts the user

b)

It changes the nameInput

c)

It displays a greeting using input

d)

It sets a variable to the label name

50.

Which statement correctly changes the font size of a label?

a)

setText("label1", "font-size", "20px");

b)

setProperty("label1", "font-size", "20px");

c)

label1.style.fontSize = "20px";

d)

setStyle("label1", "font-size", "20px");

51.

What is the purpose of using onEvent in an app?

a)

To declare a variable

b)

To draw shapes on screen

c)

To detect and respond to user actions

d)

To store user data permanently

52.

Which combination of commands would let you design a calculator that multiplies two user-entered numbers and shows the result in a label?

a)

getText, setProperty, onEvent

b)

getNumber, setText, onEvent

c)

onEvent, drawRect, console.log

d)

var, setScreen, getText

53.

You want to make a quiz app. Which function lets you update the label after a user submits their name?

a)

getProperty

b)

getText + setText inside onEvent

c)

setImageURL

d)

setProperty

54.

You're designing a form that accepts user age and prints a message. Which code block is best?

a)

onEvent → getNumber → setText

b)

setText → getNumber → getText

c)

onEvent → getText → setScreen

d)

getText → onEvent → setNumber

55.

Which of these is necessary to create a login screen?

a)

getText and setText only

b)

onEvent and getText

c)

setProperty only

d)

getNumber only

56.

You want to collect feedback from users. Which would you use?

a)

setImageURL and onEvent

b)

getText, onEvent, and setText

c)

getNumber only

d)

setScreen only

57.

What is the best way to create an app that displays different messages depending on user input?

a)

Use a fixed label

b)

Use getText and if-else

c)

Use setScreen

d)

Use setProperty only

58.

If a user types a number in a field and presses a button to show the double of that number, which logic works?

a)

setText("label", getNumber("input") + 2);

b)

setText("label", getText("input") * 2);

c)

setText("label", getNumber("input") * 2);

d)

getNumber("input");

59.

In a shopping cart app, how can you display the final price based on quantity and price per item?

a)

Use getText only

b)

getNumber + multiplication + setText

c)

getNumber + subtraction + getText

d)

setText only

60.

To reset all fields in a form, which actions are needed?

a)

Reload the screen

b)

setText("", "") for each field

c)

getText("")

d)

onEvent("", "change")

61.

What will be the output of this code?

var total = 10 + 20;

setText("resultLabel", total);

a)

10

b)

10 + 20

c)

30

d)

20

62.

What will occur if you try to setText("label1", null);?

a)

It sets the label to an empty string

b)

It causes a runtime error

c)

It sets the label to 'null'

d)

It does nothing

63.

What is the result of calling getText("inputField"); if the field is empty?

a)

Causes an error

b)

Returns undefined

c)

Returns an empty string

d)

Returns null

64.

Which function is used to change the visibility of a UI element?

a)

setProperty()

b)

showElement()

c)

setVisible()

d)

toggleVisibility()

65.

What is the purpose of the getText function in code.org JavaScript?

a)

To delete a text element

b)

To create a new text element

c)

To set the text of a UI element

d)

To retrieve the value from a UI element

66.

What will happen if you try to use a variable that has not been declared?

a)

It will throw a reference error

b)

It will return null

c)

It will display undefined

d)

It will cause a syntax error

67.

What will be the output of the following code?
var result = getNumber("inputField");
setText("outputLabel", result * 2);

a)

It displays nothing

b)

It shows an error

c)

It shows the input as a string

d)

It shows the input multiplied by 2

68.

Which code correctly retrieves the value from a checkbox?

a)

var isChecked = getProperty("checkbox1", "checked");

b)

var isChecked = getText("checkbox1");

c)

var isChecked = getValue("checkbox1");

d)

var isChecked = checkbox1.value;

69.

What does the following code do?
onEvent("btnReset", "click", function() { setText("outputLabel", ""); });

a)

Changes the button text

b)

Sets outputLabel to 'btnReset'

c)

Clears the text of outputLabel

d)

Displays an error