OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

21.

MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.

21. (C++) Templates

What is the purpose of the "typename" and "template" keywords in template parameters?

Pick ONE option

Choose one option.
Show answer & explanation
Answer: A. "typename" is used to declare the template parameter and "template" is used to specify the template type being passed as a parameter.

In C++ templates, "typename" (or "class") declares a type template parameter in the template declaration, while "template" is used to declare template template parameters—parameters that accept other templates. Option A correctly distinguishes their roles: typename declares parameters, and template is needed when passing templates as arguments.

Step-by-step Derivation:
Consider template declarations: 'template' uses typename to declare a type parameter. For template template parameters, you use 'template<template class C>' where 'template' indicates the parameter itself is a template. Option A correctly identifies typename's role in parameter declaration and template's role in template parameters. Options B reverses these roles (incorrect). Option C claims they're interchangeable (false—they serve different purposes). Option D is incorrect because both keywords are essential in their respective contexts.