Insem Exam Questions: Unit 2
Scope and Variable Management
Q1. Define variable masking (shadowing) and explain its structural impact on execution scope.
Variable masking occurs when a local variable declared inside a function shares the identical identifier as a global variable. The JavaScript engine resolves variables by searching from the innermost scope outward. The local variable intercepts the search.
This impacts execution by rendering the global variable inaccessible from within that specific function block. The global variable remains structurally intact and its value is not overwritten, but the function’s logic operates exclusively on the local shadow variable.
let target = 100; // Global variable
function calculate() {
let target = 50; // Local variable masks the global
return target * 2; // Executes using 50
}
Q2. Contrast var, let, and const declarations.
var dictates function-level or global-level scoping. It is vulnerable to hoisting; the engine processes the declaration before execution, causing variables to be accessible before their line of initialization, resulting in undefined behavior. Modern architecture rejects var.
let enforces block-level scoping. It limits variable accessibility to the immediate enclosing brackets {}. It provides a mutable reference.
const enforces block-level scoping and dictates an immutable binding. The variable cannot be reassigned to a new value or memory address. However, const does not freeze composite objects. If a const holds an object reference, the object’s internal properties remain entirely mutable.
Operators and Coercion
Q3. Explain implicit type coercion in JavaScript.
Implicit type coercion is the automatic conversion of data types by the JavaScript engine when evaluating expressions that combine mismatched operands.
- Adding a string and a number triggers concatenation:
"5" + 2evaluates to the string"52". - Subtracting a string from a number triggers numerical coercion:
"5" - 2evaluates to the number3.
Coercion introduces silent logic failures. Systems must utilize explicit type conversion functions (Number(), String()) to guarantee data integrity and bypass unpredictable engine assumptions.
Q4. Detail the functionality of bitwise operators.
Bitwise operators manipulate the 32-bit binary representation of numeric values. They execute rapid mathematical operations directly at the hardware level.
&(AND): Returns a 1 in each bit position where both operands have a 1.|(OR): Returns a 1 in each bit position where either operand has a 1.^(XOR): Returns a 1 in each bit position where only one operand has a 1.~(NOT): Inverts the bits of the operand.>>>(Zero-fill Right Shift): Shifts bits to the right and pushes zeros in from the left, explicitly discarding the sign bit.
Q5. Explain the execution of the for...in loop and its structural vulnerability.
The for...in loop iterates over the enumerable properties of an object. It extracts property keys as strings.
Its structural vulnerability stems from traversing the entire prototype chain. It iterates over inherited properties alongside direct instance properties. Executing logic indiscriminately inside a for...in loop corrupts data processing by modifying inherited prototypes. Developers must execute the hasOwnProperty() method within the loop to isolate properties strictly belonging to the immediate object instance.