Any2Quiz
Computer Science / Languages / TypeScript

Syntax Quiz - Languages TypeScript

Free practice quiz: Syntax (Languages TypeScript). 52 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 52 questions free

Sample questions

  1. In TypeScript, which keyword is used to declare a variable that cannot be reassigned?

    1. let
    2. var
    3. const
    4. static
    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.

  2. What is the TypeScript syntax for defining a function that accepts a string and returns a number?

    1. function greet(name: string): number { ... }
    2. function greet(name: string) number { ... }
    3. function greet(name): number { ... }
    4. function greet(name: string) => 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.

  3. What is the purpose of the `as` keyword in TypeScript?

    1. To define a new type
    2. To assert a type of a value
    3. To extend a class
    4. To declare a constant
    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.

  4. What does the `implements` keyword do in TypeScript?

    1. Inherits properties from a parent class
    2. Defines a class that adheres to a specific interface
    3. Creates a new interface
    4. Exports members from a module
    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.

  5. Which operator can be used to combine two or more union types into a single type that represents all possible values?

    1. & (Intersection)
    2. | (Union)
    3. + (Addition)
    4. && (Logical AND)
    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.

Take the full quiz

More quizzes

Browse all TypeScript quizzes