JavaScript Functions and Scope Quiz

Create an account and save your quiz results

Login and save your results

OR

Question 1/15

What will the function `returnSum` return when called with no arguments?
function returnSum(a = 1, b = 1) {
  return a + b;
}

Select your answer

Question 2/15

Given the function declaration, which option correctly calls the function with 3 as an argument?
function printNumber(n) {
  console.log(n);
}

Select your answer

Question 3/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 4/15

Consider the following snippet. What will `calculate(2, 3)` return?
function calculate(a, b = 1) {
  return a * b;
}

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

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 7/15

What is the effect of `const` on the variable `num` in this snippet?
const num = 7;
num = 10;

Select your answer

Question 8/15

What does the following code return?
function testScope() {
  console.log(a);
  var a = 5;
  return a;
}

Select your answer

Question 9/15

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

Select your answer

Question 10/15

In the following snippet, what is the value of `z` after `calculate()` is executed?
function calculate() {
  let x = 0;
  const y = 1;
  const z = x + y + 5;
  return z;
}
calculate();

Select your answer

Question 11/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 12/15

What will be returned by the arrow function in the following code?
let add = (a, b) => a + b;
let result = add(3, 4);

Select your answer

Question 13/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 14/15

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

Select your answer

Question 15/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

Your Results

You did not answer any questions correctly.

Your Answers

Question 1/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will the function `returnSum` return when called with no arguments?
function returnSum(a = 1, b = 1) {
  return a + b;
}

Available answers

Both parameters have default values of 1. If called with no arguments, `returnSum` returns 1 + 1, which is 2.
Question 2/15
😊 Your answer was correct 🙁 Your answer was incorrect
Given the function declaration, which option correctly calls the function with 3 as an argument?
function printNumber(n) {
  console.log(n);
}

Available answers

`printNumber(3);` correctly calls the function with 3 as an argument. The other options are either incorrect syntax or misassignments.
Question 3/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 4/15
😊 Your answer was correct 🙁 Your answer was incorrect
Consider the following snippet. What will `calculate(2, 3)` return?
function calculate(a, b = 1) {
  return a * b;
}

Available answers

The function `calculate` multiplies its two arguments. The second argument `b` defaults to 1 if not provided. Calling calculate(2, 3) multiplies 2 by 3, which results in 6.
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
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 7/15
😊 Your answer was correct 🙁 Your answer was incorrect
What is the effect of `const` on the variable `num` in this snippet?
const num = 7;
num = 10;

Available answers

Since `num` is declared with `const`, it cannot be reassigned without resulting in a TypeError.
Question 8/15
😊 Your answer was correct 🙁 Your answer was incorrect
What does the following code return?
function testScope() {
  console.log(a);
  var a = 5;
  return a;
}

Available answers

Due to JavaScript's hoisting, the variable `a` is hoisted to the top of the function with an initial value of `undefined`. Thus, console.log(a) prints `undefined` before `a` is assigned to 5.
Question 9/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 10/15
😊 Your answer was correct 🙁 Your answer was incorrect
In the following snippet, what is the value of `z` after `calculate()` is executed?
function calculate() {
  let x = 0;
  const y = 1;
  const z = x + y + 5;
  return z;
}
calculate();

Available answers

The function computes the expression `x + y + 5`, where x is 0 and y is 1. Thus, the z value will be 0 + 1 + 5, which is 6.
Question 11/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 12/15
😊 Your answer was correct 🙁 Your answer was incorrect
What will be returned by the arrow function in the following code?
let add = (a, b) => a + b;
let result = add(3, 4);

Available answers

The arrow function add takes two arguments and returns their sum. When called with 3 and 4, it returns 7.
Question 13/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 14/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 15/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'.