0% found this document useful (0 votes)
3 views3 pages

JavaScript Basics Assessment - Answer Key

javascript

Uploaded by

yzftmbjqvh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

JavaScript Basics Assessment - Answer Key

javascript

Uploaded by

yzftmbjqvh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Basics Assessment —

Answer Key
Question 1 — let · var · const
Part A
Which statement about const is correct?
●​ A. A const variable can be reassigned at any time.
●​ B. const variables are function-scoped.
●​ C. A const variable must be assigned a value when declared.
●​ D. const prevents any change to an object's properties.
Answer: C

Part B
Fill in the blanks with let, var, or const:
i. Use _______ when the value will never change.
ii. Use _______ when the value needs to change inside a block.
Answer i: const
Answer ii: let

Part C
What will this code print?
var x = 10;​
if (true) {​
var x = 20;​
let y = 30;​
}​
[Link](x);​
[Link](y);
Answer:
20
ReferenceError: y is not defined
Explanation: var x is function-scoped (or globally scoped here), so reassigning it inside the
if-block changes the value of x to 20. However, let y is block-scoped to the if-statement,
meaning it does not exist outside of it, causing a ReferenceError when [Link](y) is called.

Question 2 — if · else if · nested if-else


Part A
What is printed when score = 55?
if (score >= 90) { [Link]("A"); }​
else if (score >= 75) { [Link]("B"); }​
else if (score >= 50) { [Link]("C"); }​
else { [Link]("F"); }
●​ A. A
●​ B. B
●​ C. C
●​ D. F
Answer: C

Part B
Write a nested if-else for these age categories:
age < 13 → print "child"​
age < 18 → print "teenager"​
age < 60 → print "adult"​
otherwise → print "senior"
Answer:
if (age < 13) {​
[Link]("child");​
} else {​
if (age < 18) {​
[Link]("teenager");​
} else {​
if (age < 60) {​
[Link]("adult");​
} else {​
[Link]("senior");​
}​
}​
}
(Note: A standard chaining syntax using else if is also conceptually correct, but if strict nesting is
required, the above structure satisfies it.)

Question 3 — Ternary operator


Part A
What is the value of result?
let num = 8;​
let result = (num % 2 === 0) ? "even" : "odd";​
[Link](result);
Answer: "even"

Part B
Rewrite this as a single ternary expression stored in a variable called access:
if (loggedIn) { access = "welcome!"; }​
else { access = "please log in"; }
Answer:
let access = loggedIn ? "welcome!" : "please log in";

Question 4 — for · while · do-while


Part A
Which loop always runs at least once, even if the condition is false?
●​ A. for loop
●​ B. while loop
●​ C. do-while loop
●​ D. None of the above
Answer: C

Part B
What does this loop print?
let i = 1;​
while (i <= 5) {​
if (i % 2 !== 0) { [Link](i); }​
i++;​
}
Answer:
1
3
5

Part C
Write a for loop that prints numbers from 10 down to 1.
Answer:
for (let i = 10; i >= 1; i--) {​
[Link](i);​
}

You might also like