Unit 5 - Practice Quiz

INT219 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which of the following best describes TypeScript?

A. A completely new language replacing JavaScript
B. A superset of JavaScript that adds static typing
C. A framework for building mobile applications
D. A database management system for JSON

2 What is the standard file extension for a TypeScript file?

A. .javascript
B. .tsx
C. .ts
D. .tscript

3 Can browsers execute TypeScript code directly?

A. Yes, modern browsers support TypeScript natively
B. No, TypeScript must be compiled (transpiled) into JavaScript
C. Only Chrome supports TypeScript execution
D. Yes, if the <script type="text/typescript"> tag is used

4 Which command is used to compile a TypeScript file named main.ts?

A. node main.ts
B. compile main.ts
C. tsc main.ts
D. ts-node main.ts

5 In TypeScript, what is the syntax to annotate a variable count as a number?

A. var count = number
B. let count: number
C. int count
D. number count

6 Which type represents the absence of any type information and allows any operation?

A. void
B. unknown
C. never
D. any

7 What is the difference between void and never?

A. void is for functions that return nothing; never is for functions that never return (e.g., throw error)
B. void is deprecated; never is the modern replacement
C. void means null; never means undefined
D. There is no difference

8 How do you define an array of strings in TypeScript?

A. Array[string]
B. string[]
C. list<string>
D. string{}

9 Which TypeScript feature allows you to define a fixed-length array where each element has a specific type?

A. Union
B. Enum
C. Tuple
D. Interface

10 Consider the following code:
enum Direction { Up, Down, Left, Right }.
What is the numeric value of Direction.Left by default?

A. 0
B. 1
C. 2
D. 3

11 Which file is used to configure the TypeScript compiler options for a project?

A. package.json
B. webpack.config.js
C. tsconfig.json
D. settings.ts

12 How do you define an optional property in an interface?

A. name: string?
B. name?: string
C. optional name: string
D. name: string | undefined

13 What is the primary purpose of an Interface in TypeScript?

A. To generate HTML templates
B. To define the structure or shape of an object
C. To compile code to binary
D. To import external libraries

14 Which keyword is used to create a new type name for a type combination, such as a union?

A. interface
B. type
C. alias
D. def

15 What does a Union Type represent?

A. A value that must contain all specified types
B. A value that can be one of several types
C. A method to join two arrays
D. A type that inherits from a parent class

16 Which symbol is used to define an Intersection type?

A. |
B. +
C. &
D. &&

17 Given the code let id: string | number;, which operation is safe to perform on id without narrowing?

A. id.toUpperCase()
B. id.toFixed()
C. id.toString()
D. id / 2

18 What is Type Inference?

A. Explicitly writing types for every variable
B. The compiler automatically determining the type based on the assigned value
C. Converting a string to a number at runtime
D. Importing types from another module

19 Which of the following creates a read-only property in an interface?

A. const id: number;
B. readonly id: number;
C. static id: number;
D. final id: number;

20 What is the output of typeof when used as a type guard for a generic object?

A. "object"
B. "interface"
C. "class"
D. "json"

21 What does the <T> syntax denote in TypeScript?

A. Template Literal
B. Type Assertion
C. Generic Type Parameter
D. Tuple Definition

22 Which keyword allows an interface to inherit properties from another interface?

A. implements
B. extends
C. inherits
D. uses

23 In the configuration "strict": true, which of the following is enabled?

A. noImplicitAny
B. strictNullChecks
C. strictFunctionTypes
D. All of the above

24 How do you narrow a type using a user-defined type guard?

A. Return boolean
B. Return arg is Type
C. Return typeof arg
D. Use as Type

25 What is the safer alternative to any that requires type checking before performing operations?

A. void
B. never
C. unknown
D. object

26 Which TypeScript feature allows a function to accept different numbers or types of arguments but implemented in a single function body?

A. Function Overloading
B. Function Overriding
C. Lambdas
D. Closures

27 Consider: type Status = "Success" | "Error";. What is this called?

A. String Enum
B. String Literal Type
C. Template String
D. Type Alias Interface

28 What does the compiler option outDir specify?

A. The directory containing input source files
B. The directory where compiled JavaScript files are placed
C. The version of ECMAScript to output
D. Files to exclude from compilation

29 Which operator is used for Type Assertions (Casting) in TypeScript?

A. like
B. cast
C. as
D. to

30 In Generics, how do you constrain a type parameter T to ensure it has a length property?

A. <T has length>
B. <T extends { length: number }>
C. <T implements Length>
D. <T: length>

31 What is the type of x in the following code: let x = [10, "hello"];?

A. [number, string]
B. (number | string)[]
C. any[]
D. Object[]

32 Which logical operator is used in TypeScript to check if a property exists in an object for narrowing?

A. has
B. exists
C. in
D. of

33 What does the target option in tsconfig.json control?

A. The TypeScript version used for checking
B. The specific browser version to support
C. The ECMAScript version of the output JavaScript
D. The Operating System target

34 How can you define a function type using an interface?

A. interface Func { (): void; }
B. interface Func { function(): void; }
C. interface Func { run(): void; }
D. interface Func = () => void

35 What happens if you try to assign a number to a variable typed never?

A. It works fine
B. It throws a runtime error
C. It causes a compilation error
D. The number is converted to never

36 Which utility type constructs a type with all properties of Type set to optional?

A. Required<Type>
B. Partial<Type>
C. Pick<Type>
D. Omit<Type>

37 What is 'Structural Typing' (often called Duck Typing) in TypeScript?

A. Types are checked based on their names
B. Types are checked based on their inheritance hierarchy
C. Types are checked based on their shape/members
D. Types are strictly nominal

38 If you have interface A { x: number } and interface B { y: number }, what properties must an object of type A & B have?

A. Only x
B. Only y
C. Either x or y
D. Both x and y

39 What is the purpose of noEmitOnError: true in tsconfig.json?

A. It prevents the compiler from running
B. It deletes source files after compilation
C. It prevents generating JavaScript files if there are type errors
D. It suppresses error messages in the console

40 Which syntax correctly assigns a default type to a generic parameter?

A. <T = string>
B. <T : string>
C. <T extends string>
D. <T implements string>

41 What describes a 'Discriminated Union'?

A. A union where members are excluded using Omit
B. A union of types that all share a common literal property used to distinguish them
C. A union of primitive types only
D. A generic union

42 How do you access the type of a specific property age within a type Person?

A. Person.age
B. Person->age
C. Person["age"]
D. typeof Person.age

43 What does the keyof operator do?

A. Returns the values of an object
B. Produces a string or numeric literal union of the keys of an object type
C. Checks if a key exists at runtime
D. Locks the keys of an object

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?

A. { [key: string]: number }
B. { key: string, value: number }
C. { * : number }
D. Map<string, number>

45 Which compilation setting aids in debugging by mapping compiled JavaScript back to the original TypeScript source?

A. mapRoot
B. sourceMap
C. debug
D. trace

46 Can you use this in a function signature in TypeScript?

A. No, this is reserved
B. Yes, as the first parameter to declare the type of this within the function
C. Yes, but only in arrow functions
D. Yes, it replaces the return type

47 What is the difference between interface and type regarding declaration merging?

A. Only type supports declaration merging
B. Only interface supports declaration merging
C. Both support declaration merging
D. Neither supports declaration merging

48 What is a 'Tuple' useful for?

A. Storing a key-value pair
B. Returning multiple values of different types from a function
C. Creating an infinite list
D. Defining a dictionary

49 In the context of strictNullChecks, is null assignable to number?

A. Yes, always
B. No, unless strictNullChecks is false
C. Only if explicity cast
D. Yes, because null is 0

50 What does the exclude property in tsconfig.json do?

A. Removes comments from the output
B. Specifies files to be ignored by the compiler
C. Excludes types from the global scope
D. Prevents compilation of specific libraries