How do you declare a simple function in TypeScript that adds two numbers?
You declare it using the <code>function</code> keyword, specify parameter types, and return type. Example:<br><pre>function add(a: number, b: number): number {<br> return a + b;<br>}</pre>
Click to reveal answer
beginner
What is the syntax to declare a class with a constructor in TypeScript?
Use the <code>class</code> keyword, define a <code>constructor</code> method to initialize properties. Example:<br><pre>class Person {<br> name: string;<br> constructor(name: string) {<br> this.name = name;<br> }<br>}</pre>
Click to reveal answer
beginner
What does the <code>this</code> keyword refer to inside a class method?
this refers to the current instance of the class, allowing access to its properties and other methods.
Click to reveal answer
intermediate
How do you declare a function expression with a type annotation in TypeScript?
You can assign a function to a variable with a type. Example:<br><pre>const multiply: (x: number, y: number) => number = (x, y) => x * y;</pre>
Click to reveal answer
intermediate
What is the difference between a function declaration and a function expression in TypeScript?
A function declaration is hoisted and can be called before it is defined. A function expression is assigned to a variable and is not hoisted, so it must be defined before use.
Click to reveal answer
Which keyword is used to declare a class in TypeScript?
Aclass
Bfunction
Cobject
Dstruct
✗ Incorrect
The class keyword is used to declare classes in TypeScript.
What is the correct way to specify the return type of a function that returns a string?
Afunction greet(): String {}
Bfunction greet(): string {}
Cfunction greet(): str {}
Dfunction greet() -> string {}
✗ Incorrect
In TypeScript, the return type is specified after the parameter list with a colon, like (): string.
How do you access a property named age inside a class method?
Athis.age
Bself.age
Cage
Dclass.age
✗ Incorrect
Inside a class method, use this.propertyName to access properties.
Which of the following is a valid function expression in TypeScript?
Alet f = (x) => x * 2;
Bfunction f(x: number): number { return x * 2; };
Cconst f = function(x: number): number { return x * 2; };
Dvar f: function = (x) => x * 2;
✗ Incorrect
Option C shows a valid function expression with type annotations.
What happens if you call a function expression before it is defined?
AIt works fine because all functions are hoisted.
BIt calls the function but with default parameters.
CIt returns undefined.
DIt causes an error because function expressions are not hoisted.
✗ Incorrect
Function expressions are not hoisted, so calling them before definition causes an error.
Explain how to declare a class with a constructor and a method in TypeScript.
Think about how you create a blueprint for objects with properties and actions.
You got /5 concepts.
Describe the difference between function declarations and function expressions in TypeScript.
Consider when you can call the function in your code relative to where it is written.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a function in TypeScript?
easy
A. To create a new type of variable
B. To store data without any behavior
C. To perform a specific task and optionally return a result
D. To define a constant value
Solution
Step 1: Understand what a function does
A function is a block of code designed to perform a specific task.
Step 2: Recognize the role of return values
Functions can return results after performing their task, making them reusable.
Final Answer:
To perform a specific task and optionally return a result -> Option C
Quick Check:
Function purpose = perform task + return result [OK]
Hint: Functions do tasks and can give back results [OK]
Common Mistakes:
Confusing functions with variables
Thinking functions only store data
Believing functions cannot return values
2. Which of the following is the correct way to declare a function named greet that takes a string name and returns a string in TypeScript?
easy
A. function greet(name: string): string { return `Hello, ${name}`; }
B. function greet(name) { return 'Hello, ' + name; }
C. function greet(name: string) { console.log('Hello, ' + name); }
D. function greet(): string { return name; }
Solution
Step 1: Check function parameter and return types
function greet(name: string): string { return `Hello, ${name}`; } correctly declares the parameter name as a string and the return type as string.
Step 2: Verify function body returns a string
function greet(name: string): string { return `Hello, ${name}`; } returns a template string greeting the name, matching the return type.
Final Answer:
function greet(name: string): string { return `Hello, ${name}`; } -> Option A
Quick Check:
Correct parameter and return types = function greet(name: string): string { return `Hello, ${name}`; } [OK]
Hint: Specify parameter and return types explicitly [OK]
Common Mistakes:
Omitting parameter types in TypeScript
Not returning a value when return type is declared
Using console.log instead of return
3. What will be the output of the following TypeScript code?
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `Hi, I am ${this.name}`;
}
}
const p = new Person('Anna');
console.log(p.greet());
medium
A. Hi, I am Anna
B. Hi, I am undefined
C. Error: Property 'name' does not exist
D. Hi, I am
Solution
Step 1: Understand class construction
The Person class sets this.name to the passed argument 'Anna' in the constructor.
Step 2: Analyze the greet method output
The greet method returns a string including this.name, which is 'Anna'.
Final Answer:
Hi, I am Anna -> Option A
Quick Check:
Class property set correctly = 'Hi, I am Anna' [OK]
Hint: Constructor sets properties; methods use them [OK]
Common Mistakes:
Forgetting to assign constructor parameter to this.name
Expecting undefined because of missing this
Confusing method return with console.log output
4. Identify the error in this TypeScript class declaration:
class Car {
model: string;
constructor(model) {
this.model = model;
}
display() {
console.log('Model: ' + this.model);
}
}
medium
A. Property 'model' should be private
B. Missing return type for display method
C. Constructor should not have parameters
D. Missing type annotation for constructor parameter 'model'
Solution
Step 1: Check constructor parameter types
In TypeScript, constructor parameters should have type annotations; here, model lacks a type.
Step 2: Verify other parts
The display method does not require a return type annotation since it returns void implicitly.
Final Answer:
Missing type annotation for constructor parameter 'model' -> Option D
Quick Check:
Constructor parameters need types = Missing type annotation for constructor parameter 'model' [OK]
Hint: Always type constructor parameters in TypeScript [OK]
Common Mistakes:
Omitting parameter types in constructors
Thinking methods must always declare return types
Assuming properties must be private by default
5. You want to create a TypeScript class Calculator with a method add that takes two numbers and returns their sum. Which code correctly implements this with proper types and usage?
hard
A. class Calculator {
add(a, b) {
return a + b;
}
}
const calc = Calculator();
console.log(calc.add(5, 3));
B. class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3));
C. class Calculator {
add(a: number, b: number) {
console.log(a + b);
}
}
const calc = new Calculator();
calc.add(5, 3);
D. class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add('5', '3'));
Solution
Step 1: Check method declaration and types
class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3)); declares add with parameters typed as numbers and return type number, which is correct.
Step 2: Verify object creation and method call
class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3)); creates an instance with new Calculator() and calls add(5, 3), which returns 8.
Final Answer:
Correct class with typed method and proper usage -> Option B
Quick Check:
Typed method + new instance + correct call = class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3)); [OK]
Hint: Use 'new' to create class instances and type parameters [OK]
Common Mistakes:
Calling class without 'new'
Omitting parameter types
Passing wrong argument types (like strings instead of numbers)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.