JavaScript Modules MCQ

1. What are JavaScript modules?

a) Pre-built JavaScript libraries
b) JavaScript files that export and import functionality
c) Functions within JavaScript
d) HTML elements linked to JavaScript

Answer:

b) JavaScript files that export and import functionality

Explanation:

JavaScript modules are files that can export parts of their code (like variables, functions, or classes) and import such code from other files, making it easier to organize and maintain large codebases.

2. How do you export a function from a JavaScript module?

a) export function myFunction() {…}
b) function myFunction() {…}; export myFunction
c) module.export = myFunction()
d) export default myFunction()

Answer:

a) export function myFunction() {…}

Explanation:

The export keyword is used to export a function from a module, allowing it to be imported and used in other modules.

3. How can you import a specific function from a JavaScript module?

a) import { myFunction } from 'module'
b) include { myFunction } from 'module'
c) require('module').myFunction
d) load { myFunction } from 'module'

Answer:

a) import { myFunction } from 'module'

Explanation:

The import statement with curly braces is used to import a specific function or variable from a module.

4. What is the purpose of 'export default' in JavaScript modules?

a) To export variables as constants
b) To set the default value of a module
c) To export a single value as the default export of a module
d) To export all functions and variables in a module

Answer:

c) To export a single value as the default export of a module

Explanation:

export default is used to specify a single value (function, class, object, etc.) as the default export from a module.

5. How do you import a default export from a module?

a) import myFunction from 'module'
b) import { default as myFunction } from 'module'
c) require('module').default
d) Both a and b

Answer:

a) import myFunction from 'module'

Explanation:

When importing a default export from a module, you can use the import statement without curly braces.

6. What is the result of having multiple 'export default' statements in a single module?

a) All default exports are combined
b) Only the last export default is considered
c) A syntax error is thrown
d) The module exports an object containing all defaults

Answer:

c) A syntax error is thrown

Explanation:

A module can only have one default export. Having multiple export default statements in a module will result in a syntax error.

7. Can named exports coexist with a default export in a module?

a) Yes
b) No
c) Only if they are functions
d) Only if they are variables

Answer:

a) Yes

Explanation:

A module can have both named exports and a default export, allowing for flexible module structure and usage.

8. How do you rename an imported variable from a module?

a) import { originalName as newName } from 'module'
b) import newName from 'module'
c) import { rename: originalName to newName } from 'module'
d) import originalName as newName from 'module'

Answer:

a) import { originalName as newName } from 'module'

Explanation:

The import { originalName as newName } syntax is used to import a variable under a different name.

9. What is the main advantage of using JavaScript modules?

a) Improved performance due to parallel loading
b) Enhanced security features
c) Automatic compatibility with all browsers
d) Better code organization and maintainability

Answer:

d) Better code organization and maintainability

Explanation:

JavaScript modules provide a way to organize code into manageable and maintainable pieces, promoting better code organization and reuse.

10. How can you import everything from a module as an object?

a) import * as myModule from 'module'
b) import myModule.* from 'module'
c) require('module').*
d) import all from 'module'

Answer:

a) import * as myModule from 'module'

Explanation:

The import * as myModule syntax imports all exports from a module as properties of an object named myModule.

11. How are modules loaded in modern JavaScript?

a) Synchronously
b) Asynchronously
c) Only on demand
d) In parallel with other scripts

Answer:

b) Asynchronously

Explanation:

In modern JavaScript, modules are loaded asynchronously, which can improve page load times and overall performance.

12. What is the scope of top-level variables in a JavaScript module?

a) Global scope
b) Local to the module
c) Function scope
d) Block scope

Answer:

b) Local to the module

Explanation:

Top-level variables in a module are local to the module and not added to the global scope, reducing the chance of name conflicts.

13. Can you use conditional statements to control module imports?

a) Yes, using if statements
b) No, imports must be static
c) Yes, using switch statements
d) Only in function scope

Answer:

b) No, imports must be static

Explanation:

Imports in JavaScript modules are static and cannot be conditionally controlled using statements like if or switch.

14. How does tree shaking relate to JavaScript modules?

a) It's a process of optimizing module loading
b) It's a technique for removing unused code from modules
c) It's a method to combine modules
d) It refers to dynamically loading modules

Answer:

b) It's a technique for removing unused code from modules

Explanation:

Tree shaking is a term commonly used in the context of module bundlers and refers to the elimination of unused code from modules to reduce bundle size and improve performance.

15. What file extension is typically used for JavaScript modules?

a) .jsm
b) .jsx
c) .js
d) .module

Answer:

c) .js

Explanation:

JavaScript modules use the standard .js file extension, just like regular JavaScript files.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top