




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive practice set for developers wanting mastery over modern JavaScript. Topics include asynchronous programming, closures, event loop mechanics, ES6+ features, advanced DOM manipulation, functional programming, modular architecture, Node.js fundamentals, API integration, and automated testing. Complex coding scenarios mirror real full-stack development challenges.
Typology: Exams
1 / 137
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which of the following statements about the global execution context in JavaScript is true? A) It is created only when a script is executed in strict mode. B) Its variable object is the same as the global object. C) It does not have a this binding. D) It is destroyed after the first function call. Answer: B Explanation: In the global execution context, the variable object is the global object (window in browsers), making all global variables properties of that object. Question 2. In which phase of the execution context are function declarations hoisted? A) Creation phase B) Execution phase C) Garbage‑collection phase D) Compilation phase Answer: A Explanation: During the creation phase, the engine scans the code and hoists function declarations so they are available throughout the entire context. Question 3. What will be logged by the following code?
function foo() { console.log(this); } foo.call(null); A) null B) undefined
C) Global object (window in browsers) D) this is a SyntaxError Answer: C Explanation: In non‑strict mode, this defaults to the global object when null or undefined is passed to call/apply. Question 4. Which of the following correctly describes the Temporal Dead Zone (TDZ)? A) Variables declared with var are inaccessible before their declaration. B) let and const variables are in TDZ from the start of their enclosing block until their declaration is evaluated. C) TDZ only applies to function parameters. D) TDZ is a runtime error that occurs when accessing this before super() is called. Answer: B Explanation: The TDZ is the period between entering a block and the point where a let or const variable is initialized, during which accessing the variable throws a ReferenceError. Question 5. Which code snippet creates a closure that encapsulates a private counter? A)
function counter(){ let i=0; return function(){ return ++i; }; } B)
let i=0; function counter(){ return ++i; } C)
Answer: A Explanation: Passing null to Object.create creates an object with no prototype chain, useful for creating pure dictionaries. Question 8. Which of the following statements about ES6 class inheritance is correct? A) The extends keyword copies all prototype properties from the parent to the child. B) The super() call must appear before any use of this in the subclass constructor. C) Classes are hoisted like function declarations. D) Private fields are automatically inherited. Answer: B Explanation: In a subclass constructor, super() must be called before accessing this, as it initializes the parent class. Question 9. Which of the following will return true?
let a = 0; let b = '0'; A) a == b B) a === b C) Object.is(a, b) D) a === Number(b) Answer: A Explanation: == performs type coercion; '0' is converted to number 0, making the comparison true. === and Object.is compare without coercion.
Question 10. Which method adds a new element to a Set and returns the set itself? A) addItem() B) push() C) add() D) append() Answer: C Explanation: Set.prototype.add(value) inserts the value and returns the set, enabling method chaining. Question 11. Which of the following correctly implements an iterator for a custom object? A)
obj[Symbol.iterator] = function(){ let i=0; return { next(){ return {value:i++, done:false}; } }; }; B)
obj.iterator = function(){ /* ... */ }; C)
obj[Symbol.iterator] = function(){ return [1,2,3]; }; D)
obj[Symbol.iterator] = () => ({ next: () => ({done:true}) }); ## JavaScript Practice Exam Answer: C Explanation: Synchronous logs run first (`start`, `end`). The Promise microtask runs next (`promise`). The `setTimeout` macrotask runs last (`timeout`). **Question 14.** Which Promise combinator resolves as soon as any of the input promises fulfills, or rejects if all reject? A) `Promise.all` B) `Promise.race` C) `Promise.any` D) `Promise.allSettled` Answer: C Explanation: `Promise.any` resolves with the first fulfilled promise and only rejects after all promises have rejected. **Question 15.** In an `async` function, what does the `await` operator do? A) Pauses the entire JavaScript thread until the promise settles. B) Converts any value to a promise and returns the resolved value. C) Returns a new promise that resolves after the original promise. D) Causes the function to return a rejected promise automatically. Answer: B Explanation: `await` pauses execution of the async function, waits for the promise to settle, and returns the fulfilled value (or throws if rejected). It does not block the event loop. **Question 16.** Which of the following statements about generator functions is false? A) They return an iterator object when called. B) The `yield` keyword pauses execution and returns a value to the caller. ## JavaScript Practice Exam C) Generators can be used to implement lazy sequences. D) A generator function must be invoked with `new`. Answer: D Explanation: Generators are regular functions declared with `function*`; they are invoked like normal functions, not with `new`. **Question 17.** Which of the following is a valid way to create a Web Worker? A) `new Worker('worker.js')` B) `new Worker(() => {/* code */})` C) `Worker.create('worker.js')` D) `new WebWorker('worker.js')` Answer: A Explanation: The `Worker` constructor takes the URL of a script that runs in a separate thread. **Question 18.** Which pattern uses a closure to expose a public API while keeping internal state private? A) Singleton B) Factory C) Module D) Observer Answer: C Explanation: The Module Pattern wraps code in an IIFE, returning an object that exposes public members while private variables stay in the closure. **Question 19.** Which of the following is an example of a pure function? A) `function add(a,b){ return a+b; }` ## JavaScript Practice Exam A) A proxy can only intercept property reads, not writes. B) The `get` trap receives the target object, property key, and receiver. C) Proxies are not part of the ES6 specification. D) Proxies cannot be used with arrays. Answer: B Explanation: The `get` handler receives `(target, property, receiver)` and can customize property access. **Question 23.** Which of the following best describes event delegation? A) Attaching a separate listener to each child element. B) Using a single listener on a common ancestor to handle events from its descendants. C) Preventing events from bubbling up the DOM tree. D) Delegating events to a Web Worker. Answer: B Explanation: Event delegation leverages event bubbling by listening on a parent element and determining the target in the handler. **Question 24.** Which method is the most efficient for inserting a large number of nodes into the DOM with a single reflow? A) `element.innerHTML += ...` repeatedly B) `document.createDocumentFragment()` and then appending the fragment C) `element.appendChild()` inside a loop without a fragment D) `element.insertAdjacentHTML('beforeend', ...)` inside a loop Answer: B Explanation: A `DocumentFragment` batches DOM insertions, causing only one reflow when the fragment is appended. ## JavaScript Practice Exam **Question 25.** Which of the following statements about `fetch` is correct? A) `fetch` automatically throws an error for HTTP status codes 4xx/5xx. B) `fetch` returns a Promise that resolves to a `Response` object even for network errors. C) `fetch` can be canceled by calling `abort()` on the returned Promise. D) `fetch` does not support sending credentials by default. Answer: D Explanation: By default, `fetch` does not send cookies or HTTP authentication; you must set `credentials: 'include'` to include them. **Question 26.** Which of the following will correctly catch an error thrown inside an async function? ```js async function foo(){ throw new Error('fail'); } A)
foo().catch(err=> console.log(err)); B)
try{ foo(); } catch(e){ console.log(e); } C)
## JavaScript Practice Exam C) `{value:1, done:false}` then `{value:undefined, done:true}` D) `{value:1, done:false}` then `{value:4, done:false}` Answer: A Explanation: `next()` yields `1`. `return(4)` ends the generator early, setting `value` to the argument and `done:true`. **Question 29.** Which of the following is a characteristic of a memory leak in JavaScript? A) The garbage collector never runs. B) Objects that are no longer reachable from the root are still retained due to lingering references. C) All global variables are automatically cleaned up. D) Closures always cause memory leaks. Answer: B Explanation: A leak occurs when references (e.g., from closures or DOM nodes) keep objects alive even though they are no longer needed. **Question 30.** Which of the following is the correct way to create a custom error class named `ValidationError`? A) ```js class ValidationError extends Error { constructor(message){ super(message); this.name='ValidationError'; } } B)
## JavaScript Practice Exam function ValidationError(message){ this.message=message; } C)
class ValidationError { constructor(message){ this.message=message; } } D)
const ValidationError = new Error('ValidationError'); Answer: A Explanation: Extending Error and calling super(message) correctly sets up the prototype chain and stack trace. Question 31. Which of the following statements about the async keyword is false? A) An async function always returns a promise. B) The await operator can only be used inside async functions. C) An async function can be paused multiple times using await. D) Declaring a function as async makes it execute in a separate thread. Answer: D Explanation: async functions run on the same thread; they just return promises and allow asynchronous waiting. Question 32. Which of the following correctly demonstrates function currying? A)
Explanation: reduce returns a single accumulated value (or whatever you return), not a new array. Question 34. Which of the following is true about the WeakMap keys? A) Keys can be primitive values. B) Keys are held weakly, allowing garbage collection if there are no other references. C) WeakMaps are iterable. D) WeakMap entries are enumerable via for…of. Answer: B Explanation: WeakMap keys must be objects, and the map holds them weakly so that they do not prevent garbage collection. Question 35. Which of the following correctly creates a memoized version of a pure function fib? A)
function memoFib(n){ if(n<2) return n; return memoFib(n-1)+memoFib(n-2); } B)
const memoFib = (()=>{ const cache={}; return n=>{ if(cache[n]!=null) return cache[n]; return cache[n]=fib(n); }; })(); C)
## JavaScript Practice Exam function memoFib(){ const cache={}; return function(n){ if(cache[n]) return cache[n]; return cache[n]=fib(n); }; } D)
const memoFib = new Map(); memoFib.set = (n,val)=>{ memoFib.set(n,val); }; Answer: B Explanation: Option B creates a closure with a cache object and returns a function that checks the cache before computing. Question 36. Which of the following is the correct way to import a default export named utils from a module helpers.js? A) import { utils } from './helpers.js'; B) import utils from './helpers.js'; C) import * as utils from './helpers.js'; D) require('./helpers.js').default; Answer: B Explanation: Default exports are imported without braces. Question 37. Which of the following statements about Object.freeze() is false? A) It makes an object immutable by preventing addition, deletion, or modification of existing properties. B) It also makes nested objects immutable. C) It works only on plain objects, not on arrays. D) It returns the same object that was passed in.
function debounce(fn,d){ let t; return ()=>{ t=setTimeout(fn,d); }; }
D) ```js function debounce(fn,d){ let t; return (...a)=>{ fn(...a); }; } Answer: A Explanation: The closure stores a timeout ID, clears any pending timeout, and sets a new one, achieving debouncing. Question 40. Which of the following is true about the Array.prototype.flat() method? A) It flattens an array recursively to infinite depth by default. B) It mutates the original array. C) It accepts a depth argument to control how deep to flatten. D) It only works on arrays of numbers. Answer: C Explanation: flat(depth) returns a new array flattened to the specified depth; the default depth is 1. Question 41. Which of the following statements about the this value inside an arrow function is correct? A) It is dynamically bound based on how the function is called. B) It inherits the this value from its surrounding lexical scope. C) It is always undefined in strict mode. D) It can be changed with .call() or .apply(). Answer: B
Explanation: Arrow functions do not have their own this; they capture the this from the enclosing context. Question 42. Which of the following is a valid way to create a read‑only property id on an object user? A) Object.defineProperty(user, 'id', { value: 123, writable: false }); B) user.id = 123; Object.freeze(user); C) user.id = 123; Object.seal(user); D) Object.preventExtensions(user); user.id = 123; Answer: A Explanation: defineProperty with writable:false creates a non‑writable (read‑only) property. Question 43. Which of the following correctly describes the difference between Array.prototype.map and Array.prototype.forEach? A) map returns a new array, forEach returns undefined. B) forEach can be chained, map cannot. C) map mutates the original array, forEach does not. D) Both return the same value but differ in performance. Answer: A Explanation: map creates and returns a new array of transformed values; forEach simply iterates and returns undefined. Question 44. Which of the following is true about Symbol.for('key')? A) It creates a new unique Symbol every time it is called. B) It retrieves (or creates) a Symbol stored in the global symbol registry. C) It is not accessible across different realms (e.g., iframes).