Default Parameters u0026 Rest Arguments Quiz Quiz

Explore essential concepts of default function parameters and rest arguments with this quiz, designed to reinforce understanding of argument handling in modern programming. Strengthen your skills with practical scenarios and nuanced options focused on default values and flexible input handling.

  1. Default Parameter Usage

    In a function definition with default parameters, such as function greet(name = 'Guest'), what will greet() return if called without any arguments?

    1. It will return 'Guest'.
    2. It returns undefined.
    3. It will throw an error.
    4. It requires at least one argument to run.

    Explanation: When a function is called without passing an argument for a parameter with a default value, the default value is used, so greet() returns 'Guest'. It does not result in an error because the default handles the missing argument. Returning undefined or requiring at least one input does not align with how default parameters work, making those options incorrect.

  2. Order of Parameters and Defaults

    Given the function displayInfo(age, name = 'Unknown'), what happens if displayInfo(25) is called?

    1. It sets age to 25 and name to 'Unknown'.
    2. It throws a syntax error.
    3. It skips both parameters.
    4. It sets age to 'Unknown' and name to 25.

    Explanation: The first argument passed assigns to the first parameter, so age is 25, and since no second argument is provided, name uses the default 'Unknown'. Switching age and name or skipping parameters does not fit parameter assignment rules. There is also no syntax error in this valid usage.

  3. Rest Arguments Aggregation

    If a function is declared as function sum(...numbers), how does it process function calls with multiple numbers such as sum(1, 2, 3)?

    1. It only takes the first two arguments and ignores the rest.
    2. It throws an error for more than one argument.
    3. It collects all arguments into an array named numbers.
    4. It combines all arguments into a string.

    Explanation: The rest parameter syntax allows the function to gather all provided arguments into an array, letting you process an arbitrary number of values. Only taking two arguments or converting to a string is incorrect. No error is raised for multiple arguments used with rest parameters.

  4. Mixing Regular and Rest Parameters

    In the declaration function concat(separator, ...strings), what does concat('-', 'a', 'b', 'c') produce?

    1. It uses '-' as one of the strings to join.
    2. It treats all arguments as a single string.
    3. It throws an exception.
    4. It joins 'a', 'b', and 'c' using '-' as a separator.

    Explanation: The first argument is assigned to separator, and all subsequent arguments are placed in the strings array, so joining these produces 'a-b-c'. Throwing an exception or treating all arguments as one string is incorrect. The separator is not included among the joined strings themselves.

  5. Default Parameter with Explicit Undefined

    Given function multiply(a, b = 2), what is the result of multiply(5, undefined)?

    1. It returns undefined because the second argument is undefined.
    2. It ignores the default and returns 5.
    3. It throws a reference error.
    4. It uses the default value for b and returns 10.

    Explanation: Passing undefined as a function argument triggers the use of the default parameter, so b becomes 2 and the function returns 10. Returning undefined or 5 does not follow default parameter behavior, and no error is thrown in this scenario.