Classes and Object-Oriented Programming in TypeScript Quiz Quiz

Challenge your knowledge of object-oriented principles and class syntax in TypeScript with this quiz covering inheritance, access modifiers, interfaces, and more. Strengthen your understanding of how TypeScript adds type safety and structure to classic object-oriented programming concepts.

  1. TypeScript Class Syntax Basics

    In TypeScript, which keyword is used to create a new instance of a class named Animal, as shown in 'class Animal {}'?

    1. new Animal()
    2. Animal.new()
    3. create Animal()
    4. instantiate Animal()

    Explanation: The correct way to instantiate a class in TypeScript is by using the 'new' keyword followed by the class name and parentheses, making 'new Animal()' the valid option. 'create Animal()' and 'instantiate Animal()' are not valid TypeScript syntax. 'Animal.new()' is also incorrect, as 'new' is not a method of the class but a keyword used to create new objects.

  2. Inheritance and Class Extension

    Which syntax correctly makes a class Dog inherit from another class Animal in TypeScript?

    1. class Dog implements Animal {}
    2. class Dog inherits Animal {}
    3. class Dog : Animal {}
    4. class Dog extends Animal {}

    Explanation: 'class Dog extends Animal {}' properly uses the 'extends' keyword to establish inheritance from Animal to Dog. 'implements' is used for interfaces, not class inheritance. 'inherits' and the colon syntax ':', as in other languages, are not valid in TypeScript for inheritance.

  3. Access Modifiers and Member Visibility

    Given 'class Person { private name: string; public age: number; }', which property can be accessed directly from outside an instance?

    1. private
    2. Person
    3. age
    4. name

    Explanation: 'age' is declared as public, so it can be accessed directly from outside the class instance. 'name' is private and cannot be accessed outside of the class. 'private' is a modifier, not a property, and 'Person' is the class itself, not a property. Only public properties are accessible from outside the class.

  4. Interfaces vs Abstract Classes

    Which statement best describes a key difference between interfaces and abstract classes in TypeScript?

    1. Abstract classes cannot define any methods.
    2. Interfaces can only include private members.
    3. Interfaces cannot contain implementation code, whereas abstract classes can provide method implementations.
    4. Interfaces are created using the 'class' keyword.

    Explanation: Interfaces in TypeScript only define the shape and signatures of members and cannot provide any actual code; abstract classes, however, can contain implementations for some methods. Interfaces do not have private members, while 'abstract classes cannot define any methods' is incorrect since they can. Interfaces are created with the 'interface' keyword, not 'class'.

  5. Static Methods and Properties

    How is a static method defined and called in a TypeScript class named Calculator?

    1. static add(a: number, b: number): number; new Calculator().add(2, 3);
    2. add(a: number, b: number) static: number; new Calculator().add(2, 3);
    3. static add(a: number, b: number): number; Calculator.add(2, 3);
    4. add static(a: number, b: number): number; Calculator.add(2, 3);

    Explanation: Static methods are defined with the 'static' keyword and called on the class itself, such as 'Calculator.add(2, 3)'. The second and third options have invalid syntax and use of 'static'. The last option uses the 'new' keyword and instance method call, which is incorrect for static members.