TypeScript Quiz

Welcome to The Coding College, where learning meets fun! At The Coding College, we believe quizzes are a great way to reinforce your understanding. Take this TypeScript Quiz to evaluate your knowledge and identify areas for improvement.

Quiz Instructions

  1. Answer each question carefully; multiple-choice questions may have more than one correct answer.
  2. Review your answers at the end to see detailed explanations.
  3. No cheating! Use this as an opportunity to learn.

Beginner-Level Questions

1. What is TypeScript?
a) A superset of JavaScript
b) A new programming language
c) Used only for backend development
d) Developed by Microsoft

2. How do you declare a variable in TypeScript with a specific type?
a) let num: number = 10;
b) let num = 10;
c) int num = 10;
d) var num: number = 10;

3. What does the tsc command do in TypeScript?
a) Installs TypeScript
b) Transpiles TypeScript into JavaScript
c) Runs TypeScript code directly in the browser
d) Creates a tsconfig.json file

4. Which of the following are valid types in TypeScript?
a) string
b) number
c) boolean
d) function

Intermediate-Level Questions

5. What is the output of the following TypeScript code?

let x: string | number;
x = 5;
x = "Hello";
console.log(typeof x);

a) number
b) string
c) undefined
d) Error

6. What is an interface used for in TypeScript?
a) Defining the structure of an object
b) Implementing functionality
c) Compiling TypeScript to JavaScript
d) Extending built-in types

7. Which of the following is a correct use of a tuple in TypeScript?
a) let myTuple: [number, string] = [5, "Hello"];
b) let myTuple: {number, string} = [5, "Hello"];
c) let myTuple: [string, number] = ["Hello", 5];
d) Both a and c

8. What does the readonly keyword do in TypeScript?
a) Prevents a property from being changed after initialization
b) Makes an entire class immutable
c) Prevents an object from being deleted
d) Ensures strict type-checking

Advanced-Level Questions

9. What is a generic in TypeScript?
a) A way to define reusable code components that work with multiple types
b) A special type of variable for arrays
c) A function that only works with strings and numbers
d) None of the above

10. What is the purpose of the keyof operator in TypeScript?
a) To create new properties for an object
b) To extract the keys of an object as a union of strings
c) To iterate over the values of an object
d) To define the type of a key-value pair

11. What is the difference between type and interface in TypeScript?
a) type is used for primitive types, interface is not.
b) interface is extendable, type is not.
c) Both are interchangeable, but type can handle unions.
d) None of the above.

12. How do you enable strict mode in TypeScript?
a) Add "strict": true in tsconfig.json
b) Use the --strict flag with the tsc command
c) Declare use strict in your code
d) Both a and b

Answer Key

Beginner-Level Answers

  1. a, d – TypeScript is a superset of JavaScript developed by Microsoft.
  2. a – Variables with a type annotation are declared like let num: number = 10;.
  3. b – The tsc command transpiles TypeScript into JavaScript.
  4. a, b, c – string, number, and boolean are valid types; function is not.

Intermediate-Level Answers

  1. b – The typeof operator returns string for "Hello".
  2. a – Interfaces define the structure of objects in TypeScript.
  3. d – Both a and c are valid ways to declare a tuple.
  4. a – The readonly keyword makes a property immutable after initialization.

Advanced-Level Answers

  1. a – Generics enable reusable components that work with multiple types.
  2. b – The keyof operator extracts the keys of an object as a union of strings.
  3. c – Both are similar, but type is more versatile and can handle unions.
  4. d – You can enable strict mode using both "strict": true in tsconfig.json and the --strict flag.

Scorecard

  • 0–4: Beginner – It’s a great time to revisit TypeScript basics!
  • 5–8: Intermediate – You’re on your way to mastering TypeScript!
  • 9–12: Advanced – Well done! You’re a TypeScript pro.

Challenge Yourself

Share your quiz results or challenge your peers at The Coding College. Stay tuned for more quizzes, tutorials, and resources to enhance your TypeScript skills!

Leave a Comment