Insem Exam Questions: Unit 1
Core Architecture and Integration
Q1. Explain the architectural integration of JavaScript into an HTML document. Contrast inline execution with linked scripts.
The <script> element integrates execution logic into an HTML document. When the browser parser encounters a <script> tag, it pauses DOM construction to download, parse, and execute the logic.
Inline scripting embeds JavaScript directly within the <script> tags inside the HTML. This violates the separation of concerns, coupling markup with logic. It creates unmaintainable architecture and prevents browser caching.
Linked scripts decouple logic using the src attribute (e.g., <script src="app.js"></script>). This enforces clean architecture. The browser caches the external .js file, reducing bandwidth consumption and accelerating subsequent page loads. Modern integration requires the defer or async attributes. defer downloads the script in the background and delays execution until DOM parsing completes. async executes the script immediately upon download completion, regardless of the DOM state.
Q2. Detail the execution mechanics of JavaScript as a dynamic, interpreted language.
JavaScript engines (like V8) do not require explicit pre-compilation into machine code prior to execution, unlike Java or C++. They utilize Just-In-Time (JIT) compilation to parse and execute code immediately at runtime.
JavaScript utilizes dynamic typing. Variables hold any data type and do not bind to strict static types. Reassignment mutates the type dynamically. This architecture provides rapid development but demands rigorous runtime testing to prevent type errors.
Flow Control and Data Types
Q3. Differentiate between primitive and composite data types in JavaScript.
Primitive data types (Number, String, Boolean, Undefined, Null) represent the lowest level of language implementation. They are immutable. JavaScript passes primitives by value; a distinct copy of the data enters the execution context.
Composite types (Objects, Arrays) aggregate multiple values. They are mutable. JavaScript passes composites by reference. The execution context receives a memory address, not a distinct copy. Modifying a composite type alters the original object globally.
A critical architectural flaw exists: typeof null incorrectly evaluates to "object". This is an unpatchable legacy bug; Null remains a strict primitive.
Q4. Explain the execution logic of the switch statement versus complex if-else chains.
The switch statement evaluates a single expression against multiple case clauses using strict equality (===). It replaces deeply nested, unreadable if-else chains, optimizing branching logic.
Execution routes directly to the matching case. The engine executes all subsequent statements sequentially unless halted. A break statement is mandatory to terminate the block. Omitting break causes execution fall-through, triggering unintended logic in subsequent cases.
switch (status) {
case 1:
executeStart();
break; // Mandatory termination
case 2:
executeStop();
break;
default:
handleError();
}
Q5. Contrast the while loop with the do-while loop.
The while loop evaluates its condition prior to execution. If the initial evaluation is false, the loop block never executes. It risks infinite looping if the block lacks an explicit exit trigger.
The do-while loop executes the block first, then evaluates the condition. This structural difference guarantees a minimum of one execution pass. It is mandatory when initialization logic resides inside the loop body.
// while loop
while (dataPending) {
fetchData(); // May execute 0 times
}
// do-while loop
do {
fetchData(); // Guaranteed to execute at least 1 time
} while (dataPending);