What is the expected output if greet.js exports a function that returns `Hello, ${name}!`?
medium
A. Hello, Anna!
B. greet is not defined
C. undefined
D. SyntaxError
Solution
Step 1: Understand module import and function call
The greet module exports a function that returns a greeting string.
Step 2: Predict console output
Calling greet('Anna') returns Hello, Anna!, which is logged.
Final Answer:
Hello, Anna! -> Option A
Quick Check:
Function call output = Hello, Anna! [OK]
Hint: Imported functions return expected results when called [OK]
Common Mistakes:
Assuming greet is undefined without import
Expecting undefined if export is missing
Confusing syntax errors with runtime output
4. What is wrong with this Node.js code snippet?
const utils = require('./utils');
console.log(utils.add(2, 3));
// utils.js content:
// module.exports = {
// add: (a, b) => a + b
// }
medium
A. The require path should include file extension
B. The module.exports syntax is incorrect
C. No error, code works correctly
D. The add function is not exported properly
Solution
Step 1: Check require path usage
Node.js allows requiring files without extension if .js is default.
Step 2: Verify module.exports and function export
The add function is correctly exported as an object property.
Step 3: Confirm usage in main file
Calling utils.add(2, 3) is valid and returns 5.
Final Answer:
No error, code works correctly -> Option C
Quick Check:
Correct export and import = works [OK]
Hint: Default .js extension is optional in require [OK]
Common Mistakes:
Thinking file extension is mandatory in require
Believing module.exports syntax is wrong
Assuming function is not exported properly
5. You have two modules: math.js exports functions add and multiply, and app.js imports them. How does using modules help when your project grows larger?
hard
A. Modules automatically speed up your code execution
B. Modules let you reuse code and avoid repeating functions in many files
C. Modules prevent any bugs from happening
D. Modules force all code to be in one file
Solution
Step 1: Understand code reuse with modules
Modules allow sharing functions like add and multiply across files without rewriting.
Step 2: Evaluate other options
Modules do not automatically speed up code or prevent bugs, nor do they force single-file code.
Final Answer:
Modules let you reuse code and avoid repeating functions in many files -> Option B