Sample questions
Question 1Easy
In <script setup>, what is true of code in the top level?
- It runs once per application, when the app is first created and mounted
- It runs every time a component instance is created
- It runs only on mount
- 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
- Each instance executes setup.
- 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?
- It runs only after the component has been mounted
- It runs when the instance is created, before mount
- It runs only when the component updates
- 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
- In script setup, the setup code runs when the component instance is created.
- That happens before the component is mounted to the DOM.
- 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?
- onBeforeMount
- setup
- onCreated
- onMounted
Answer: D. onMounted
Explanation
onMounted runs after the component's DOM is inserted.
Step-by-step solution
- Setup runs.
- Render and insert.
- 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?
- onCreated
- onMounted
- onRender
- onReady
Answer: B. onMounted
Explanation
onMounted runs once the component's DOM is created and inserted.
Step-by-step solution
- OnMounted is a Composition API lifecycle hook.
- It runs after the component's DOM has been created and inserted.
- 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?
- No, only after later reactive updates
- Yes, it always runs right after the initial render and again on later updates
- Only with immediate
- 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
- First render fires mounted.
- 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?
- beforeRemove
- beforeDispose
- beforeUnmount
- beforeClose
Answer: C. beforeUnmount
Explanation
Vue 3 renamed beforeDestroy to beforeUnmount and destroyed to unmounted.
Step-by-step solution
- Vue 2 had beforeDestroy and destroyed hooks in the Options API.
- Vue 3 renamed beforeDestroy to beforeUnmount.
- 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?
- In
onUnmounted - In
onMounted, right after the component appears - In
onUpdated, after each re-render finishes - In the template, using an inline expression
Answer: A. In onUnmounted
Explanation
Cleanup belongs in unmount hooks to avoid leaks.
Step-by-step solution
- A timer or event listener set up in the component keeps running after the component is removed.
- OnUnmounted runs when the component is removed from the DOM.
- 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?
- Parent's, then child's
- Child's, then parent's
- Order is random
- 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
- Child tree mounts.
- 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>?
- onMounted and onUnmounted, as for normal components
- onBeforeUpdate and onUpdated, on each re-render
- onCreated and onDestroyed, as in the older API
- onActivated and onDeactivated
Answer: D. onActivated and onDeactivated
Explanation
Cached components trigger activated/deactivated instead of mount/unmount when switching.
Step-by-step solution
- A component wrapped in KeepAlive is cached instead of destroyed when switched away.
- Vue calls onDeactivated when it is hidden and onActivated when it is shown again.
- 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?
- onMounted
- onBeforeMount
- onActivated
- onRestored
Answer: C. onActivated
Explanation
Cached instances are not remounted; onActivated and onDeactivated mark visibility.
Step-by-step solution
- First show mounts.
- 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 →