Sets – Basics

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Sets – Basics

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does new Set() create a new Set object?
A No
B Yes
2. Does add() add a value to a Set?
A Yes
B No
3. Does a Set automatically remove duplicate values?
A No
B Yes
4. Is 'set' with a lowercase s valid for creating a Set?
A No
B Yes
5. Must 'add' be written in lowercase?
A Yes
B No

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1new Set() AWrong - 'Set' must start with a capital S
2nums.add(1) Bcolors.add("red")
3Automatically removes duplicate values from a collection CSet
4Inserts a value into a Set DWrong - 'add' must start with a lowercase a
5let s = new set(); ECreates a new, empty Set
6s.Add(1); FAdds the value 1 to the nums Set

Definitions

new Set()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let nums = ___ Set();
2.
nums.___(1);
3.
let colors = ___ Set();
4.
colors.___("red");
5.
nums.___(2);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let s = new set();
B s.add(1);
C console.log(s);
Question 2
A let s2 = new Set();
B s2.Add(2);
C console.log(s2);
Question 3
A let s3 = new Set();
B s3.add(3);
C s3.add(3);
Question 4
A let a = 1;
B let b = 2;
C let s5 = new set();
Question 5
A let s6 = new Set();
B s6.add(4);
C console.log(s6);
D s6.Add(5);

Answer Key

Sets – Basics
Section A: Quick Questions
Q1
B — Yes
Q2
A — Yes
Q3
B — Yes
Q4
A — No
Q5
A — Yes
Section B: Match the Pairs
Q1
new Set() → Creates a new, empty Set
Q2
nums.add(1) → Adds the value 1 to the nums Set
Q3
Automatically removes duplicate values from a collection → Set
Q4
Inserts a value into a Set → colors.add("red")
Q5
let s = new set(); → Wrong - 'Set' must start with a capital S
Q6
s.Add(1); → Wrong - 'add' must start with a lowercase a
Section C: Definitions
Q1
new Set() — Creates a new Set, a built-in object that stores only unique values.
Section D: Fill in the Blanks
Q1
let nums = new Set();
Q2
nums.add(1);
Q3
let colors = new Set();
Q4
colors.add("red");
Q5
nums.add(2);
Section E: Spot the Error
Q1
A — 'Set' must start with a capital S, not lowercase.
Q2
B — 'add' must be lowercase - a capital A causes an error.
Q3
C — This line is actually correct - adding a duplicate just has no effect.
Q4
C — 'Set' must start with a capital S, not lowercase.
Q5
D — 'add' must be lowercase - a capital A causes an error.