DEV Community

Cover image for Think You're a JavaScript Pro? Find Out with this Quiz👇
Arafat
Arafat

Posted on

Think You're a JavaScript Pro? Find Out with this Quiz👇

Q1. How is a forEach statement different from a for statement?

  1. Only a for statement uses a callback function.

  2. A for statement is generic, but a forEach statement can be used only with an array.

  3. Only a forEach statement lets you specify your own iterator.

  4. A forEach statement is generic, but a for statement can be used only with an array.

Answer

2

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');
Enter fullscreen mode Exit fullscreen mode
  1. after 10 second
  2. after results are received from the HTTP request
  3. after 10000 seconds
  4. immediately

Answer

4

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
Enter fullscreen mode Exit fullscreen mode
  1. a.prototype.belly[0]
  2. Object.getPrototype0f (a).belly[0]
  3. Animal.belly[0]
  4. a.belly[0]

Answer

3

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);
}
Enter fullscreen mode Exit fullscreen mode

B

for (var i = 1; i <= 4; i++) {
  (function (i) {
    setTimeout(function () {
      console.log(j);
    }, j * 1000);
  })(j);
}
Enter fullscreen mode Exit fullscreen mode

C

for (var i = 1; i <= 4; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 1000);
}
Enter fullscreen mode Exit fullscreen mode

D

for (var i = 1; i <= 4; i++) {
  (function (j) {
    setTimeout(function () {
      console.log(j);
    }, j * 1000);
  })(i);
}
Enter fullscreen mode Exit fullscreen mode

E

for (var j = 1; j <= 4; j++) {
  setTimeout(function () {
    console.log(j);
  }, j * 1000);
}
Enter fullscreen mode Exit fullscreen mode

Answer

D

  1. Reference setTimeout
  2. Reference immediately invoked anonymous functions

Q5. How does a function create a closure?

  1. It reloads the document whenever the value changes.
  2. It returns a reference to a variable in its parent scope.
  3. It completes execution without returning.
  4. It copies a local variable to the global scope.

Answer

2

Reference


Q6. Which statements(multiple options) creates a new function called discountPrice?

A

let discountPrice = function (price) {
  return price * 0.85;
};
Enter fullscreen mode Exit fullscreen mode

B

let discountPrice(price) {
  return price * 0.85;
};
Enter fullscreen mode Exit fullscreen mode

C

let function = discountPrice(price) {
  return price * 0.85;
};
Enter fullscreen mode Exit fullscreen mode

D

discountPrice = function (price) {
  return price * 0.85;
};
Enter fullscreen mode Exit fullscreen mode

Answer

A & D

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);
Enter fullscreen mode Exit fullscreen mode
  1. Storm()
  2. undefined
  3. 'rain'
  4. 'snow'

Answer

4

Reference prototype chain


Q8. What is the result in the console of running this code?

'use strict';
function logThis() {
  this.desc = 'logger';
  console.log(this);
}
new logThis();
Enter fullscreen mode Exit fullscreen mode
  1. undefined
  2. window
  3. {desc: "logger"}
  4. function

Answer

3

Reference javascript classes


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)) {
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. exception('One or both parameters are not numbers')
  2. catch('One or both parameters are not numbers')
  3. error('One or both parameters are not numbers')
  4. throw('One or both parameters are not numbers')

Answer

4

Reference javascript throw


Q10. When would you use a conditional statement?

  1. When you want to reuse a set of statements multiple times.
  2. When you want your code to choose between multiple options.
  3. When you want to group data together.
  4. When you want to loop through a group of statement.

Answer

2

Reference javascript conditionals


Q11. Which Object method returns an iterable that can be used to iterate over the properties of an object?

  1. Object.get()
  2. Object.loop()
  3. Object.each()
  4. Object.keys()

Answer

4

Reference javascript object static methods


Q12. What is one difference between collections created with Map and collections created with Object?

  1. You can iterate over values in a Map in their insertion order.
  2. You can count the records in a Map with a single method call.
  3. Keys in Maps can be strings.
  4. You can access values in a Map without iterating over the whole collection.

Answer

2 (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

  1. ReferenceError
  2. true
  3. 0
  4. false

Answer

3

Reference boolean logic


Q14. Which of the following values is not a Boolean false?

  1. Boolean(0)
  2. Boolean("")
  3. Boolean(NaN)
  4. Boolean("false")

Answer

4

Reference boolean of a string


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();
Enter fullscreen mode Exit fullscreen mode
  1. x.get('Y')
  2. x.Y
  3. x.Y()
  4. x.get().Y

Answer

2

Reference getters


Post your result in the comment section and let me know how was the quiz🙌👍

Top comments (2)

Collapse
 
dagnelies profile image
Arnaud Dagnelies • Edited

I didn't went beyond Q4. It marks the following answer as correct:

for (var i = 1; i <= 4; i++) {
  (function (j) {
    setTimeout(function () {
      console.log(j);
    }, j * 1000);
  })(i);
}
Enter fullscreen mode Exit fullscreen mode

However, just changing the var into let would get rid of the issue and be much simpler IMHO:

for (let i = 1; i <= 4; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 1000);
}
Enter fullscreen mode Exit fullscreen mode

...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 contrived var loops using closures.

Collapse
 
arafat4693 profile image
Arafat • Edited

Nice, still you tried. đź‘Ť I put var to remind people that there is another type of variable besides let and const