MCQs on Modules and Modular Code Structure | JavaScript Advanced

This set of 30 multiple-choice questions (MCQs) will test your understanding of key concepts in module management and bundling, including importing/exporting modules, default vs. named exports, tree shaking, and working with bundlers like Webpack and Rollup.


Importing and Exporting Modules

  1. Which of the following keywords is used to import a module in JavaScript?
    a) include
    b) import
    c) require
    d) use
  2. What does the export default syntax allow in JavaScript?
    a) It allows exporting multiple values.
    b) It allows exporting a single value from a module.
    c) It allows importing modules.
    d) It prevents exporting any value.
  3. Which of the following is correct for importing a default export from a module?
    a) import { x } from 'module';
    b) import x from 'module';
    c) import * as x from 'module';
    d) import 'module' as x;
  4. How can you import specific named exports from a module?
    a) import * as { x } from 'module';
    b) import { x } from 'module';
    c) import { x, y } from 'module';
    d) import { x, y, z } from 'module';
  5. What is the purpose of the export keyword in JavaScript?
    a) It is used to import modules.
    b) It makes variables or functions available for use in other files.
    c) It initializes modules.
    d) It prevents variable leakage across modules.
  6. Which of the following would be the correct syntax for exporting a function in JavaScript?
    a) export function myFunction() {}
    b) function export myFunction() {}
    c) export myFunction() {}
    d) myFunction export() {}
  7. Which of the following is NOT a correct way to export a function?
    a) export default function() {}
    b) export function myFunc() {}
    c) module.exports = myFunc;
    d) export { myFunc };
  8. Which statement is true regarding named exports?
    a) A module can have only one default export.
    b) Named exports are always anonymous.
    c) Named exports can be renamed during import.
    d) Named exports cannot be used in destructuring.
  9. How would you import all exports from a module under a specific namespace?
    a) import * as x from 'module';
    b) import { * } from 'module';
    c) import x as * from 'module';
    d) import * all from 'module';
  10. What happens if you try to import a module without exporting anything from it?
    a) The module is not available for use.
    b) A syntax error occurs.
    c) The module is imported with null values.
    d) The import statement is ignored.

Default Exports and Named Exports

  1. Which of the following is a key difference between default export and named export?
    a) Default exports can be renamed upon import; named exports cannot.
    b) Named exports can only export functions, not variables.
    c) Default export is only for functions, while named export is for variables.
    d) Default exports require the export keyword.
  2. Which of the following is a valid way to import both a default export and named exports from a module?
    a) import defaultValue, { x, y } from 'module';
    b) import { defaultValue, x, y } from 'module';
    c) import { x, y } from 'module';
    d) import * from 'module';
  3. What happens if a module has both a default export and named exports?
    a) You can only import one type of export.
    b) Default export cannot be imported.
    c) Both can be imported separately or together.
    d) The module becomes invalid.
  4. Which is the correct way to export both a default and named export from the same module?
    a) export default foo; export { bar };
    b) export foo; export default { bar };
    c) default export foo, bar;
    d) export { foo }, export default bar;
  5. Can a module have more than one default export?
    a) Yes, if the exports are of different types.
    b) No, only one default export is allowed per module.
    c) Yes, but only one can be imported.
    d) No, but you can have multiple named exports.
  6. Which of the following is the correct syntax to export multiple functions from a module?
    a) export function x() {}; export function y() {};
    b) module.exports = { x, y };
    c) export default { x, y };
    d) export { x, y };
  7. When importing a module that exports an object, how can you destructure it?
    a) import { x, y } from 'module';
    b) import * as { x, y } from 'module';
    c) import { x, y } from 'module' as obj;
    d) import { x, y } from 'module' default;
  8. What is a potential issue when mixing default and named exports in a single module?
    a) It causes a syntax error.
    b) It leads to a circular dependency.
    c) There is no issue; it’s completely valid.
    d) It makes the module incompatible with older versions of JavaScript.
  9. Which of the following is true about named exports?
    a) They are more flexible and can be renamed upon import.
    b) They only support exporting functions.
    c) They cannot be imported using destructuring.
    d) They are used to export a single value.
  10. How does importing a default export differ from named exports?
    a) Default imports use curly braces, while named exports do not.
    b) Named exports can be renamed, while default imports cannot.
    c) Default exports can be renamed at import, while named exports cannot.
    d) There is no difference.

Bundling and Tree Shaking

  1. What is tree shaking?
    a) Removing unused code during build time to reduce bundle size.
    b) Reorganizing code to improve readability.
    c) Encrypting the code for security.
    d) Compressing the code to save bandwidth.
  2. Which of the following tools supports tree shaking?
    a) Webpack
    b) Rollup
    c) Both a and b
    d) None of the above
  3. Which of the following does tree shaking help improve?
    a) Memory usage only
    b) Code performance only
    c) Bundle size
    d) Compilation speed
  4. In the context of bundlers, what does the term “bundling” refer to?
    a) Breaking down code into smaller modules
    b) Combining multiple files into a single file for efficient distribution
    c) Optimizing the performance of the code
    d) Sorting files in alphabetical order
  5. What is the main purpose of Webpack?
    a) To transpile JavaScript code
    b) To manage dependencies
    c) To bundle and optimize assets
    d) To check for syntax errors
  6. Which of the following is a feature of Rollup compared to Webpack?
    a) Rollup is faster at bundling large projects.
    b) Rollup is better suited for libraries and smaller projects.
    c) Rollup supports dynamic imports more efficiently.
    d) Rollup is a more complex tool than Webpack.
  7. Which of the following is NOT a benefit of using bundlers like Webpack or Rollup?
    a) Reduced bundle size
    b) Faster page load times
    c) Code splitting
    d) Eliminating the need for JavaScript
  8. What is code splitting in the context of bundlers?
    a) Splitting the code into separate files to load only when necessary
    b) Breaking down large functions into smaller ones
    c) Making the code more readable
    d) Combining multiple files into one
  9. Which of the following is the default behavior of Webpack regarding tree shaking?
    a) Tree shaking is enabled by default for all projects.
    b) Tree shaking is disabled by default but can be enabled through configuration.
    c) Webpack does not support tree shaking.
    d) Tree shaking is automatically done only if the project uses ES6 syntax.
  10. What is the primary advantage of using a bundler like Webpack or Rollup for modular code?
    a) Faster execution of code
    b) The ability to bundle and optimize the entire codebase
    c) Ensuring compatibility with legacy browsers
    d) Automatically generating test cases for code

Answer Key

QnoAnswer (Option with Text)
1b) import
2b) It allows exporting a single value from a module.
3b) import x from ‘module’;
4b) import { x } from ‘module’;
5b) It makes variables or functions available for use in other files.
6a) export function myFunction() {};
7c) module.exports = myFunc;
8c) Named exports can be renamed during import.
9a) import * as x from ‘module’;
10b) A syntax error occurs.
11a) Default exports can be renamed upon import; named exports cannot.
12a) import defaultValue, { x, y } from ‘module’;
13c) Both can be imported separately or together.
14a) export default foo; export { bar };
15b) No, only one default export is allowed per module.
16d) export { x, y };
17a) import { x, y } from ‘module’;
18c) There is no issue; it’s completely valid.
19a) They are more flexible and can be renamed upon import.
20c) Default exports can be renamed at import, while named exports cannot.
21a) Removing unused code during build time to reduce bundle size.
22c) Both a and b
23c) Bundle size
24b) Combining multiple files into a single file for efficient distribution
25c) To bundle and optimize assets
26b) Rollup is better suited for libraries and smaller projects.
27d) Eliminating the need for JavaScript
28a) Splitting the code into separate files to load only when necessary
29b) Tree shaking is disabled by default but can be enabled through configuration.
30b) The ability to bundle and optimize the entire codebase

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top