LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Exercise 2 of 4

Scope and Hoisting Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.

Question 11 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
var count = 5;
(function () {
  var count = 1;
  count++;
})();
console.log(count);
A 2
B 6
C 1
D 5
Question page & discussion
Question 12 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function makeLogger() {
  const prefix = "[LF]";
  return (msg) => `${prefix} ${msg}`;
}
const prefix = "[OTHER]";
console.log(makeLogger()("ready"));
A [OTHER] ready
B undefined ready
C ReferenceError
D [LF] ready
Question page & discussion
Question 13 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function getValue() {
  return value;
}
let value = "set";
console.log(getValue());
A undefined
B ReferenceError: Cannot access 'value' before initialization
C null
D set
Question page & discussion
Question 14 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const x = "outer";
function f(a = x) {
  const x = "inner";
  return a;
}
console.log(f());
A inner
B ReferenceError
C outer
D undefined
Question page & discussion
Question 15 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function f(x) {
  {
    let x = 2;
    x *= 10;
  }
  return x;
}
console.log(f(1));
A 20
B 2
C 1
D SyntaxError
Question page & discussion
Question 16 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
var fns = [];
for (var i = 0; i < 3; i++) {
  let j = i;
  fns.push(() => j);
}
console.log(fns.map((f) => f()), i);
A [ 3, 3, 3 ] 3
B [ 0, 1, 2 ] 2
C [ 2, 2, 2 ] 3
D [ 0, 1, 2 ] 3
Question page & discussion
Question 17 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const Animal = class Pet {
  whoAmI() {
    return Pet.name;
  }
};
console.log(new Animal().whoAmI(), typeof Pet);
A Animal function
B Pet function
C ReferenceError: Pet is not defined
D Pet undefined
Question page & discussion
Question 18 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
function f() {
  x = 5;
  {
    var x;
  }
  return x;
}
console.log(f(), typeof x);
A 5 number
B undefined undefined
C ReferenceError
D 5 undefined
Question page & discussion
Question 19 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
let rate = 5;
const interest = (p) => (p * rate) / 100;
rate = 8;
console.log(interest(1000));
A 80
B 50
C 130
D NaN
Question page & discussion
Question 20 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
var a = "A";
function one() {
  var a = "B";
  function two() {
    function three() {
      return a;
    }
    return three();
  }
  return two();
}
console.log(one(), a);
A A A
B B B
C B A
D undefined A
Question page & discussion
Up next · Exercise 3Find the Error15 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links