Endsem Exam Questions: Unit 3

Functions and Parameter Passing

Q1. Contrast pass-by-value and pass-by-reference execution in JavaScript.

JavaScript executes pass-by-value strictly for primitive types (Number, String, Boolean). The execution engine sends a discrete copy of the data into the function scope. Modifications to this parameter inside the function do not alter the original variable in the global scope.

JavaScript executes pass-by-reference for composite types (Objects, Arrays). The function does not receive a copy of the object; it receives the memory address. Modifying the properties of the passed object within the local function permanently mutates the original object in the global state. This architecture demands rigorous side-effect management.

Q2. Explain the necessity of the base condition in a recursive function.

A recursive function calls itself to execute sub-operations. The call stack allocates memory for each subsequent execution context.

The base condition is a strict logical check that halts recursion. Omitting the base condition guarantees an infinite loop. The engine repeatedly calls the function until it exhausts allocated memory, triggering a stack overflow exception and crashing the application.

function factorial(n) {
    if (n === 0) return 1; // Base condition prevents stack overflow
    return n * factorial(n - 1);
}

Q3. Define an anonymous function and explain its architectural utility.

An anonymous function is a function literal declared without an internal identifier. It lacks a specific name.

Its architectural utility lies in preventing global namespace pollution and enabling functional programming paradigms. Developers assign anonymous functions directly to variables, pass them as arguments, or utilize them as callback functions for event handlers. They execute isolated logic without risking naming collisions in large codebases.

document.getElementById('btn').addEventListener('click', function() {
    console.log("Anonymous callback executed.");
});

Objects and Properties

Q4. Explain the execution and limitation of the for...in loop.

The for...in loop iterates dynamically over the enumerable properties of an object, extracting the keys as strings.

Its primary limitation is structural overreach. It iterates over immediate instance properties and all inherited properties residing in the prototype chain. Executing generic logic inside this loop corrupts inherited prototypes. To isolate execution safely, the developer must wrap internal logic with the hasOwnProperty() method check.

Q5. Detail the architectural function of the Math object.

The Math object is not an instantiable constructor. It operates as a static namespace encapsulating predefined mathematical constants and functions.

It executes complex arithmetic at the hardware level natively, bypassing the need for developers to write unoptimized custom algorithms. Core methods include Math.random() for pseudorandom number generation, Math.pow() for exponentiation, and trigonometry functions like Math.sin().