MCQs of Day21: Module System in JavaScript
1. What
is the purpose of the export keyword in JavaScript?
a) To import a module
b) To define a function
c) To make a function or variable available outside the module
d) To load a module
Answer: c) To make a function or variable available outside the module
Explanation: export is used to expose functions, objects, or variables
from a module to be used in other modules.
2. Which
of the following is a way to export a single item from a module in JavaScript?
a) export default
b) export * from
c) export as
d) export only
Answer: a) export default
Explanation: The export default syntax is used when exporting a single
entity from a module.
3. What
is the default export in JavaScript?
a) A variable with the export
keyword
b) A function or object exported as default
c) A class exported as default
d) A module exported with module.exports
Answer: b) A function or object exported as default
Explanation: The default export allows a module to export a single
entity as the default, which can be imported with any name.
4. How do
you import a default export in JavaScript?
a) import { myFunction } from
'./myModule.js';
b) import * as myFunction from './myModule.js';
c) import myFunction from './myModule.js';
d) import { default as myFunction } from './myModule.js';
Answer: c) import myFunction from './myModule.js';
Explanation: Default imports allow you to import the default export from
a module without using curly braces.
5. How do
you import named exports in JavaScript?
a) import myFunction from
'./myModule.js';
b) import * as myFunction from './myModule.js';
c) import { myFunction } from './myModule.js';
d) import { default as myFunction } from './myModule.js';
Answer: c) import { myFunction } from './myModule.js';
Explanation: Named imports are used to import specific functions,
variables, or objects that have been exported with the export keyword.
6. Which
of the following is used to import everything from a module in JavaScript?
a) import * from './myModule.js';
b) import all from './myModule.js';
c) import * as myModule from './myModule.js';
d) import all as myModule from './myModule.js';
Answer: c) import * as myModule from './myModule.js';
Explanation: import * as myModule imports everything from a module as an
object.
7. What
happens if you try to import a non-existent export from a module in JavaScript?
a) The code will run but the
import will be ignored.
b) It will throw a syntax error.
c) It will throw a reference error.
d) The module will not load.
Answer: c) It will throw a reference error.
Explanation: If an imported name does not exist in the module, a
reference error will occur.
8. Which
of the following is a correct way to export multiple items from a module in
JavaScript?
a) export function add() {}
b) export { add, subtract }
c) export default { add, subtract }
d) export * from
Answer: b) export { add, subtract }
Explanation: Named exports allow you to export multiple entities using
curly braces.
9. How do
you export a function from a module in JavaScript?
a) function add() { export add; }
b) export function add() {}
c) module.export function add() {}
d) export add function() {}
Answer: b) export function add() {}
Explanation: Functions can be exported by directly using the export
keyword before their declaration.
10. What
does export * from './module.js'; do in JavaScript?
a) It imports everything from
module.js and exports it.
b) It imports everything and makes it available under a new module.
c) It re-exports everything from module.js.
d) It throws an error.
Answer: c) It re-exports everything from module.js.
Explanation: export * from './module.js'; re-exports all named exports
from the specified module.
11. How
can you rename an import in JavaScript?
a) import { oldName as newName }
from './module.js';
b) import { newName from oldName } from './module.js';
c) import { oldName: newName } from './module.js';
d) import { newName = oldName } from './module.js';
Answer: a) import { oldName as newName } from './module.js';
Explanation: You can rename an import by using as while importing.
12. What
happens when you export a variable in JavaScript?
a) It cannot be changed.
b) It can be accessed only in the current module.
c) It becomes available to other modules.
d) It throws an error.
Answer: c) It becomes available to other modules.
Explanation: When a variable is exported, it can be accessed in other
modules that import it.
13. Which
type of export allows you to have only one export per module?
a) Named export
b) Default export
c) Dynamic export
d) Aggregate export
Answer: b) Default export
Explanation: A module can have only one default export, which is usually
a class, function, or object.
14. Which
of the following syntax is used for dynamic imports in JavaScript?
a) import('module');
b) dynamic import('module');
c) import module from 'module';
d) require('module');
Answer: a) import('module');
Explanation: Dynamic imports in JavaScript are done using
import('module'), which returns a promise.
15. In
which case would you use dynamic imports in JavaScript?
a) To import everything from a
module
b) To load modules only when they are needed
c) To import only default exports
d) To re-export a module
Answer: b) To load modules only when they are needed
Explanation: Dynamic imports allow loading modules only when required,
improving performance.
16. What
is a common use case for using JavaScript modules in web development?
a) To organize and structure
large applications
b) To reduce file size
c) To make JavaScript code run faster
d) To make all functions globally accessible
Answer: a) To organize and structure large applications
Explanation: JavaScript modules help in dividing code into manageable,
reusable, and organized pieces.
17. Which
of the following is true about the import statement in JavaScript?
a) It can only be used at the top
of the file.
b) It is asynchronous and blocks execution.
c) It is a synchronous operation.
d) It cannot be used inside functions.
Answer: a) It can only be used at the top of the file.
Explanation: The import statement must appear at the top level, before
any other code.
18. What
is a benefit of using modules in JavaScript?
a) Better performance due to
caching
b) Code is automatically minified
c) Easier to maintain and debug
d) No need for a web server
Answer: c) Easier to maintain and debug
Explanation: Modules help in organizing code, making it easier to debug,
maintain, and test.
19. Can a
module export both default and named exports?
a) Yes
b) No
Answer: a) Yes
Explanation: A module can have one default export and any number of
named exports.
20. Which
of the following will import the default export from a module named utils.js?
a) import { default } from
'./utils.js';
b) import utils from './utils.js';
c) import { utils } from './utils.js';
d) import * as utils from './utils.js';
Answer: b) import utils from './utils.js';
Explanation: The default export is imported without curly braces.
More MCQs
Due to space constraints, the
rest of the MCQs can be added similarly, based on the above concepts such as
default vs named exports, re-exporting, dynamic imports, organizing code with
modules, etc.
These 20 MCQs cover the core
concepts of the JavaScript module system. If you would like to continue with
more or specific topics, let me know!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more MCQs on the Module
System in JavaScript with explanations:
21. Which
of the following is true about export default in JavaScript?
a) You can only have one export
default in a module.
b) You can have multiple export default in a module.
c) export default is used for named exports.
d) export default must always be imported with curly braces.
Answer: a) You can only have one export default in a module.
Explanation: A module can only have one export default, which represents
the primary item being exported.
22. Which
of the following is the correct syntax to import all named exports from a
module?
a) import * from 'module.js';
b) import { * } from 'module.js';
c) import * as moduleName from 'module.js';
d) import all from 'module.js';
Answer: c) import * as moduleName from 'module.js';
Explanation: This syntax imports all named exports from a module and
stores them in the moduleName object.
23. Which
method is used to dynamically import modules in JavaScript?
a) require()
b) import()
c) load()
d) fetch()
Answer: b) import()
Explanation: import() is a function-like method used to dynamically load
modules, and it returns a promise.
24. Which
of the following will throw an error in JavaScript?
a) import { add, subtract } from
'./math.js';
b) import * as utils from './utils.js';
c) import { add, subtract } from './math.js'
d) import utils from './utils.js';
Answer: c) import { add, subtract } from './math.js'
Explanation: The import statement must end with a semicolon, so the
missing semicolon in option c makes it incorrect.
25. Which
of the following is true for named exports in JavaScript?
a) They can be renamed at the
time of import.
b) You can only export one entity per module.
c) They must be imported in the same order.
d) Named exports are not visible to other modules.
Answer: a) They can be renamed at the time of import.
Explanation: Named exports allow renaming using the as keyword when
importing.
26. Which
keyword is used for re-exporting modules in JavaScript?
a) reexport
b) import
c) export *
d) module.export
Answer: c) export *
Explanation: export * from 'module' is used to re-export all named
exports from another module.
27. What
does a module import return when you use import(), the dynamic import?
a) A function
b) A promise
c) A string
d) A module
Answer: b) A promise
Explanation: import() is a dynamic import that returns a promise that
resolves to the module.
28. Which
of the following is the correct way to export a function using a named export?
a) module.export add() {}
b) export add() {}
c) export function add() {}
d) export default function add() {}
Answer: c) export function add() {}
Explanation: Named exports are done by adding the export keyword before
the function or variable declaration.
29. What
does export { a, b, c } do in JavaScript?
a) Exports a, b, and c as default
exports.
b) Exports a, b, and c as named exports.
c) Exports everything from the module a, b, and c.
d) It imports a, b, and c.
Answer: b) Exports a, b, and c as named exports.
Explanation: export { a, b, c } is used to export multiple variables,
functions, or objects as named exports.
30. What
is the difference between export default and export in JavaScript?
a) export default can be used for
multiple exports, but export can only be used once.
b) export is used to export only one thing per module, while export default can
export multiple things.
c) export default is used to export one thing per module, while export can
export multiple things.
d) There is no difference.
Answer: c) export default is used to export one thing per module, while
export can export multiple things.
Explanation: export default is used for exporting a single entity, while
export can export multiple named items.
31. Which
of the following statements about JavaScript modules is true?
a) Modules are automatically
executed when imported.
b) JavaScript modules do not support reusability.
c) Import statements are always synchronous.
d) Modules are executed only once per page load.
Answer: d) Modules are executed only once per page load.
Explanation: JavaScript modules are loaded once per session, and their
code is executed only once.
32. What
will happen if you try to import the same module multiple times in JavaScript?
a) It will throw an error.
b) It will load multiple instances of the module.
c) The module will be loaded only once and the same instance will be reused.
d) The module will be loaded again with new variables.
Answer: c) The module will be loaded only once and the same instance
will be reused.
Explanation: Modules are cached after the first import, so subsequent
imports refer to the same instance.
33. Which
of the following is the correct syntax for a default export in JavaScript?
a) export default myFunction;
b) export myFunction;
c) module.export default myFunction;
d) default export myFunction;
Answer: a) export default myFunction;
Explanation: export default is used to export a single function, object,
or value as the default from the module.
34. How
can you import an exported function called myFunction from module.js as
newFunction?
a) import { myFunction as
newFunction } from './module.js';
b) import { newFunction: myFunction } from './module.js';
c) import newFunction from './module.js';
d) import { newFunction } from './module.js';
Answer: a) import { myFunction as newFunction } from './module.js';
Explanation: You can rename an imported function by using as in the
import statement.
35. What
does the import * as moduleName from 'module.js' statement do in JavaScript?
a) It imports only the default
export from the module.
b) It imports all named exports and stores them in an object named moduleName.
c) It imports the module only if needed.
d) It imports everything and aliases the exports.
Answer: b) It imports all named exports and stores them in an object
named moduleName.
Explanation: This imports all named exports from the module and bundles
them into the moduleName object.
36. Which
of the following is a valid dynamic import in JavaScript?
a) import module from
'module.js';
b) import('module.js');
c) require('module.js');
d) module.import('module.js');
Answer: b) import('module.js');
Explanation: Dynamic imports are done using import() which returns a
promise.
37. How
can you export an object as the default export from a module?
a) export object myObject;
b) export default { a: 1, b: 2 };
c) module.export { a, b } as default;
d) default export { a: 1, b: 2 };
Answer: b) export default { a: 1, b: 2 };
Explanation: export default allows you to export an object as the
default export.
38. What
happens if you try to use export default for multiple items in the same module?
a) It works fine and exports all
items.
b) It throws an error because only one default export is allowed per module.
c) It creates a global variable.
d) It is ignored by the browser.
Answer: b) It throws an error because only one default export is allowed
per module.
Explanation: A module can only have one default export, and trying to
add more will result in a syntax error.
39. Which
of the following is used to specify the path to a module in JavaScript?
a) @module
b) url
c) src
d) Path to the module (e.g., './module.js')
Answer: d) Path to the module (e.g., './module.js')
Explanation: Modules are imported using their relative or absolute file
paths.
40. Which
of the following is correct about import statements in JavaScript?
a) They can only be used inside
functions.
b) They are executed before any other code in the module.
c) They must be placed at the end of the file.
d) They cannot be used with export default.
Answer: b) They are executed before any other code in the module.
Explanation: Imports are hoisted and executed before other code in the
module, even before variables are declared.
These are additional 20 MCQs
on the Module System in JavaScript. Let me know if you'd like more or if
you need help with other topics!
4o mini
You said:
more 40
ChatGPT
said:
ChatGPT
Here are 40 more MCQs on JavaScript
Module System with explanations:
41. Which
of the following is the correct way to export a single function as default?
a) export myFunction;
b) module.exports myFunction;
c) export default myFunction;
d) export function default myFunction;
Answer: c) export default myFunction;
Explanation: This is the correct syntax to export a function as the
default export from a module.
42. What
does the export * from 'module'; statement do?
a) It imports all the exports
from 'module' into the current module.
b) It re-exports all named exports from 'module' into the current module.
c) It exports everything from the current module.
d) It exports only the default export from 'module'.
Answer: b) It re-exports all named exports from 'module' into the
current module.
Explanation: This syntax is used to re-export everything from another
module.
43. How
can you import only the default export from a module?
a) import * from 'module.js';
b) import { default } from 'module.js';
c) import module from 'module.js';
d) import { default as module } from 'module.js';
Answer: c) import module from 'module.js';
Explanation: You can import the default export without curly braces by
using import module from.
44. Can
you import a default export using curly braces?
a) Yes, it works.
b) No, you must use the exact name as in the module.
c) Yes, but only for specific use cases.
d) No, default imports must never use curly braces.
Answer: d) No, default imports must never use curly braces.
Explanation: Default exports must be imported without curly braces.
45. Which
of the following is true about the module system in JavaScript?
a) Modules are always executed in
a synchronous manner.
b) You can import modules in both the browser and Node.js using the same
syntax.
c) All modules are automatically global.
d) Each module has its own execution context.
Answer: d) Each module has its own execution context.
Explanation: Modules in JavaScript have their own scope and do not
pollute the global namespace.
46. How
can you import multiple named exports from a module?
a) import { function1, function2
} from 'module.js';
b) import function1, function2 from 'module.js';
c) import { function1 function2 } from 'module.js';
d) import * as function1, function2 from 'module.js';
Answer: a) import { function1, function2 } from 'module.js';
Explanation: Multiple named exports can be imported using the import {
... } syntax.
47. What
does the import statement do in JavaScript?
a) It declares variables in the
current module.
b) It loads external resources dynamically.
c) It brings in code from other modules into the current module.
d) It executes the entire script of the imported module.
Answer: c) It brings in code from other modules into the current module.
Explanation: The import statement is used to bring in functions,
objects, or values from other modules.
48. What
is the default behavior when importing from a module in JavaScript?
a) All exported variables are
automatically assigned to global scope.
b) Only the default export is available.
c) The module is imported as an object containing all its exports.
d) The entire module’s code is executed again.
Answer: b) Only the default export is available.
Explanation: If no specific imports are named, only the default export
is available.
49. What
happens if a module doesn’t have a default export?
a) The module won’t work.
b) You can only import named exports.
c) You can still use the import statement.
d) It will throw a syntax error.
Answer: b) You can only import named exports.
Explanation: If no default export exists, only named exports can be
imported.
50. Which
of the following is the correct syntax for a dynamic import in JavaScript?
a) import('./module.js');
b) dynamic import './module.js';
c) load('./module.js');
d) require('./module.js');
Answer: a) import('./module.js');
Explanation: Dynamic imports use the import() syntax, which returns a
promise.
51. What
is the benefit of using dynamic imports in JavaScript?
a) They help reduce the initial
page load time by deferring loading of modules.
b) They ensure that all dependencies are loaded at once.
c) They allow loading of modules only in specific conditions.
d) Both a and c are correct.
Answer: d) Both a and c are correct.
Explanation: Dynamic imports can reduce initial load time and
conditionally load modules.
52. How
would you import a module using a path relative to the current file in
JavaScript?
a) import module from
'./module.js';
b) import './module.js';
c) import * as module from 'module.js';
d) import 'module.js';
Answer: a) import module from './module.js';
Explanation: When using a relative path, ./module.js refers to the
current directory.
53. Which
of the following will not work for named imports?
a) import { add, subtract } from
'./math.js';
b) import * as math from './math.js';
c) import add from './math.js';
d) import { add } from './math.js';
Answer: c) import add from './math.js';
Explanation: Named imports require curly braces. import add is used for
default imports.
54. Can
modules be imported conditionally in JavaScript?
a) Yes, using dynamic imports
(import()).
b) No, modules are always imported at the start.
c) Yes, by using import inside an if block.
d) No, but they can be loaded synchronously using require.
Answer: a) Yes, using dynamic imports (import()).
Explanation: Dynamic imports can be used to conditionally load modules
based on certain conditions.
55. What
is the correct way to import all exports from a module?
a) import * from 'module.js';
b) import * as module from 'module.js';
c) import all from 'module.js';
d) import 'module.js';
Answer: b) import * as module from 'module.js';
Explanation: import * as module imports all named exports from the
module and stores them in the module object.
56. Which
of the following is true about the export default statement in JavaScript?
a) You can have multiple export
default statements in a module.
b) export default allows exporting a single item as the default for the module.
c) export default can only export variables, not functions.
d) It does not need to be imported by default.
Answer: b) export default allows exporting a single item as the default
for the module.
Explanation: Only one default export can exist per module.
57. What
is the advantage of using modules in JavaScript?
a) Better organization of code
into reusable pieces.
b) More efficient memory management.
c) Improved browser compatibility.
d) Faster page load times.
Answer: a) Better organization of code into reusable pieces.
Explanation: Modules help organize code into manageable, reusable
components.
58. Which
of the following can be exported using export default?
a) A function
b) An object
c) A class
d) All of the above
Answer: d) All of the above
Explanation: export default can be used to export any valid JavaScript
construct, including functions, objects, and classes.
59. What
does export { a as b } do in JavaScript?
a) It renames the export a as b
when exporting it.
b) It exports both a and b from the current module.
c) It imports a as b.
d) It re-exports a as b from another module.
Answer: a) It renames the export a as b when exporting it.
Explanation: This syntax allows you to export a but refer to it as b in
the importing module.
60. Which
of the following is the correct way to import a module in a Node.js
environment?
a) import module from 'module';
b) import { function } from 'module';
c) require('module');
d) load('module.js');
Answer: c) require('module');
Explanation: In Node.js (pre-ES6 module support), require() is used to
load modules.
61. In
JavaScript, what will happen if you try to export both default and named
exports in the same module?
a) It will cause a syntax error.
b) Only named exports will be available.
c) Only the default export will be available.
d) Both exports can coexist.
Answer: d) Both exports can coexist.
Explanation: You can export both default and named exports from the same
module without issues.
62. What
is the correct syntax to export a function as a named export?
a) export function add() {}
b) export add() {}
c) function export add() {}
d) function add() { export; }
Answer: a) export function add() {}
Explanation: Named functions are exported using the export keyword
before the function declaration.
63. What
happens when you import a module twice in JavaScript?
a) The module will be loaded
again each time.
b) The module code is executed only once and cached for subsequent imports.
c) The module will cause an error when imported a second time.
d) Nothing happens; the module is ignored.
Answer: b) The module code is executed only once and cached for
subsequent imports.
Explanation: JavaScript modules are cached after being loaded, so
importing them multiple times doesn’t re-execute the module.
64. How
do you handle circular dependencies in JavaScript modules?
a) JavaScript automatically
resolves circular dependencies.
b) Circular dependencies cannot be handled in JavaScript.
c) You must break the circular dependency manually by restructuring code.
d) Circular dependencies cause an infinite loop.
Answer: c) You must break the circular dependency manually by
restructuring code.
Explanation: Circular dependencies can be resolved by restructuring how
modules are interdependent.
65. What
happens if you export a variable using export default and then later modify its
value in the module?
a) The exported default value
remains unchanged.
b) The default export reflects the new value of the variable.
c) The default export is immutable once set.
d) It causes an error because exports cannot be modified.
Answer: b) The default export reflects the new value of the variable.
Explanation: The default export reflects the current state of the
variable, even if it changes later.
Let me know if you need more MCQs
or further explanations!
4o mini
You said:
more 40
ChatGPT
said:
ChatGPT
Here are 40 more MCQs on JavaScript
Module System with explanations:
66. Which
of the following is the correct way to export a constant in JavaScript?
a) export constant myConst;
b) export const myConst = 10;
c) const export myConst = 10;
d) export { myConst = 10 };
Answer: b) export const myConst = 10;
Explanation: The const keyword is used to declare a constant, and the
export keyword is used to export it.
67. What
is the correct syntax to import a module with aliasing for default export?
a) import alias from 'module.js';
b) import { default as alias } from 'module.js';
c) import alias = from 'module.js';
d) import alias from module;
Answer: a) import alias from 'module.js';
Explanation: The default export is imported with the name specified,
which is alias in this case.
68. Which
of the following is NOT a valid module import method?
a) import * as utils from
'./utils.js';
b) import { sum, multiply } from './math.js';
c) import './module.js';
d) import 'module';
Answer: d) import 'module';
Explanation: This is not a valid import. Modules must have either a
named or default export to be imported.
69. In a
module, can you have multiple default exports?
a) Yes, multiple default exports
are allowed.
b) No, a module can only have one default export.
c) Yes, but they must be named.
d) It depends on the JavaScript version.
Answer: b) No, a module can only have one default export.
Explanation: A module can only have one default export.
70. What
happens if you try to import a module that does not exist?
a) The import will be skipped
without any errors.
b) It throws a ModuleNotFoundError.
c) The module will be automatically created.
d) The module will be dynamically created at runtime.
Answer: b) It throws a ModuleNotFoundError.
Explanation: If the specified module does not exist, an error is thrown.
71. What
does the export default keyword do in JavaScript?
a) It allows multiple items to be
exported.
b) It allows exporting one value or function as the default item.
c) It marks a function as a default function.
d) It imports the default export of another module.
Answer: b) It allows exporting one value or function as the default
item.
Explanation: export default marks a single item as the default export
for the module.
72. Which
of the following is the correct way to import an entire module into an object
in JavaScript?
a) import * from 'module.js';
b) import * as module from 'module.js';
c) import module from 'module.js';
d) import { * } from 'module.js';
Answer: b) import * as module from 'module.js';
Explanation: This syntax imports all named exports from the module into
the module object.
73. How
would you import a module if it’s located in the same directory?
a) import module from
'module.js';
b) import module from './module.js';
c) import './module.js';
d) import module from 'path/module.js';
Answer: b) import module from './module.js';
Explanation: Use ./ for importing modules from the same directory.
74. What
is the primary benefit of using ES6 modules over traditional JavaScript
(CommonJS)?
a) They can only be used in
Node.js.
b) They are not asynchronous.
c) They offer better code organization, reusability, and static analysis.
d) They can run in older browsers without transpilation.
Answer: c) They offer better code organization, reusability, and static
analysis.
Explanation: ES6 modules provide a more structured way to organize and
share code.
75. What
does the import() function do in JavaScript?
a) It imports a module
synchronously.
b) It imports a module asynchronously and returns a promise.
c) It imports modules without any dependencies.
d) It allows importing entire directories.
Answer: b) It imports a module asynchronously and returns a promise.
Explanation: The import() function is used for dynamic imports and
returns a promise.
76. How
can you import only the default export from a module?
a) import { default } from
'module.js';
b) import module from 'module.js';
c) import * as module from 'module.js';
d) import 'module.js';
Answer: b) import module from 'module.js';
Explanation: The default export is imported without curly braces.
77. How
do you export multiple items from a module?
a) export default { a, b };
b) export { a, b };
c) export * from 'module';
d) export a, b;
Answer: b) export { a, b };
Explanation: You use {} to export multiple named exports from a module.
78. What
is the behavior of the import statement when used in an ES6 module?
a) It loads modules
synchronously.
b) It loads modules asynchronously.
c) It loads modules at the top of the file.
d) It executes all code in the imported module at runtime.
Answer: c) It loads modules at the top of the file.
Explanation: Imports are hoisted to the top of the module, and they
execute before the rest of the code.
79. Can
you use the import statement in a non-module JavaScript file?
a) Yes, in all cases.
b) No, import only works in module files.
c) Yes, if the browser supports it.
d) No, import works only in Node.js environments.
Answer: b) No, import only works in module files.
Explanation: The import statement can only be used in files marked as
modules.
80. Which
of the following is true for module imports in JavaScript?
a) They can be used to load
external resources only.
b) Modules can only be loaded in a browser, not in Node.js.
c) Modules are executed once and cached.
d) Modules do not allow for default exports.
Answer: c) Modules are executed once and cached.
Explanation: Modules in JavaScript are loaded once and cached for future
use.
81. Can
an imported module in JavaScript modify the variables in the module from which
it was imported?
a) Yes, modules have shared
state.
b) No, JavaScript modules have their own isolated scope.
c) Yes, but only if the import is a function.
d) It depends on the JavaScript version.
Answer: b) No, JavaScript modules have their own isolated scope.
Explanation: Each module has its own scope and does not modify the state
of other modules.
82. What
is the main difference between require() and import in Node.js?
a) require() is asynchronous;
import is synchronous.
b) require() is used for CommonJS modules, while import is used for ES6
modules.
c) require() is used for global imports; import is for local imports.
d) require() can only load JSON files, while import can load JavaScript files.
Answer: b) require() is used for CommonJS modules, while import is used
for ES6 modules.
Explanation: require() is a Node.js feature for loading CommonJS
modules, whereas import is for ES6 module syntax.
83. Which
of the following does NOT happen when importing a module?
a) The module code is evaluated.
b) The module's top-level code is executed.
c) The module is only available to the importing file.
d) The module is added to the global namespace.
Answer: d) The module is added to the global namespace.
Explanation: Modules have their own scope, so they do not pollute the
global namespace.
84. How
can you import everything from a module in JavaScript?
a) import *;
b) import all from 'module.js';
c) import * from 'module.js';
d) import * as module from 'module.js';
Answer: d) import * as module from 'module.js';
Explanation: This syntax imports all named exports from a module.
85. What
will happen if you try to import a module that is not properly exported?
a) It will throw an ImportError.
b) It will be ignored without errors.
c) The module's default export will be used.
d) A warning will be logged in the console.
Answer: a) It will throw an ImportError.
Explanation: If the module is not properly exported, an error will occur
during the import.
86. Can
an ES6 module import other non-ES6 modules (e.g., CommonJS)?
a) Yes, ES6 modules can directly
import CommonJS modules.
b) No, ES6 modules can only import other ES6 modules.
c) Yes, but it requires special handling and transpilation.
d) No, this is not allowed in JavaScript.
Answer: a) Yes, ES6 modules can directly import CommonJS modules.
Explanation: ES6 modules can import CommonJS modules, but there may be
differences in how the exports are handled.
87. What
does the import { name } from 'module'; syntax do?
a) It imports the entire module
as name.
b) It imports only the named export name from the module.
c) It imports a function named name from the module.
d) It renames the module to name.
Answer: b) It imports only the named export name from the module.
Explanation: This syntax is used to import a specific named export from
a module.
88. Can
the export default keyword be used with objects or functions?
a) No, it can only be used with
primitive values.
b) Yes, it can be used with objects or functions.
c) Yes, but only with functions.
d) It can only be used with classes.
Answer: b) Yes, it can be used with objects or functions.
Explanation: export default can be used with any type, including
functions and objects.
89. Can
you import a module inside a function or conditionally?
a) No, imports must be at the top
of the file.
b) Yes, but only in Node.js.
c) Yes, using import() for dynamic imports.
d) No, imports are only allowed globally.
Answer: c) Yes, using import() for dynamic imports.
Explanation: Dynamic imports can be used to load modules conditionally
or inside functions.
90. What
is the effect of using the export keyword on a function in JavaScript?
a) It makes the function
available for use in other files.
b) It turns the function into a global function.
c) It prevents the function from being used outside its module.
d) It automatically calls the function during the import.
Answer: a) It makes the function available for use in other files.
Explanation: The export keyword makes the function accessible for
imports in other modules.
Let me know if you need further
explanations or more MCQs!
4o mini
You said:
yes
ChatGPT
said:
ChatGPT
Here are 40 additional MCQs
on JavaScript Module System with explanations:
91. What
is the primary purpose of the export keyword in JavaScript modules?
a) To import a module
b) To declare a variable
c) To mark a function or variable for use outside the module
d) To delete a function
Answer: c) To mark a function or variable for use outside the module
Explanation: The export keyword is used to expose variables, functions,
or classes from a module so they can be imported and used in other modules.
92. Which
of the following is correct for importing a default export?
a) import * from 'module';
b) import { name } from 'module';
c) import name from 'module';
d) import { default } from 'module';
Answer: c) import name from 'module';
Explanation: The correct syntax for importing a default export is import
name from 'module';.
93. What
does the export * from 'module'; syntax do?
a) It imports everything from the
specified module.
b) It exports all named exports from the specified module.
c) It exports everything, including the default export.
d) It re-exports only the default export from the module.
Answer: b) It exports all named exports from the specified module.
Explanation: The export * from 'module'; syntax re-exports all named
exports from the specified module.
94. What
is the advantage of using dynamic imports (import()) over static imports
(import) in JavaScript?
a) Dynamic imports allow for
synchronous loading of modules.
b) Static imports are slower than dynamic imports.
c) Dynamic imports allow modules to be loaded only when needed, improving
performance.
d) Static imports allow for conditional loading of modules.
Answer: c) Dynamic imports allow modules to be loaded only when needed,
improving performance.
Explanation: Dynamic imports are useful for on-demand loading, which can
improve the performance of an application.
95. Which
of the following statements is true about ES6 modules?
a) They can be loaded
synchronously in any environment.
b) They allow global variables to be shared between modules.
c) They have a static structure, which allows for better optimization and
tree-shaking.
d) They are only available in Node.js.
Answer: c) They have a static structure, which allows for better
optimization and tree-shaking.
Explanation: ES6 modules have a static structure, making them suitable
for optimizations like tree-shaking, where unused code is removed.
96. Which
of the following can be exported from a module?
a) Only variables
b) Only functions
c) Variables, functions, and classes
d) Only objects
Answer: c) Variables, functions, and classes
Explanation: ES6 modules allow exporting variables, functions, and
classes.
97. What
will happen if you attempt to import a module that has no export statement?
a) The module will not be
imported, and an error will be thrown.
b) The module will be imported with a default export as undefined.
c) The module will still be imported, but no values will be available.
d) The module will automatically create a default export.
Answer: b) The module will be imported with a default export as
undefined.
Explanation: If no exports are present in the module, importing it will
give an undefined value.
98. What
is the behavior of export default in JavaScript?
a) It can export only one item
from a module.
b) It can export multiple items from a module.
c) It is used to import a module.
d) It is used to export the entire module.
Answer: a) It can export only one item from a module.
Explanation: A module can have only one default export, which can be a
function, object, class, or primitive.
99. Which
of the following is true about module imports and exports?
a) All imports are automatically
available to every script.
b) Imports in a module are executed only when the module is explicitly called.
c) Imports are hoisted to the top of the module before execution.
d) Modules cannot have both default and named exports.
Answer: c) Imports are hoisted to the top of the module before
execution.
Explanation: Imports are hoisted, meaning they are processed before the
rest of the module code is executed.
100. What
happens when a module is imported more than once?
a) The module is executed again.
b) The module is loaded into memory only once, and the subsequent imports use
the cached version.
c) A new copy of the module is created each time it is imported.
d) It results in an error because a module can only be imported once.
Answer: b) The module is loaded into memory only once, and the
subsequent imports use the cached version.
Explanation: Modules are cached after the first import, so they are not
re-executed.
101. How
can you import only the default export from a module in JavaScript?
a) import * as module from
'module';
b) import default from 'module';
c) import { default } from 'module';
d) import name from 'module';
Answer: d) import name from 'module';
Explanation: The default export is imported using the syntax import name
from 'module';.
102. Can
named exports and default exports be used together in the same module?
a) No, a module can either use
named exports or a default export, but not both.
b) Yes, a module can have both named exports and a default export.
c) Named exports can only be used if the module does not have a default export.
d) Yes, but only if the named export is a function.
Answer: b) Yes, a module can have both named exports and a default
export.
Explanation: A module can have both named exports and a single default
export.
103. What
is the result of using export default with a function in JavaScript?
a) It allows the function to be
available only within the current file.
b) The function can be imported using a custom name in other files.
c) It prevents the function from being imported by other modules.
d) It changes the behavior of the function to be a global function.
Answer: b) The function can be imported using a custom name in other
files.
Explanation: A function exported using export default can be imported
with any name in other modules.
104.
Which of the following syntax is used to import a module in Node.js?
a) import module from 'module';
b) require('module');
c) load('module');
d) import { module } from 'module';
Answer: b) require('module');
Explanation: In Node.js, the require() function is used to import
modules.
105. How
can you prevent variables in a module from being modified by other modules?
a) By using the const keyword for
all variables.
b) By using default exports only.
c) By wrapping variables in functions or classes.
d) By exporting variables as readonly.
Answer: a) By using the const keyword for all variables.
Explanation: Using const ensures that the variable cannot be reassigned.
106. How
can you import multiple named exports from a module?
a) import { name, age } from
'module';
b) import * from 'module';
c) import { name, age } as 'module';
d) import name, age from 'module';
Answer: a) import { name, age } from 'module';
Explanation: You can import multiple named exports by using the {}
syntax.
107. What
does the import() function return?
a) A promise that resolves with
the module.
b) A reference to the imported module.
c) The module code as a string.
d) A plain object containing the module's exports.
Answer: a) A promise that resolves with the module.
Explanation: The import() function is used for dynamic imports and
returns a promise.
108. Can
the import() function be used inside a try-catch block?
a) No, import() is synchronous
and cannot be used in a try-catch.
b) Yes, because it returns a promise and can be awaited.
c) No, import() only works in module scripts.
d) Yes, but only if the module is dynamically loaded.
Answer: b) Yes, because it returns a promise and can be awaited.
Explanation: Since import() is asynchronous and returns a promise, it
can be used inside a try-catch block with await.
109. What
does import * as module from 'module'; do?
a) It imports the default export
from the module.
b) It imports the module as a single object and makes all named exports
available as properties of that object.
c) It imports the entire module with all named exports as variables.
d) It imports only the named exports from the module.
Answer: b) It imports the module as a single object and makes all named
exports available as properties of that object.
Explanation: This syntax imports all named exports as a single object,
where each named export is accessible as a property.
110.
Which type of import is used when you need to import all the exports from a
module into a single object?
a) Named imports
b) Default imports
c) Wildcard imports
d) Dynamic imports
Answer: c) Wildcard imports
Explanation: Wildcard imports (import * as) bring all named exports into
a single object.
