Lifecycle Hooks Questions with Answers

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

Sample questions

Question 1Easy

In <script setup>, what is true of code in the top level?

  1. It runs once per application, when the app is first created and mounted
  2. It runs every time a component instance is created
  3. It runs only on mount
  4. It runs only on updates

Answer: B. It runs every time a component instance is created

Explanation

Top-level setup code is per instance, unlike a plain <script> block that runs once on import.

Step-by-step solution

  1. Each instance executes setup.
  2. Module script runs once.

Why the other options are wrong

  • A. That is a module-level script.
  • C. It runs before mount.
  • D. It is not update-bound.

Question 2Easy

In the Composition API, where does the code in <script setup> run relative to other hooks?

  1. It runs only after the component has been mounted
  2. It runs when the instance is created, before mount
  3. It runs only when the component updates
  4. It runs only when the component is unmounted

Answer: B. It runs when the instance is created, before mount

Explanation

The setup code corresponds to the creation phase.

Step-by-step solution

  1. In script setup, the setup code runs when the component instance is created.
  2. That happens before the component is mounted to the DOM.
  3. So the setup code runs at creation, before mount (option B).

Why the other options are wrong

  • A. Setup code does not wait for mounting; it runs first.
  • C. Setup code runs once at creation, not on each update.
  • D. Setup code does not run at unmount; that is the cleanup hooks' job.

Tip: Put one-time setup in script setup, and DOM work in onMounted.

Question 3Easy

In which hook is the DOM first available to access via template refs?

  1. onBeforeMount
  2. setup
  3. onCreated
  4. onMounted

Answer: D. onMounted

Explanation

onMounted runs after the component's DOM is inserted.

Step-by-step solution

  1. Setup runs.
  2. Render and insert.
  3. onMounted fires.

Why the other options are wrong

  • A. DOM does not exist yet.
  • B. setup runs before render.
  • C. No onCreated in Composition API.

Question 4Easy

Which lifecycle hook is called after the component has mounted to the DOM in Composition API?

  1. onCreated
  2. onMounted
  3. onRender
  4. onReady

Answer: B. onMounted

Explanation

onMounted runs once the component's DOM is created and inserted.

Step-by-step solution

  1. OnMounted is a Composition API lifecycle hook.
  2. It runs after the component's DOM has been created and inserted.
  3. So the hook called after mounting is onMounted (option B).

Why the other options are wrong

  • A. OnCreated is not a Composition API hook; setup covers creation.
  • C. OnRender is not a lifecycle hook for this purpose.
  • D. OnReady is not a Vue lifecycle hook.

Tip: OnMounted is the place to touch the DOM or start DOM-dependent work.

Question 5Medium

Does onUpdated run on the first render?

  1. No, only after later reactive updates
  2. Yes, it always runs right after the initial render and again on later updates
  3. Only with immediate
  4. Only on the server

Answer: A. No, only after later reactive updates

Explanation

onUpdated fires after a re-render caused by state change.

Step-by-step solution

  1. First render fires mounted.
  2. Changes fire updated.

Why the other options are wrong

  • B. Initial render triggers mounted.
  • C. There is no immediate option.
  • D. SSR does not run it.

Question 6Medium

Which Options API hook name replaced beforeDestroy in Vue 3?

  1. beforeRemove
  2. beforeDispose
  3. beforeUnmount
  4. beforeClose

Answer: C. beforeUnmount

Explanation

Vue 3 renamed beforeDestroy to beforeUnmount and destroyed to unmounted.

Step-by-step solution

  1. Vue 2 had beforeDestroy and destroyed hooks in the Options API.
  2. Vue 3 renamed beforeDestroy to beforeUnmount.
  3. So the Vue 3 hook name is beforeUnmount (option C).

Why the other options are wrong

  • A. BeforeRemove is not a Vue lifecycle hook.
  • B. BeforeDispose is not a Vue lifecycle hook.
  • D. BeforeClose is not a Vue lifecycle hook.

Tip: In Vue 3 Options API, use beforeUnmount and unmounted for cleanup.

Question 7Medium

Where should you clear a timer or remove an event listener added in a component?

  1. In onUnmounted
  2. In onMounted, right after the component appears
  3. In onUpdated, after each re-render finishes
  4. In the template, using an inline expression

Answer: A. In onUnmounted

Explanation

Cleanup belongs in unmount hooks to avoid leaks.

Step-by-step solution

  1. A timer or event listener set up in the component keeps running after the component is removed.
  2. OnUnmounted runs when the component is removed from the DOM.
  3. So cleanup belongs in onUnmounted (option A).

Why the other options are wrong

  • B. OnMounted is where setup happens; cleaning up there would run too early.
  • C. OnUpdated runs after every re-render, not when the component is removed.
  • D. A template expression cannot clean up timers or listeners.

Tip: Pair each timer or listener set up in onMounted with its cleanup in onUnmounted.

Question 8Medium

Parent and child both register onMounted. Which logs first?

  1. Parent's, then child's
  2. Child's, then parent's
  3. Order is random
  4. Only the parent's

Answer: B. Child's, then parent's

Explanation

A parent is mounted only after all its children are mounted.

Step-by-step solution

  1. Child tree mounts.
  2. Parent hook runs last.

Why the other options are wrong

  • A. Parent mounts after children.
  • C. Order is deterministic.
  • D. Both run.

Question 9Hard

Which hooks are specific to components wrapped in <KeepAlive>?

  1. onMounted and onUnmounted, as for normal components
  2. onBeforeUpdate and onUpdated, on each re-render
  3. onCreated and onDestroyed, as in the older API
  4. onActivated and onDeactivated

Answer: D. onActivated and onDeactivated

Explanation

Cached components trigger activated/deactivated instead of mount/unmount when switching.

Step-by-step solution

  1. A component wrapped in KeepAlive is cached instead of destroyed when switched away.
  2. Vue calls onDeactivated when it is hidden and onActivated when it is shown again.
  3. So these hooks are specific to KeepAlive components (option D).

Why the other options are wrong

  • A. Mount and unmount are not called on each switch for cached components.
  • B. Update hooks run on any component, not only cached ones.
  • C. OnCreated and onDestroyed are not the Composition API hooks for this.

Tip: Use onActivated and onDeactivated to pause or resume work in KeepAlive components.

Question 10Hard

Which hook runs for a component inside <KeepAlive> when it is re-shown from cache?

  1. onMounted
  2. onBeforeMount
  3. onActivated
  4. onRestored

Answer: C. onActivated

Explanation

Cached instances are not remounted; onActivated and onDeactivated mark visibility.

Step-by-step solution

  1. First show mounts.
  2. Re-show triggers activated.

Why the other options are wrong

  • A. Not run again; the instance is cached.
  • B. Not run again.
  • D. onRestored is not a Vue API for this; the correct choice is onActivated.

Practise all 14 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