Unit 5 - Practice Quiz
1 Which of the following best describes TypeScript?
2 What is the standard file extension for a TypeScript file?
3 Can browsers execute TypeScript code directly?
<script type="text/typescript"> tag is used
4
Which command is used to compile a TypeScript file named main.ts?
node main.ts
compile main.ts
tsc main.ts
ts-node main.ts
5
In TypeScript, what is the syntax to annotate a variable count as a number?
var count = number
let count: number
int count
number count
6 Which type represents the absence of any type information and allows any operation?
void
unknown
never
any
7
What is the difference between void and never?
void is for functions that return nothing; never is for functions that never return (e.g., throw error)
void is deprecated; never is the modern replacement
void means null; never means undefined
8 How do you define an array of strings in TypeScript?
Array[string]
string[]
list<string>
string{}
9 Which TypeScript feature allows you to define a fixed-length array where each element has a specific type?
10
Consider the following code:
enum Direction { Up, Down, Left, Right }.
What is the numeric value of Direction.Left by default?
11 Which file is used to configure the TypeScript compiler options for a project?
package.json
webpack.config.js
tsconfig.json
settings.ts
12 How do you define an optional property in an interface?
name: string?
name?: string
optional name: string
name: string | undefined
13 What is the primary purpose of an Interface in TypeScript?
14 Which keyword is used to create a new type name for a type combination, such as a union?
interface
type
alias
def
15 What does a Union Type represent?
16 Which symbol is used to define an Intersection type?
|
+
&
&&
17
Given the code let id: string | number;, which operation is safe to perform on id without narrowing?
id.toUpperCase()
id.toFixed()
id.toString()
id / 2
18 What is Type Inference?
19 Which of the following creates a read-only property in an interface?
const id: number;
readonly id: number;
static id: number;
final id: number;
20
What is the output of typeof when used as a type guard for a generic object?
"object"
"interface"
"class"
"json"
21
What does the <T> syntax denote in TypeScript?
22 Which keyword allows an interface to inherit properties from another interface?
implements
extends
inherits
uses
23
In the configuration "strict": true, which of the following is enabled?
noImplicitAny
strictNullChecks
strictFunctionTypes
24 How do you narrow a type using a user-defined type guard?
boolean
arg is Type
typeof arg
as Type
25
What is the safer alternative to any that requires type checking before performing operations?
void
never
unknown
object
26 Which TypeScript feature allows a function to accept different numbers or types of arguments but implemented in a single function body?
27
Consider: type Status = "Success" | "Error";. What is this called?
28
What does the compiler option outDir specify?
29 Which operator is used for Type Assertions (Casting) in TypeScript?
like
cast
as
to
30
In Generics, how do you constrain a type parameter T to ensure it has a length property?
<T has length>
<T extends { length: number }>
<T implements Length>
<T: length>
31
What is the type of x in the following code: let x = [10, "hello"];?
[number, string]
(number | string)[]
any[]
Object[]
32 Which logical operator is used in TypeScript to check if a property exists in an object for narrowing?
has
exists
in
of
33
What does the target option in tsconfig.json control?
34 How can you define a function type using an interface?
interface Func { (): void; }
interface Func { function(): void; }
interface Func { run(): void; }
interface Func = () => void
35
What happens if you try to assign a number to a variable typed never?
never
36 Which utility type constructs a type with all properties of Type set to optional?
Required<Type>
Partial<Type>
Pick<Type>
Omit<Type>
37 What is 'Structural Typing' (often called Duck Typing) in TypeScript?
38
If you have interface A { x: number } and interface B { y: number }, what properties must an object of type A & B have?
x
y
x or y
x and y
39
What is the purpose of noEmitOnError: true in tsconfig.json?
40 Which syntax correctly assigns a default type to a generic parameter?
<T = string>
<T : string>
<T extends string>
<T implements string>
41 What describes a 'Discriminated Union'?
Omit
42
How do you access the type of a specific property age within a type Person?
Person.age
Person->age
Person["age"]
typeof Person.age
43
What does the keyof operator do?
44 If you want to allow an object to have any number of properties where keys are strings and values are numbers, which syntax is used?
{ [key: string]: number }
{ key: string, value: number }
{ * : number }
Map<string, number>
45 Which compilation setting aids in debugging by mapping compiled JavaScript back to the original TypeScript source?
mapRoot
sourceMap
debug
trace
46
Can you use this in a function signature in TypeScript?
this is reserved
this within the function
47
What is the difference between interface and type regarding declaration merging?
type supports declaration merging
interface supports declaration merging
48 What is a 'Tuple' useful for?
49
In the context of strictNullChecks, is null assignable to number?
50
What does the exclude property in tsconfig.json do?