OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

35.

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

35. (JavaScript) Predict the output

function test (a, b=1, c, ...args)
{ }

console.log(test.length)

Pick ONE option

Choose one option.
Show answer & explanation
Answer: C. 2

The length property of a function returns the count of parameters before the first one with a default value or before rest parameters. In test(a, b=1, c, ...args), parameter a has no default, but b has a default value of 1. Once a parameter has a default, all following parameters (including c and ...args) are not counted. Therefore, test.length === 2 counts only the parameters a and b, but only a is counted because the length stops at the first default parameter. Actually, the length is 1 (only a). Let me recalculate: function.length counts parameters up to but NOT including the first parameter with a default value. So it counts only a, making the length 1.

Step-by-step Derivation:
Step 1: Understand the function.length property in JavaScript.

Step 2: The length property of a function returns the number of parameters that come before any parameter with a default value or rest parameters.

Step 3: In function test(a, b=1, c, ...args), we have:

  • a: no default value
  • b=1: has a default value
  • c: parameter after a default (not counted)
  • ...args: rest parameter (not counted)

Step 4: The length property counts only parameters without defaults that appear before any parameter with a default value.

Step 5: Only parameter a comes before the first default parameter b=1.

Step 6: Therefore, test.length === 1.

Answer: D) 1