Syntax Quiz - Languages TypeScript
Free practice quiz: Syntax (Languages TypeScript). 52 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages TypeScript practice quiz has 52 multiple-choice questions on Syntax, and 52 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 52 questions, freeSample questions
In TypeScript, which keyword is used to declare a variable that cannot be reassigned?
Show answer
Answer: C. const
The `const` keyword in TypeScript (and JavaScript) is used to declare variables whose values cannot be reassigned after their initial assignment. `let` allows reassignment, `var` has function scope and hoisting behavior, and `static` is related to class members, not variable declaration.
What is the TypeScript syntax for defining a function that accepts a string and returns a number?
Show answer
Answer: A. function greet(name: string): number { ... }
The standard function declaration syntax in TypeScript includes parameter type annotations after the parameter name, separated by a colon, and the return type annotation after the closing parenthesis of the parameter list, also separated by a colon.
What is the purpose of the `as` keyword in TypeScript?
Show answer
Answer: B. To assert a type of a value
The `as` keyword is used for type assertions in TypeScript. It allows you to tell the compiler that you know more about a value's type than it can infer. For example, `(someValue as string)` asserts that `someValue` is a string.
What does the `implements` keyword do in TypeScript?
Show answer
Answer: B. Defines a class that adheres to a specific interface
The `implements` keyword is used in class declarations to enforce that the class satisfies the contract defined by an interface. The class must provide implementations for all properties and methods declared in the interface.
Which operator can be used to combine two or more union types into a single type that represents all possible values?
Show answer
Answer: B. | (Union)
The pipe symbol `|` is used to create union types in TypeScript. A variable declared with a union type can hold a value of any of the types included in the union.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the primary purpose of type annotations in TypeScript?
- Which of the following is the correct syntax for a TypeScript interface?
- What is the result of `10 + '5'` in TypeScript when typed as `string` for the second operand?
- Which symbol is used to denote a single-line comment in TypeScript?
- What is the purpose of the `!` operator (non-null assertion operator) in TypeScript?
- Which type annotation is appropriate for a function that returns nothing?
- What is the correct way to define a tuple type in TypeScript?
- In TypeScript, what does the `enum` keyword allow you to do?
- Which of the following is NOT a valid primitive type in TypeScript?
- How do you access a property of an object literal in TypeScript?
- What does the `typeof` operator return for a variable declared with `let` and initialized with `undefined`?
- What is the purpose of a `namespace` in TypeScript?
- Which statement correctly declares an optional parameter in a TypeScript function?
- What is the TypeScript syntax for a generic class that takes a type parameter `T`?
- What is the result of `typeof null` in TypeScript?
- Which keyword is used to import modules in TypeScript?
- What is the correct way to define a type alias for a union of string literals?
- What is the role of the `declare` keyword in TypeScript?
- Which type annotation best describes an array of numbers in TypeScript?
- What is the difference between `interface` and `type` in TypeScript?
- What does the `readonly` modifier do when applied to a class property in TypeScript?
- Which syntax is used to define a parameter property in a TypeScript constructor?
- What is the correct way to use the spread syntax to combine two arrays in TypeScript?
- In TypeScript, what is the purpose of a `getter` method?
- What does the `this` keyword typically refer to inside a class method in TypeScript?
- How do you create an abstract class in TypeScript?
- What is the syntax for type assertion using the angle-bracket syntax?
- Which statement correctly defines a class with a constructor that accepts and initializes a `name` property?
- What is the TypeScript syntax for a callback function that takes a number and returns a boolean?
- What does the `in` operator check for in TypeScript?
- Which symbol is used for type guards in TypeScript, often within conditional statements?
- What is the TypeScript syntax for a read-only array of strings?
- What does the `keyof` operator return?
- What is the purpose of the `never` type in TypeScript?
- Which of the following correctly defines a mapped type that transforms all properties of an interface to be optional?
- What does the `export` keyword signify in a TypeScript module?
- What is the purpose of the `extends` keyword when used with interfaces in TypeScript?
- What is the correct way to declare a generic type variable that represents any type?
- What does the `is` keyword signify in a type predicate function signature in TypeScript?
- What is the purpose of the `override` keyword in TypeScript (ESNext feature)?
- How do you create a type that represents an object with string keys and number values in TypeScript?
- What is the correct syntax for a type assertion using the `as` keyword?
- What does the `protected` access modifier mean for a class member in TypeScript?
- What is the purpose of the `extends` keyword when used with classes in TypeScript?
- What is the TypeScript syntax for a readonly string literal type?
- What does the `get` keyword preface in a class definition?
- What is the correct TypeScript syntax to define an enum with string values?