JavaScript Functions and Scope Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/15

How will the use of `let` affect the availability of variable `z` in the following code?
{
  let z = 10;
  console.log(z);
}
console.log(typeof z);

Select your answer

Question 2/15

In this function declaration, which variable(s) are accessible inside `innerFunction`?
function outerFunction() {
  var outerVar = 'outer';
  function innerFunction() {
    console.log(outerVar);
  }
}

Select your answer

Question 3/15

In this code, what will be in the variable `power` after calling `getPower`?
function getPower(base, exponent = 2) {
  return Math.pow(base, exponent);
}
let power = getPower(5);

Select your answer

Question 4/15

What value will the variable `result` hold?
function multiplier(a, b = 2) {
  return a * b;
}
let result = multiplier(4);

Select your answer

Question 5/15

What does the following function return when called with argument 5?
function increment(x) {
  return ++x;
}

Select your answer

Question 6/15

Which statement is correct about the `outerFunction` in this code?
function outerFunction() {
  var x = 'outer-local';
  function innerFunction() {
    var x = 'inner-local';
    console.log(x);
  }
  innerFunction();
}
outerFunction();

Select your answer

Question 7/15

What is the impact of calling `comparison(5, '5')` in this code?
function comparison(x, y) {
  return x == y;
}

Select your answer

Question 8/15

How will attaching a method `foo` to the object `myObject` affect its behavior, given the following execution?
let myObject = {
  name: 'Sample Object',
  foo: function() {
    return this.name + ' - foo';
  }
};
console.log(myObject.foo());

Select your answer

Question 9/15

What will executing the following code return?
let result = (function greet(name) {
  return 'Hello ' + name;
})('Alice');

Select your answer

Question 10/15

What will be logged after running the following code?
function logger(x = 'default') {
  console.log(x);
}
logger();

Select your answer

Question 11/15

For this code, what is returned by `compute()`?
function compute() {
  let a = 3;
  let b = 4;
  return a + b;
}

Select your answer

Question 12/15

What will be logged to the console by the code below?
let count = 0;
(function() {
  count += 2;
  console.log(count);
})();

Select your answer

Question 13/15

What is the result of `doCount(3)` in this snippet?
function doCount(val) {
  for (var i = 0; i < val; i++) {
  }
  return i;
}

Select your answer

Question 14/15

In the code snippet below, what is the value of result?
function square(x) {
  return x * x;
}
let result = square(3) + square(4);

Select your answer

Question 15/15

What will the following code log in the console?
let car = {
  make: 'Toyota',
  year: 2019,
};
car.getAge = function() {
  const currentYear = new Date().getFullYear();
  return currentYear - this.year;
};
console.log(car.getAge());

Select your answer

Your Results

You did not answer any questions correctly.

Your Answers

Question 1/15
😊 Your answer was correct 🙁 Your answer was incorrect
How will the use of `let` affect the availability of variable `z` in the following code?
{
  let z = 10;
  console.log(z);
}
console.log(typeof z);

Available answers

The block scope defined by the braces makes `z` accessible only within the block due to `let`. Thus, it logs 10 within and undefined outside.
Question 2/15
😊 Your answer was correct 🙁 Your answer was incorrect
In this function declaration, which variable(s) are accessible inside `innerFunction`?
function outerFunction() {
  var outerVar = 'outer';
  function innerFunction() {
    console.log(outerVar);
  }
}

Available answers

`innerFunction` has access to variables within its parent's scope due to closures, allowing access to `outerVar`.
Question 3/15
😊 Your answer was correct 🙁 Your answer was incorrect
In this code, what will be in the variable `power` after calling `getPower`?
function getPower(base, exponent = 2) {
  return Math.pow(base, exponent);
}
let power = getPower(5);

Available answers

The function `getPower` defaults `exponent` to 2 if not provided, so `Math.pow(5, 2)` computes 5^2, which equals 25.
Question 4/15
😊 Your answer was correct 🙁 Your answer was incorrect
What value will the variable `result` hold?
function multiplier(a, b = 2) {
  return a * b;
}
let result = multiplier(4);

Available answers

The function `multiplier` defaults `b` to 2 if not provided. The call `multiplier(4)` is thus 4 * 2, resulting in 8.
Question 5/15
😊 Your answer was correct 🙁 Your answer was incorrect
What does the following function return when called with argument 5?
function increment(x) {
  return ++x;
}

Available answers

The function uses the prefix increment operator, which increases the value of x by one before the value is returned. So, calling increment(5) returns 6.
Question 6/15
😊 Your answer was correct 🙁 Your answer was incorrect
Which statement is correct about the `outerFunction` in this code?
function outerFunction() {
  var x = 'outer-local';
  function innerFunction() {
    var x = 'inner-local';
    console.log(x);
  }
  innerFunction();
}
outerFunction();

Available answers

`innerFunction` accesses its own local variable `x` which is 'inner-local'. Thus, `outerFunction` logs 'inner-local'.
Question 7/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is the impact of calling `comparison(5, '5')` in this code?
function comparison(x, y) {
  return x == y;
}

Available answers

The loose equality `==` allows type coercion, so `comparison(5, '5')` evaluates to true.
Question 8/15
😊 Your answer was correct 🙁 Your answer was incorrect
How will attaching a method `foo` to the object `myObject` affect its behavior, given the following execution?
let myObject = {
  name: 'Sample Object',
  foo: function() {
    return this.name + ' - foo';
  }
};
console.log(myObject.foo());

Available answers

The method `foo` returns the object's `name` property concatenated with ' - foo'. Since `this` refers to `myObject`, it logs 'Sample Object - foo'.
Question 9/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will executing the following code return?
let result = (function greet(name) {
  return 'Hello ' + name;
})('Alice');

Available answers

The IIFE is immediately invoked with 'Alice', returning 'Hello Alice' which is stored in `result`.
Question 10/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will be logged after running the following code?
function logger(x = 'default') {
  console.log(x);
}
logger();

Available answers

Function `logger` is called without arguments, making `x` default to 'default', which is logged.
Question 11/15
😊 Your answer was correct 🙁 Your answer was incorrect
For this code, what is returned by `compute()`?
function compute() {
  let a = 3;
  let b = 4;
  return a + b;
}

Available answers

`compute` evaluates the sum of `a` and `b`, which is 3 + 4, resulting in 7.
Question 12/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will be logged to the console by the code below?
let count = 0;
(function() {
  count += 2;
  console.log(count);
})();

Available answers

The IIFE increments `count` by 2 and logs it. Since `count` started at 0, it logs 2.
Question 13/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is the result of `doCount(3)` in this snippet?
function doCount(val) {
  for (var i = 0; i < val; i++) {
  }
  return i;
}

Available answers

After the for loop completes, `i` has been incremented to 3, so `doCount(3)` returns 3.
Question 14/15
😊 Your answer was correct 🙁 Your answer was incorrect
In the code snippet below, what is the value of result?
function square(x) {
  return x * x;
}
let result = square(3) + square(4);

Available answers

The function square returns the square of its argument. Thus, square(3) returns 9 and square(4) returns 16. Hence, result is 9 + 16, which is 25.
Question 15/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will the following code log in the console?
let car = {
  make: 'Toyota',
  year: 2019,
};
car.getAge = function() {
  const currentYear = new Date().getFullYear();
  return currentYear - this.year;
};
console.log(car.getAge());

Available answers

`car.getAge` method calculates and returns the difference between the current year and the car's manufacture year, 2019.