Q1. How is a forEach statement different from a for statement?
Only a for statement uses a callback function.
A for statement is generic, but a forEach statement can be used only with an array.
Only a forEach statement lets you specify your own iterator.
A forEach statement is generic, but a for statement can be used only with an array.
2 Answer
Reference Differences between forEach and for loop
Q2. When would the final statement in the code shown be logged to the console? When would 'results shown' be logged to the console?
let modal = document.querySelector('#result');
setTimeout(function () {
modal.classList.remove('hidden');
}, 10000);
console.log('Results shown');
- after 10 second
- after results are received from the HTTP request
- after 10000 seconds
- immediately
4 Answer
Reference Javascript is synchronous and single threaded
Q3. Which snippet could you add to this code to print "food" to the console?
class Animal {
static belly = [];
eat() {
Animal.belly.push('food');
}
}
let a = new Animal();
a.eat();
console.log(/* Snippet Here */); //Prints food
a.prototype.belly[0]
Object.getPrototype0f (a).belly[0]
Animal.belly[0]
a.belly[0]
3 Answer
Reference Javascript Class static Keyword
Q4. You've written the code shown to log a set of consecutive values, but it instead results in the value 5, 5, 5, and 5 being logged to the console. Which revised version of the code would result in the value 1, 2, 3 and 4 being logged?
A
for (var i = 1; i <= 4; i++) {
setTimeout(function () {
console.log(i);
}, i * 10000);
}
B
for (var i = 1; i <= 4; i++) {
(function (i) {
setTimeout(function () {
console.log(j);
}, j * 1000);
})(j);
}
C
for (var i = 1; i <= 4; i++) {
setTimeout(function () {
console.log(i);
}, i * 1000);
}
D
for (var i = 1; i <= 4; i++) {
(function (j) {
setTimeout(function () {
console.log(j);
}, j * 1000);
})(i);
}
E
for (var j = 1; j <= 4; j++) {
setTimeout(function () {
console.log(j);
}, j * 1000);
}
D Answer
Q5. How does a function create a closure?
- It reloads the document whenever the value changes.
- It returns a reference to a variable in its parent scope.
- It completes execution without returning.
- It copies a local variable to the global scope.
2 Answer
Q6. Which statements(multiple options) creates a new function called discountPrice?
A
let discountPrice = function (price) {
return price * 0.85;
};
B
let discountPrice(price) {
return price * 0.85;
};
C
let function = discountPrice(price) {
return price * 0.85;
};
D
discountPrice = function (price) {
return price * 0.85;
};
A & D Answer
Reference defining javascript functions
Q7. What is the result in the console of running the code shown?
var Storm = function () {};
Storm.prototype.precip = 'rain';
var WinterStorm = function () {};
WinterStorm.prototype = new Storm();
WinterStorm.prototype.precip = 'snow';
var bob = new WinterStorm();
console.log(bob.precip);
- Storm()
- undefined
- 'rain'
- 'snow'
4 Answer
Q8. What is the result in the console of running this code?
'use strict';
function logThis() {
this.desc = 'logger';
console.log(this);
}
new logThis();
undefined
window
{desc: "logger"}
function
3 Answer
Q9. You're adding error handling to the code shown. Which code would you include within the if statement to specify an error message?
function addNumbers(x, y) {
if (isNaN(x) || isNaN(y)) {
}
}
exception('One or both parameters are not numbers')
catch('One or both parameters are not numbers')
error('One or both parameters are not numbers')
throw('One or both parameters are not numbers')
4 Answer
Q10. When would you use a conditional statement?
- When you want to reuse a set of statements multiple times.
- When you want your code to choose between multiple options.
- When you want to group data together.
- When you want to loop through a group of statement.
2 Answer
Reference javascript conditionals
Q11. Which Object method returns an iterable that can be used to iterate over the properties of an object?
Object.get()
Object.loop()
Object.each()
Object.keys()
4 Answer
Reference javascript object static methods
Q12. What is one difference between collections created with Map and collections created with Object?
- You can iterate over values in a Map in their insertion order.
- You can count the records in a Map with a single method call.
- Keys in Maps can be strings.
- You can access values in a Map without iterating over the whole collection.
2 (Answer
Map.prototype.size returns the number of elements in a Map, whereas Object does not have a built-in method to return its size.
)
Reference map methods javascript
Q13. 0 && hi
- ReferenceError
- true
- 0
- false
3 Answer
Q14. Which of the following values is not a Boolean false?
Boolean(0)
Boolean("")
Boolean(NaN)
Boolean("false")
4 Answer
Q15. For the following class, how do you get the value of 42 from an instance of X?
class X {
get Y() {
return 42;
}
}
var x = new X();
x.get('Y')
x.Y
x.Y()
x.get().Y
2 Answer
Post your result in the comment section and let me know how was the quiz🙌👍
Top comments (2)
I didn't went beyond Q4. It marks the following answer as correct:
However, just changing the
var
intolet
would get rid of the issue and be much simpler IMHO:...so, well, that was enough for the lazy me. I mean, at least teach them something useful like don't use
var
in loops ...it's such a common pitfall and so easy to avoid. It looks more important to me than teaching contrivedvar
loops using closures.Nice, still you tried. 👍 I put var to remind people that there is another type of variable besides
let
andconst