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

All questions of Functions for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

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.

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?

Explanation:
When the above code is executed, it will output "undefined" in the console.

Reasoning:
To understand why "undefined" is the output, let's break down the code step by step:

1. The function outer() is defined.
2. Inside the outer() function, a variable x is declared and assigned a value of 10.
3. The function inner() is defined.
4. Inside the inner() function, a console.log(x) statement is executed. At this point, the variable x is still in the temporal dead zone (TDZ) since it has been declared but not yet assigned a value.
5. Next, the variable x is declared and assigned a value of 20. This assignment happens after the console.log(x) statement.
6. Finally, the inner() function is invoked.

Hoisting and Scoping:
The reason why the code outputs "undefined" instead of 10 is due to a concept called hoisting and the scoping rules in JavaScript.

- Hoisting: JavaScript moves all variable and function declarations to the top of their respective scopes during the creation phase. However, only the declarations are hoisted, not the assignments. In our code, the variable x is hoisted to the top of the inner() function, but its assignment of 20 is not hoisted.
- Scoping: JavaScript has function-level scoping. This means that variables declared inside a function are only accessible within that function, not outside. In our code, the variable x inside the inner() function shadows the variable x in the outer() function.

Order of Execution:
1. When the inner() function is invoked, the console.log(x) statement is executed. At this point, the variable x is in the TDZ and has not been assigned a value yet, so it is undefined.
2. After the console.log(x) statement, the variable x is assigned a value of 20.
3. Since the variable x inside the inner() function only exists within the function's scope, it does not affect the variable x in the outer() function.
4. Finally, when the outer() function is invoked, it calls the inner() function, which outputs "undefined" to the console.

Therefore, the correct answer is option C) Undefined.

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?

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. Calling "updateX()" updates the value of the global "x". Therefore, the output is 10.

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?

CodeNation answered
The code declares a global variable "x" with the value 5. The function "updateX" declares a local variable "x" with the value 10 but does not modify the global variable. Calling "updateX()" has no effect on the value of the global "x". Therefore, the output is 5.

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?
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?

Mira Sengupta answered
Explanation:
- The code snippet provided defines a function called `outer()` that contains a variable `x` with a value of 10.
- Inside the `outer()` function, there is another function called `inner()` that simply logs the value of `x` to the console.
- The `inner()` function is then returned from the `outer()` function.
- The line `var closureFn = outer();` invokes the `outer()` function and assigns the returned `inner()` function to the variable `closureFn`.
- Finally, the line `closureFn();` calls the `inner()` function stored in the `closureFn` variable.

Output:
The output of the code will be `10`.

Explanation:
- When the `inner()` function is returned from the `outer()` function, it forms a closure.
- A closure is a combination of a function and the lexical environment within which that function was declared.
- In this case, the `inner()` function has access to the variable `x` from its parent function `outer()`, even after the `outer()` function has finished executing.
- When the `closureFn()` function is called, it executes the `inner()` function and logs the value of `x` to the console, which is `10`.
- This is because the `inner()` function maintains a reference to its outer environment, including the variable `x`.
- Therefore, the value of `x` is accessible to the `inner()` function even though it is no longer in scope.
- This behavior is characteristic of closures in JavaScript, where functions retain access to variables from their containing scope even after the outer function has completed execution.

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?

Simar Sharma answered
The code defines a function expression named "calculate" that takes three parameters: "a", "b", and "operation". The "operation" parameter is expected to be a function. In this case, the "add" function is passed as an argument to "calculate". The "calculate" function then calls the "operation" function with the values of "a" and "b". So, "calculate(3, 4, add)" is equivalent to "add(3, 4)", which returns the sum of 7. The result is then logged to the console.

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?

Gaurav Khanna answered
Explanation:

Initial Setup:
- The variable x is declared and assigned a value of 5.
- A function named updateX is defined, which updates the value of x to 10.

setTimeout Function:
- The setTimeout function is used to delay the execution of the updateX function by 1000 milliseconds (1 second).

Console Logging:
- The value of x is logged to the console before the setTimeout function has a chance to execute the updateX function.

Output:
- As the updateX function is executed after a delay of 1 second, the value of x remains 5 when it is logged to the console.
- Therefore, the output of the code will be 5.

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 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.

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 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!".

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 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.

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?
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.

Chapter doubts & questions for Functions - JavaScript for Web Development 2024 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 2024 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

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev