Explore key concepts behind variadic functions and argument objects. This quiz helps you assess your understanding of calling conventions, parameter handling, and dynamic argument techniques in programming languages.
Which of the following function definitions best represents a variadic function that accepts any number of numeric parameters?
Explanation: The correct answer is function sum(...numbers) because the ellipsis indicates that the function can accept any number of arguments as an array. The option function sum(numbers, extra) only accepts two parameters and is not variadic. The option function sum([numbers]) would require a single array argument, not a variable number. Lastly, function sum(number1, number2) only accepts exactly two arguments and does not support variable parameters.
In some programming languages, what does the 'arguments' object typically contain within a function when the function is called with three values?
Explanation: The correct answer is an array-like object containing all passed values, as the arguments object holds each argument supplied to the function even if parameters aren't explicitly named. Only the first value as an object property is incorrect because the arguments object stores every argument, not just the first. A single string with comma-separated values is not the behavior of the arguments object. A reference to all global variables is unrelated to how function arguments are accessed.
When using a function with rest parameters and the arguments object, which of the following is true about their relationship?
Explanation: Rest parameters create a real array, making it possible to use standard array methods, whereas the arguments object is only array-like and does not have all array methods. Both are true arrays containing the same references is untrue because only rest parameters provide a true array. Arguments object always excludes parameters passed via rest parameters is incorrect; the arguments object includes all arguments regardless of how they are named. Rest parameters are not available outside the function scope, so that answer is invalid.
If a variadic function designed with rest parameters is called with five arguments, how many elements will the rest parameter contain?
Explanation: The rest parameter collects all supplied arguments beyond those explicitly listed, so if the function receives five arguments and the rest parameter is meant to collect them all, it will contain five elements. One and two are incorrect unless the function’s signature specifically accounts for some parameters before the rest. Zero would only be correct if no arguments were passed.
Why is it generally recommended to use rest parameters over the arguments object in modern programming languages?
Explanation: Rest parameters create actual arrays, making them safer and easier to handle, and their behavior is predictable. The arguments object does not necessarily use more CPU resources than rest parameters, so that is not a primary reason. Rest parameters do not automatically prevent syntax errors. The arguments object does not restrict argument types to strings; it can represent arguments of any data type.