All Exams  >   Software Development  >   JavaScript for Web Development  >   All Questions

All questions of Functions for Software Development Exam

What will be the output of the following code?
var x = 10;
function test() {
  if (x === 10) {
    var x = 20;
  }
  console.log(x);
}
test();
  • a)
    10
  • b)
    undefined
  • c)
    20
  • d)
    Error
Correct answer is option 'B'. Can you explain this answer?

Tech Era answered
The 'var x = 20' declaration inside the 'if' statement creates a new variable 'x' with block scope. Since the 'console.log(x)' statement is outside the block, it refers to the outer variable 'x', which is undefined.

Consider the following JavaScript code:
function outer() {
  var x = 10;
  function inner() {
    console.log(x);
  }
  return inner;
}
var closureFn = outer();
closureFn();
What will be the output of the above code?
  • a)
    10
  • b)
    0
  • c)
    Undefined
  • d)
    This code will produce an error
Correct answer is option 'A'. Can you explain this answer?

Simar Sharma answered
The code defines a function named "outer" that declares a variable "x" with the value 10. Inside "outer", there is a nested function named "inner" that logs the value of "x" to the console. Finally, the "inner" function is returned from "outer" and assigned to the variable "closureFn". Calling "closureFn()" executes the "inner" function, which accesses the value of "x" from its outer scope. Therefore, the output is 10.

Consider the following JavaScript code:
var x = 5;
function updateX() {
  x = 10;
}
setTimeout(updateX, 1000);
console.log(x);
What will be the output of the above code?
  • a)
    5
  • b)
    10
  • c)
    Undefined
  • d)
    This code will produce an error
Correct answer is option 'A'. Can you explain this answer?

Simar Sharma answered
The code declares a global variable "x" with the value 5. The function "updateX" modifies the global variable "x" by assigning it the value 10. However, the function "updateX" is called asynchronously using "setTimeout" with a delay of 1000 milliseconds. Therefore, the console log is executed before the "updateX" function has a chance to modify the value of "x". Hence, the output is 5.

What will be the output of the following JavaScript code?
var x = 5;
function updateX() {
  var x = 10;
}
updateX();
console.log(x);
  • a)
    5
  • b)
    10
  • c)
    Undefined
  • d)
    This code will produce an error
Correct answer is option 'A'. Can you explain this answer?

Gaurav Khanna answered
Explanation:

Variable Scope:
- In the given code, there are two instances of the variable `x`.
- The first one is declared globally with a value of 5, and the second one is declared inside the `updateX` function with a value of 10.
- When `updateX` function is called, it creates a new local variable `x` with a value of 10, but it does not modify the global variable `x`.

Output:
- When `console.log(x)` is executed after calling the `updateX` function, it prints the value of the global variable `x`, which is 5.
- Therefore, the output of the code will be `5`.

Consider the following JavaScript code:
var calculate = function(a, b, operation) {
  return operation(a, b);
};
function add(a, b) {
  return a + b;
}
var result = calculate(3, 4, add);
console.log(result);
What will be the output of the above code?
  • a)
    3
  • b)
    4
  • c)
    7
  • d)
    This code will produce an error
Correct answer is option 'C'. Can you explain this answer?

Arka Banerjee answered
Explanation:
- The given code defines a function called `calculate` which takes three parameters: `a`, `b`, and `operation`.
- The `calculate` function returns the result of calling the `operation` function with `a` and `b` as arguments.
- The code also defines a function called `add` which takes two parameters: `a` and `b`.
- The `add` function returns the sum of `a` and `b`.
- Lastly, the code calls the `calculate` function with the arguments `3`, `4`, and the `add` function.
- The result of this calculation is then printed to the console using `console.log`.

Execution:
1. The `calculate` function is called with the arguments `3`, `4`, and the `add` function.
2. Inside the `calculate` function, the `operation` function (in this case, `add`) is called with `a` and `b` as arguments.
3. The `add` function calculates the sum of `a` and `b`, which is `3 + 4 = 7`.
4. The `calculate` function returns the result of the `add` function, which is `7`.
5. The returned result is stored in the `result` variable.
6. The `console.log` function prints the value of the `result` variable to the console.
7. Therefore, the output of the code will be `7`.

Conclusion:
The output of the given JavaScript code will be `7`.

What will be the output of the following JavaScript code?
var x = 5;
function updateX() {
  x = 10;
}
updateX();
console.log(x);
  • a)
    5
  • b)
    10
  • c)
    Undefined
  • d)
    This code will produce an error
Correct answer is option 'B'. Can you explain this answer?

Gaurav Khanna answered
Explanation:

Variable Declaration:
- The code declares a variable x and assigns it a value of 5.

Function Declaration:
- A function named updateX is declared which sets the value of x to 10.

Function Call:
- The updateX function is called.

Console Output:
- Finally, the value of x is logged to the console using console.log(x).

Output:
- The output of this code will be 10 because the updateX function is called which updates the value of x to 10.
Therefore, the correct answer is 10.

Consider the following JavaScript code:
function outer() {
  var x = 10;
  function inner() {
    console.log(x);
    var x = 20;
  }
  inner();
}
outer();
What will be the output of the above code?
  • a)
    10
  • b)
    20
  • c)
    Undefined
  • d)
    This code will produce an error
Correct answer is option 'C'. Can you explain this answer?

Simar Sharma answered
The code defines a function named "outer" that declares a variable "x" with the value 10. Inside "outer", there is a nested function named "inner" that logs the value of "x" to the console. However, before the console log, there is a variable declaration "var x = 20". This declaration creates a new local variable "x" within the "inner" function's scope. Since JavaScript has function-level scope, the declaration of the local "x" variable shadows the outer "x" variable. Therefore, when the console log is executed, the value of the local "x" variable, which is undefined, is logged.

What will be the output of the following code?
function add(a, b) {
  return a + b;
}
function multiply(a, b) {
  return a * b;
}
var result = add(2, multiply(3, 4));
console.log(result);
  • a)
    14
  • b)
    12
  • c)
    10
  • d)
    24
Correct answer is option 'D'. Can you explain this answer?

Gaurav Joshi answered
Explanation:

Function Definitions:
- The code defines two functions, add and multiply, which perform addition and multiplication operations respectively.

Calculation:
- The add function takes two parameters a and b and returns their sum.
- The multiply function takes two parameters a and b and returns their product.
- In the expression add(2, multiply(3, 4)), the multiply function is called first with arguments 3 and 4, resulting in the product 12.
- Then, the add function is called with arguments 2 and 12, resulting in the sum 14.

Output:
- The final value 14 is stored in the variable result.
- The code then prints the value of result using console.log.
- Therefore, the output of the code will be 24.

What will be the output of the following code?
function multiply(a, b = 2) {
  return a * b;
}
console.log(multiply(3));
  • a)
    5
  • b)
    6
  • c)
    3
  • d)
    Error
Correct answer is option 'C'. Can you explain this answer?

Tech Era answered
The 'multiply' function accepts two parameters, 'a' and 'b', with 'b' having a default value of 2. When called with 'multiply(3)', it uses the default value of 'b' (2) and returns the product of 'a' (3) and 'b' (2), which is 6.

What will be the output of the following code?
function sum(a, b) {
  return a + b;
}
var result = sum(3, 4);
console.log(result);
  • a)
    7
  • b)
    34
  • c)
    12
  • d)
    undefined
Correct answer is option 'A'. Can you explain this answer?

The 'sum' function returns the sum of its two parameters. When called with 'sum(3, 4)', it returns 7, which is assigned to the 'result' variable and then printed.

What will be the output of the following JavaScript code?
function sum(a, b) {
  return a + b;
}
var result = sum(3, 4);
console.log(result);
  • a)
    3
  • b)
    4
  • c)
    7
  • d)
    This code will produce an error
Correct answer is option 'C'. Can you explain this answer?

Codebreakers answered
The code defines a function named "sum" that takes two parameters and returns their sum. Calling "sum(3, 4)" passes the values 3 and 4 to the function, which returns the sum of 7. The result is then logged to the console.

What will be the output of the following code?
var x = 5;
function test() {
  console.log(x);
  var x = 10;
}
test();
  • a)
    5
  • b)
    undefined
  • c)
    10
  • d)
    Error
Correct answer is option 'B'. Can you explain this answer?

Tanuja Mishra answered
In the 'test' function, the 'console.log(x)' statement is executed before the 'var x = 10' line. At that point, 'x' is undefined, so it prints undefined.

What will be the output of the following code?
function greet(name) {
  console.log("Hello, " + name + "!");
}
greet();
  • a)
    "Hello, undefined!"
  • b)
    "Hello, null!"
  • c)
    "Hello, !"
  • d)
    Error
Correct answer is option 'A'. Can you explain this answer?

Tech Era answered
The 'greet' function expects a parameter 'name' to be passed when called. Since no argument is provided when calling 'greet()', the 'name' parameter is undefined, resulting in the output "Hello, undefined!".

Consider the following JavaScript code:
function multiplyBy(factor) {
  return function(number) {
    return number * factor;
  };
}
var multiplyByTwo = multiplyBy(2);
var multiplyByFive = multiplyBy(5);
console.log(multiplyByTwo(3));
console.log(multiplyByFive(4));
What will be the output of the above code?
  • a)
    3, 4
  • b)
    6, 20
  • c)
    5, 8
  • d)
    This code will produce an error
Correct answer is option 'B'. Can you explain this answer?

Simar Sharma answered
The code defines a function named "multiplyBy" that takes a parameter "factor" and returns an inner function. The inner function takes a parameter "number" and multiplies it by the "factor" value captured in its closure. By calling "multiplyBy(2)", we create a closure that multiplies any number by 2. Similarly, calling "multiplyBy(5)" creates a closure that multiplies any number by 5. Therefore, "multiplyByTwo(3)" returns 6, and "multiplyByFive(4)" returns 20.

What will be the output of the following code?
function outer() {
  var x = 10;
  function inner() {
    console.log(x);
  }
  x = 20;
  return inner;
}
var fn = outer();
fn();
  • a)
    10
  • b)
    20
  • c)
    undefined
  • d)
    Error
Correct answer is option 'A'. Can you explain this answer?

Tech Era answered
The 'inner' function is defined inside the 'outer' function and has access to the variables in its parent scope, including 'x'. When 'fn()' is called, it prints the value of 'x', which is 10.

What will be the output of the following JavaScript code?
var multiply = function(a, b) {
  return a * b;
};
console.log(multiply(2, 5));
  • a)
    2
  • b)
    5
  • c)
    10
  • d)
    This code will produce an error
Correct answer is option 'C'. Can you explain this answer?

Codebreakers answered
The code defines a function expression named "multiply" that takes two parameters and returns their product. Calling "multiply(2, 5)" passes the values 2 and 5 to the function, which returns the product of 10. The result is then logged to the console.

What will be the output of the following code?
function outer() {
  var x = 10;
  function inner() {
    console.log(x);
  }
  return inner;
}
var fn = outer();
fn();
  • a)
    10
  • b)
    undefined
  • c)
    Error
  • d)
    null
Correct answer is option 'A'. Can you explain this answer?

The 'inner' function is defined inside the 'outer' function and has access to the variables in its parent scope, including 'x'. When 'fn()' is called, it prints the value of 'x', which is 10.

Chapter doubts & questions for Functions - JavaScript for Web Development 2025 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Functions - JavaScript for Web Development in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Top Courses Software Development