Directives & Templates Questions with Answers

26 practice questions on Directives & Templates (Vue.js) with answers and step-by-step solutions. Try 10 samples below, then practise all 26 free in the AptiNest app.

Sample questions

Question 1Easy

Which directive renders an element only when a condition is truthy and adds or removes it from the DOM?

  1. v-if
  2. v-show
  3. v-bind
  4. v-model

Answer: A. v-if

Explanation

v-if conditionally creates or destroys the element.

Step-by-step solution

  1. v-if adds the element to the DOM when the condition is truthy.
  2. It removes the element from the DOM when the condition is falsy.
  3. So v-if adds or removes the element (option A).

Why the other options are wrong

  • B. v-show keeps the element in the DOM and only toggles its display.
  • C. v-bind sets an attribute, not visibility.
  • D. v-model binds form input values.

Tip: v-if removes the element; v-show hides it with CSS.

Question 2Easy

What is the shorthand for v-bind:href="url"?

  1. @href="url"
  2. :href="url"
  3. #href="url"
  4. $href="url"

Answer: B. :href="url"

Explanation

: is the shorthand for v-bind.

Step-by-step solution

  1. The full form is v-bind with a colon-separated argument, such as v-bind:href.
  2. The shorthand replaces the word v-bind with a single colon.
  3. So :href is the shorthand (option B).

Why the other options are wrong

  • A. The at sign is the shorthand for v-on, not v-bind.
  • C. A hash sign is not a Vue binding shorthand.
  • D. A dollar sign is not a Vue binding shorthand.

Tip: Colon for bind, at sign for on.

Question 3Easy

Which syntax outputs a JavaScript expression's value as text in a Vue template?

  1. {{ expression }}
  2. <% expression %>
  3. ${ expression }
  4. [[ expression ]]

Answer: A. {{ expression }}

Explanation

Double curly braces (mustache syntax) interpolate text.

Step-by-step solution

  1. Write mustaches.
  2. Vue evaluates and escapes the result.

Why the other options are wrong

  • B. EJS-style, not Vue.
  • C. Template literal syntax, not Vue.
  • D. [[ ]] is not Vue interpolation; Vue uses double curly braces.

Question 4Easy

Which directive is used to loop over an array in a template?

  1. v-loop
  2. v-each
  3. v-repeat
  4. v-for

Answer: D. v-for

Explanation

v-for="item in items" renders the element for each entry.

Step-by-step solution

  1. Vue has a built-in directive for rendering lists in templates.
  2. The directive is written as item in items.
  3. It renders the element once for each entry.

Why the other options are wrong

  • A. v-loop is not a Vue directive.
  • B. v-each is not a Vue directive.
  • C. v-repeat is not a Vue directive.

Tip: Use v-for with item in items, and add a key to each rendered element.

Question 5Medium

What is the difference between v-show and v-if?

  1. v-show removes the element from the DOM, while v-if hides it with CSS
  2. They are identical except for the spelling of the name
  3. v-show can only be applied to components, not plain elements
  4. v-show toggles CSS display; v-if adds or removes the node

Answer: D. v-show toggles CSS display; v-if adds or removes the node

Explanation

v-show keeps the element in the DOM and toggles display; v-if has higher toggle cost but lower initial cost when false.

Step-by-step solution

  1. v-show always keeps the element in the DOM.
  2. It toggles the CSS display property.
  3. v-if adds or removes the element from the DOM instead.
  4. So v-show toggles CSS display and v-if adds or removes the node (option D).

Why the other options are wrong

  • A. The two are reversed: v-show does not remove the element.
  • B. They work differently in the DOM, not just in spelling.
  • C. v-show works on plain elements as well as components.

Tip: Use v-show for frequent toggling and v-if when the element is rarely needed.

Question 6Medium

What is a valid way to bind several classes conditionally?

  1. :class="active: isOn, bold"
  2. class="{{ active }}"
  3. v-class="active"
  4. :class="{ active: isOn, 'text-bold': bold }"

Answer: D. :class="{ active: isOn, 'text-bold': bold }"

Explanation

:class accepts an object whose keys are class names and whose values are conditions.

Step-by-step solution

  1. Use v-bind on class.
  2. Pass an object of name to boolean.

Why the other options are wrong

  • A. Not a valid object literal.
  • B. Mustaches are not allowed in attributes.
  • C. No v-class directive.

Question 7Medium

Which v-model modifier trims whitespace from user input?

  1. .lazy
  2. .number
  3. .once
  4. .trim

Answer: D. .trim

Explanation

.trim removes leading and trailing whitespace; .lazy syncs on change, .number casts to number.

Step-by-step solution

  1. The trim modifier removes leading and trailing whitespace from the input.
  2. The lazy modifier syncs on change instead of on each input.
  3. The number modifier casts the value to a number.
  4. So the whitespace modifier is .trim (option D).

Why the other options are wrong

  • A. .lazy syncs on change; it does not trim.
  • B. .number converts the value to a number; it does not trim.
  • C. .once is not a v-model modifier.

Tip: v-model modifiers: .trim trims, .lazy waits for change, .number casts to a number.

Question 8Medium

What does v-model on a text input do?

  1. Two-way binding of input value and data
  2. Only binds data to the input, never the reverse
  3. Only listens to the input event without binding a value
  4. Validates the input and blocks invalid values automatically

Answer: A. Two-way binding of input value and data

Explanation

v-model binds the value and updates the data on input events.

Step-by-step solution

  1. v-model on a text input binds the input's value to the data.
  2. When the user types, the data is updated; when the data changes, the input updates.
  3. So it is two-way binding (option A).

Why the other options are wrong

  • B. It binds both ways, not only from data to the input.
  • C. It binds the value as well as listening to input events.
  • D. v-model does not validate input.

Tip: v-model is two-way binding for form inputs.

Question 9Hard

Why is <template v-if> useful?

  1. It groups several siblings under one condition without adding a wrapper element
  2. It lazily loads the template file from the server only when the condition turns true
  3. It enables server rendering
  4. It caches the output forever

Answer: A. It groups several siblings under one condition without adding a wrapper element

Explanation

The <template> tag is a non-rendered wrapper that the compiler removes.

Step-by-step solution

  1. Put v-if on template.
  2. Children are shown or hidden together.
  3. No extra DOM node.

Why the other options are wrong

  • B. It is not about loading.
  • C. SSR is unrelated.
  • D. No caching is done.

Question 10Hard

Can v-if and v-for be used on the same element, and what is the Vue 3 behavior?

  1. Yes, and v-for has the higher priority of the two directives
  2. No, combining them is always a compile-time error
  3. Yes, but v-if runs first and cannot see the loop variable
  4. Yes, and both are evaluated together with a shared scope

Answer: C. Yes, but v-if runs first and cannot see the loop variable

Explanation

In Vue 3, v-if is evaluated before v-for; wrap in a <template v-for> instead.

Step-by-step solution

  1. In Vue 3, v-if has higher priority than v-for on the same element.
  2. So v-if is evaluated first, and it cannot see the loop variable.
  3. To fix this, wrap the loop in a template element that has v-for, and put v-if inside.
  4. So the answer is option C.

Why the other options are wrong

  • A. In Vue 3 v-if has the higher priority, not v-for.
  • B. Combining them is allowed; it is a precedence issue, not a compile error.
  • D. They are not evaluated together; v-if runs first.

Tip: Do not put v-if and v-for on the same element in Vue 3; move the condition to a wrapper.

Practise all 26 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Vue.js topics · All topics