nerdexam
Microsoft

98-382 · Question #12

You analyze the following code fragment. Line numbers are included for reference only. ``javascript 01 "use strict"; 02 var val1 = 25; 03 var val2 = 4; 04 function multiply() { 05 return val1 val2…

Based on the provided JavaScript code and scoping rules: The call to the multiply function on line 09 calls the global multiply() (lines 04-06), which uses global val1 (25) and val2 (4). Therefore, 25 4 = 100. The statement 'The call to the multiply function on line 09 returns…

Perform Operations using Data Types and Operators

Question

You analyze the following code fragment. Line numbers are included for reference only.
01 "use strict";
02 var val1 = 25;
03 var val2 = 4;
04 function multiply() {
05 return val1 * val2;
06 }
07
08 console.log("Global multiply returns(): " + multiply());
09
10 function getProduct() {
11 var val1 = 2;
12 var val2 = 3;
13
14 function multiply() {
15 return val1 * val2;
16 }
17
18 return multiply();
19 }
20 }
Evaluate the truthfulness of the following statements:

Explanation

Based on the provided JavaScript code and scoping rules:

  • The call to the multiply function on line 09 calls the global multiply() (lines 04-06), which uses global val1 (25) and val2 (4). Therefore, 25 * 4 = 100. The statement 'The call to the multiply function on line 09 returns 100.' is Yes (True).
  • The call to the multiply function on line 19 calls the nested multiply() (lines 14-16) within getProduct(). This nested function uses val1 (2) and val2 (3) defined in the getProduct() scope. Therefore, 2 * 3 = 6. The statement 'The call to the multiply function on line 19 returns 100.' is No (False).

Topics

#variable scope#nested functions#closures#strict mode

Community Discussion

No community discussion yet for this question.

Full 98-382 Practice