let myObject = {
name: 'Sample Object',
foo: function() {
return this.name + ' - foo';
}
};
console.log(myObject.foo());
JavaScript Functions and Scope Quiz
Want to learn more than this quiz offers you? Have a look at my Frontend web
development courses.
Create an account and save your quiz results
Login and save your results
OR
Question 1/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 2/15
What does the following function return when called with argument 5?
function increment(x) {
return ++x;
}
Select your answer
Question 3/15
What will be the output of the following code snippet?
var x = 1;
function outer() {
var x = 2;
function inner() {
var x = 3;
console.log(x);
}
inner();
}
outer();
Select your answer
Question 4/15
What is the effect of `const` on the variable `num` in this snippet?
const num = 7;
num = 10;
Select your answer
Question 5/15
Consider the below code. What will be logged to the console?
let y = 10;
function modifyVariable() {
y += 5;
console.log(y);
}
modifyVariable();
Select your answer
Question 6/15
Which of the following code snippets correctly declares an IIFE?
// Choose the correct format
Select your answer
Question 7/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 8/15
For this code, what is returned by `compute()`?
function compute() {
let a = 3;
let b = 4;
return a + b;
}
Select your answer
Question 9/15
Consider the following snippet. What will `calculate(2, 3)` return?
function calculate(a, b = 1) {
return a * b;
}
Select your answer
Question 10/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 11/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 12/15
What value will the variable `result` hold?
function multiplier(a, b = 2) {
return a * b;
}
let result = multiplier(4);
Select your answer
Question 13/15
What will be the output of this immediately invoked function expression?
(function() {
var a = b = 5;
})();
console.log(typeof a);
console.log(typeof b);
Select your answer
Question 14/15
What is the impact of calling `comparison(5, '5')` in this code?
function comparison(x, y) {
return x == y;
}
Select your answer
Question 15/15
What is the effect of calling `sum(4, 5, 6)` in the given code?
function sum(x, y, ...rest) {
return x + y;
}
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 attaching a method `foo` to the object `myObject` affect its behavior, given the following execution?
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 2/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 3/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will be the output of the following code snippet?
var x = 1;
function outer() {
var x = 2;
function inner() {
var x = 3;
console.log(x);
}
inner();
}
outer();
Available answers
The code defines three variables called x in different scopes. The innermost x (with value 3) is in the inner function's scope, and that's the one that is logged by console.log.
Question 4/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 5/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Consider the below code. What will be logged to the console?
let y = 10;
function modifyVariable() {
y += 5;
console.log(y);
}
modifyVariable();
Available answers
Since y is declared outside the function with a value of 10, and the function modifyVariable adds 5 to y, the logged value will be 15.
Question 6/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Which of the following code snippets correctly declares an IIFE?
// Choose the correct format
Available answers
An IIFE (Immediately Invoked Function Expression) can be declared with (function() {})();, enclosing the function in parentheses followed by immediate invocation.
Question 7/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 8/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 9/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 10/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 11/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 12/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 13/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will be the output of this immediately invoked function expression?
(function() {
var a = b = 5;
})();
console.log(typeof a);
console.log(typeof b);
Available answers
In the IIFE, `b` is assigned to 5 in the global scope because it's not declared with var, let, or const. `a` is only defined within the function's scope, so it is undefined outside. The console logs 'undefined' and 'number'.
Question 14/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 15/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is the effect of calling `sum(4, 5, 6)` in the given code?
function sum(x, y, ...rest) {
return x + y;
}
Available answers
The `sum` function calculates the sum of only the first two arguments, ignoring the rest, so `sum(4, 5, 6)` results in 4 + 5, outputting 9.