QuizzesGenerics — Concept Check
intermediate
javascript

Generics — Concept Check

Questions on Generics. Generics carry a type through a function instead of losing it. Familiar from Java, with two differences worth knowing: inference is much stronger, and there is no erasure workaround because there is nothing to work around.

7 minutes 7 questions Pass at 70%
Time Limit
7 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

7 questions on Generics — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a function `first<T>(arr: T[]): T | undefined` in TypeScript. What happens when you call `first(['apple', 'banana', 'cherry'])`?

    • AIt returns 'apple' with type string.
    • BIt returns 'apple' with type any.
    • CIt returns 'apple' with type unknown.
    • DIt returns 'apple' with type number.
  2. 2.In TypeScript, you have a function `longest<T extends { length: number }>(a: T, b: T): T`. What happens if you call `longest(1, 2)`?

    • AIt returns undefined because T cannot be inferred.
    • BIt returns 1 or 2 based on comparison.
    • CIt returns NaN because of invalid comparison.
    • DIt throws an error because number has no length property.
  3. 3.A developer writes `function log<T>(value: T): void { console.log(value) }`. What is the issue with this generic function?

    • AIt unnecessarily uses a generic type T.
    • BIt cannot log objects correctly.
    • CIt only works with primitive types.
    • DIt throws a type error on execution.
  4. 4.You have a function `pluck<T, K extends keyof T>(obj: T, key: K): T[K]`. What will happen if you call `pluck(user, 'emial')`?

    • AIt returns null because the key is incorrect.
    • BIt logs a warning and returns undefined.
    • CIt returns undefined because 'emial' is not a key.
    • DIt throws an error because 'emial' is not a key of T.
  5. 5.A developer uses `function len<T extends string>(s: T): number { return s.length }`. Why is this function considered pointless?

    • AIt does not handle null values.
    • BThe type constraint is redundant.
    • CIt lacks error handling.
    • DIt only works with strings.
  6. 6.In TypeScript, what is the purpose of using `extends` in a generic type parameter?

    • ATo allow any type to be used without restriction.
    • BTo enforce nominal typing like in Java.
    • CTo ensure the type has certain properties or methods.
    • DTo restrict the type to a specific set of values.
  7. 7.You have a function `mapValues<T, U>(obj: Record<string, T>, fn: (v: T) => U): Record<string, U>`. What does this function do?

    • AFilters object keys based on a function.
    • BTransforms each value in an object using a function.
    • CMaps keys to values directly without transformation.
    • DConverts an object to an array of values.