What keyword do you use to bring code from another module?
Aimport
Bexport
Crequire
Dinclude
✗ Incorrect
The import keyword brings code from another module.
Which benefit is NOT provided by using modules?
AMaking code run without errors automatically
BAvoiding name conflicts
CEasier code maintenance
DBetter code organization
✗ Incorrect
Modules help organize and maintain code but do not automatically fix errors.
Explain why modules are important in TypeScript and how they help manage code.
Think about how splitting a big task into smaller tasks makes it easier to handle.
You got /4 concepts.
Describe how the export and import keywords work in TypeScript modules.
Imagine sharing toys with friends and asking to borrow theirs.
You got /3 concepts.
Practice
(1/5)
1. Why do we need modules in TypeScript?
easy
A. To split code into smaller, manageable parts
B. To make code run faster
C. To write code without any errors
D. To avoid using variables
Solution
Step 1: Understand the purpose of modules
Modules help organize code by splitting it into smaller parts.
Step 2: Identify the main benefit
This splitting makes code easier to manage and understand.
Final Answer:
To split code into smaller, manageable parts -> Option A
Quick Check:
Modules organize code = B [OK]
Hint: Modules help organize and manage code easily [OK]
Common Mistakes:
Thinking modules make code faster
Believing modules fix all errors automatically
Confusing modules with variable declarations
2. Which of the following is the correct way to export a function named greet from a TypeScript module?
easy
A. function greet() { console.log('Hello'); }
B. export function greet() { console.log('Hello'); }
C. export greet function() { console.log('Hello'); }
D. function greet() { console.log('Hello'); } export greet;
Solution
Step 1: Recall correct export syntax
In TypeScript, to export a function, use export function functionName() {}.
Step 2: Check each option
export function greet() { console.log('Hello'); } uses correct syntax. Options B and C have wrong order or missing keywords. function greet() { console.log('Hello'); } does not export.
Final Answer:
export function greet() { console.log('Hello'); } -> Option B
Quick Check:
Correct export syntax = D [OK]
Hint: Use 'export' before function to export it [OK]
Common Mistakes:
Placing 'export' after function name
Forgetting to export the function
Using invalid export syntax
3. Consider two TypeScript files: // file1.ts export const value = 42; // file2.ts import { value } from './file1'; console.log(value); What will be the output when file2.ts is run?
medium
A. null
B. undefined
C. Error: Cannot find module './file1'
D. 42
Solution
Step 1: Understand export and import
file1.ts exports a constant named value with 42. file2.ts imports it correctly.
Step 2: Predict console output
Since import is correct, console.log(value) prints 42.
Final Answer:
42 -> Option D
Quick Check:
Imported value logs 42 = A [OK]
Hint: Imported constants print their exported values [OK]
Common Mistakes:
Assuming import fails without file extension
Expecting undefined without export
Confusing import syntax
4. What is wrong with this TypeScript module code?
export const a = 10; const b = 20; import { b } from './moduleB';
medium
A. The import statement must be at the top before any other code
B. You cannot import a variable named 'b' if it's declared locally
C. You cannot import after export in the same file
D. There is no error in this code
Solution
Step 1: Check import statement placement
In TypeScript, import statements must be at the top before any other code.
Step 2: Identify the error
Here, import comes after export and variable declaration, which is invalid.
Final Answer:
The import statement must be at the top before any other code -> Option A
Quick Check:
Imports must be top-level = A [OK]
Hint: Always place imports at the top of the file [OK]
Common Mistakes:
Placing imports after code
Thinking imports can be anywhere
Confusing import and export order
5. You have two modules: // mathUtils.ts export function add(x: number, y: number) { return x + y; } export function multiply(x: number, y: number) { return x * y; } // app.ts import * as math from './mathUtils'; console.log(math.add(2, 3)); console.log(math.multiply(4, 5)); Why is using import * as math helpful in this case?
hard
A. It automatically fixes type errors in imported functions
B. It makes the code run faster by bundling functions
C. It groups all exports under one name to avoid name conflicts
D. It prevents the need to export functions in mathUtils.ts
Solution
Step 1: Understand import * as syntax
This syntax imports all exported members under a single namespace object.
Step 2: Identify benefit
Grouping exports under math avoids naming conflicts and keeps code organized.
Final Answer:
It groups all exports under one name to avoid name conflicts -> Option C
Quick Check:
Namespace import groups exports = C [OK]
Hint: Use 'import * as' to group exports under one name [OK]
Common Mistakes:
Thinking it improves performance
Believing it fixes type errors automatically
Assuming exports are not needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.