Master the art of JavaScript with 30 thought-provoking MCQs divided topic-wise, covering essential patterns like Module, Singleton, Factory, Observer, Pub/Sub, and key best practices for scalable, maintainable code.
Module Pattern and Revealing Module Pattern (6 MCQs)
Which of the following is a characteristic of the Module Pattern in JavaScript? a) It uses closures to encapsulate private variables and methods. b) It relies on prototypical inheritance. c) It is used for asynchronous programming. d) It supports multiple global variables directly.
The Revealing Module Pattern primarily focuses on: a) Creating a chain of responsibility for objects. b) Reducing memory usage in object creation. c) Exposing only necessary properties and methods. d) Generating instances of objects dynamically.
In the Module Pattern, variables declared inside the module are: a) Globally accessible. b) Local to the module and cannot be accessed directly from outside. c) Available in the prototype chain. d) Always public properties.
Which syntax is commonly used to define a Module in JavaScript? a) const module = function() {}; b) function module() { return {}; } c) const module = (function() { return {}; })(); d) module = new Module();
Why is the Revealing Module Pattern considered an enhancement of the Module Pattern? a) It introduces prototypical inheritance. b) It automatically binds methods to their contexts. c) It simplifies the module structure by directly mapping private methods to public ones. d) It enforces stricter variable scoping.
What is a common disadvantage of the Module Pattern? a) Difficulty in debugging private variables. b) Lack of support for closures. c) High memory consumption due to global variables. d) Inability to encapsulate logic.
Singleton, Factory, and Observer Patterns (8 MCQs)
The Singleton Pattern ensures: a) Multiple instances of a class can be created. b) Only one instance of a class exists throughout the application. c) All methods are bound to a single context. d) Object creation is avoided altogether.
Which method is typically used in the Singleton Pattern to retrieve the instance? a) createInstance() b) getInstance() c) new Singleton() d) initializeInstance()
The Factory Pattern is primarily used for: a) Observing changes in object properties. b) Decoupling object creation from its usage. c) Managing event-driven programming. d) Ensuring only one instance of a class exists.
In the Observer Pattern, observers are: a) Independent and unaware of the subject. b) Directly modifying the subject’s state. c) Registered to listen to changes in the subject’s state. d) Restricted from accessing the subject’s data.
Which of the following best describes the Factory Pattern? a) It uses classes to observe changes in data. b) It provides a way to create objects without specifying their concrete types. c) It enforces encapsulation of object creation logic. d) It allows a single instance of a class to manage state.
The Observer Pattern helps implement which type of architecture? a) Layered architecture b) Event-driven architecture c) Service-oriented architecture d) Modular architecture
Which of these is NOT true about the Singleton Pattern? a) It allows multiple instantiations in parallel. b) It is useful in managing shared resources. c) It can be implemented using closures. d) It is often used with configuration objects.
The Observer Pattern typically consists of: a) Publishers and factories. b) Observers and singletons. c) Subjects and observers. d) Modules and factories.
Event-Driven Programming and Pub/Sub Pattern (8 MCQs)
Event-driven programming is primarily based on: a) Recursive function calls. b) User-defined object states. c) Emitting and handling events. d) Direct procedural execution.
The Pub/Sub pattern involves: a) Synchronous communication between objects. b) Publishers broadcasting events to subscribers. c) Subjects observing changes in their state. d) Direct calls from publishers to subscribers.
A key difference between Observer and Pub/Sub patterns is: a) Pub/Sub requires direct references between entities. b) Pub/Sub decouples publishers and subscribers. c) Observer allows one-to-many relationships, while Pub/Sub does not. d) Observer patterns cannot be used for event-driven programming.
Which JavaScript method is commonly used to add event listeners? a) addEventListener() b) onEvent() c) listenEvent() d) registerEvent()
A primary advantage of the Pub/Sub pattern is: a) Strong coupling between components. b) Simplified debugging. c) Decoupling of event emitters and listeners. d) Reduction of runtime errors.
In event-driven programming, what is an “event”? a) A set of CSS rules. b) A specific user action or system notification. c) A predefined function. d) A property of an object.
Which of the following is a Pub/Sub library in JavaScript? a) Lodash b) Axios c) PubNub d) Moment.js
How does the Pub/Sub pattern improve scalability? a) By creating direct dependencies between components. b) By enabling asynchronous communication. c) By synchronizing all events. d) By eliminating the need for callbacks.
JavaScript Code Style and Best Practices (8 MCQs)
Which of the following is a common JavaScript best practice? a) Use var for all variable declarations. b) Avoid using strict mode. c) Use const and let instead of var. d) Declare variables without initialization.
What does ESLint help with? a) Debugging memory leaks. b) Enforcing consistent code styles. c) Optimizing JavaScript runtime. d) Managing event listeners.
Which statement about JavaScript semicolons is true? a) They are always mandatory. b) They are optional but recommended for clarity. c) They must be avoided in ES6. d) They only work in strict mode.
When writing JavaScript code, it’s best to: a) Avoid comments to reduce file size. b) Write comments to explain complex logic. c) Use comments as a substitute for clear code. d) Ignore code indentation for faster typing.
A recommended way to handle errors in JavaScript is: a) Use global variables for error tracking. b) Wrap critical code blocks in try...catch statements. c) Avoid error handling to improve performance. d) Log errors to the console only in production.
Which tool is commonly used for JavaScript code formatting? a) Prettier b) Mocha c) Chai d) Karma
Why should you avoid polluting the global scope in JavaScript? a) It improves readability. b) It prevents performance bottlenecks. c) It reduces potential for variable conflicts. d) It ensures compatibility with ES6.
Which of the following enhances JavaScript code maintainability? a) Minimizing use of functions. b) Modularizing code into reusable components. c) Avoiding external libraries. d) Using only inline scripts.
Answers
Qno
Answer (Option with Text)
1
a) It uses closures to encapsulate private variables and methods.
2
c) Exposing only necessary properties and methods.
3
b) Local to the module and cannot be accessed directly from outside.
4
c) const module = (function() { return {}; })();
5
c) It simplifies the module structure by directly mapping private methods to public ones.
6
a) Difficulty in debugging private variables.
7
b) Only one instance of a class exists throughout the application.
8
b) getInstance()
9
b) Decoupling object creation from its usage.
10
c) Registered to listen to changes in the subject’s state.
11
b) It provides a way to create objects without specifying their concrete types.
12
b) Event-driven architecture
13
a) It allows multiple instantiations in parallel.
14
c) Subjects and observers.
15
c) Emitting and handling events.
16
b) Publishers broadcasting events to subscribers.
17
b) Pub/Sub decouples publishers and subscribers.
18
a) addEventListener()
19
c) Decoupling of event emitters and listeners.
20
b) A specific user action or system notification.
21
c) PubNub
22
b) By enabling asynchronous communication.
23
c) Use const and let instead of var.
24
b) Enforcing consistent code styles.
25
b) They are optional but recommended for clarity.
26
b) Write comments to explain complex logic.
27
b) Wrap critical code blocks in try...catch statements.