Composition API Questions with Answers

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

Sample questions

Question 1Easy

What does <script setup> expose to the template automatically?

  1. Only returned values, exactly like a setup function that must return an object
  2. Nothing
  3. Only props
  4. Top-level variables, functions and imports

Answer: D. Top-level variables, functions and imports

Explanation

The compiler makes top-level bindings available without a return statement.

Step-by-step solution

  1. Declare a const.
  2. Use it in the template.

Why the other options are wrong

  • A. No return needed.
  • B. The template can use them.
  • C. Not only props.

Question 2Easy

What is the second argument of inject('key', 'fallback')?

  1. A type check that makes Vue warn when the provided value has a different type
  2. A required flag
  3. A scope name
  4. A default value used when nothing is provided

Answer: D. A default value used when nothing is provided

Explanation

Without a provider and a default, Vue warns that injection was not found.

Step-by-step solution

  1. Look up key.
  2. Use default if missing.

Why the other options are wrong

  • A. The second argument is a fallback value; Vue does no type check with it.
  • B. The second argument is a default value, not a required flag.
  • C. The second argument is a fallback value, not a scope name.

Question 3Easy

What does <script setup> provide?

  1. A way to write only Options API components
  2. A server-only script that never runs in the browser
  3. A global plugin registry for third-party libraries
  4. Compile-time sugar for the Composition API in SFCs

Answer: D. Compile-time sugar for the Composition API in SFCs

Explanation

Top-level bindings in <script setup> are exposed to the template.

Step-by-step solution

  1. Script setup is a compile-time shorthand for the Composition API.
  2. Top-level bindings in it are exposed to the template automatically.
  3. So it gives compile-time sugar for the Composition API in single-file components (option D).

Why the other options are wrong

  • A. It is used for Composition API code, not only Options API.
  • B. It runs in the browser like any component script; it is not server-only.
  • C. It does not register plugins; that is done with app.use.

Tip: Use script setup for new single-file components.

Question 4Medium

How do you get a template ref to an element with ref="box" in <script setup>?

  1. document.getElementById('box')
  2. this.$refs only
  3. const box = ref(null) — same name as the template ref
  4. useRef('box')

Answer: C. const box = ref(null) — same name as the template ref

Explanation

A ref variable with the same name is bound automatically and filled after mount.

Step-by-step solution

  1. Declare ref(null).
  2. Use name in template.
  3. Read it in onMounted.

Why the other options are wrong

  • A. Works but bypasses Vue.
  • B. Options API style.
  • D. React API.

Question 5Medium

What is a composable in Vue?

  1. A component that has a script and style but no template
  2. A custom directive registered on the app instance
  3. A function that reuses stateful logic via Composition API
  4. A module in the older Vuex store

Answer: C. A function that reuses stateful logic via Composition API

Explanation

Composables, by convention named useXxx, return refs and functions for reuse.

Step-by-step solution

  1. A composable is a function that holds reusable stateful logic.
  2. It uses the Composition API, such as refs and computed values.
  3. By convention it is named useSomething and returns refs and functions (option C).

Why the other options are wrong

  • A. A component with a script and style but no template is not a composable.
  • B. A custom directive is a separate feature, not a composable.
  • D. A Vuex module is a store module, not a composable.

Tip: Name composables with a use prefix and return refs and functions from them.

Question 6Medium

What is a composable, by convention?

  1. A function using Composition API that encapsulates reusable stateful logic, named useXxx
  2. A mixin object with data and methods that is merged into each component that uses it
  3. A global plugin installed with app.use
  4. A component compiled to a class

Answer: A. A function using Composition API that encapsulates reusable stateful logic, named useXxx

Explanation

Composables like useMouse() return refs and functions that components can share.

Step-by-step solution

  1. Write a function.
  2. Use refs inside.
  3. Return state and methods.

Why the other options are wrong

  • B. Mixins are the Options API approach.
  • C. Plugins are different.
  • D. Components are not required.

Question 7Medium

What does defineProps<{ title: string }>() demonstrate?

  1. Type-based props declaration in <script setup>
  2. Runtime-only validation with no type information
  3. A global component registration with TypeScript
  4. Typing the events a component emits

Answer: A. Type-based props declaration in <script setup>

Explanation

The generic argument declares props types and the compiler derives runtime options.

Step-by-step solution

  1. DefineProps with a type argument declares the props in TypeScript types.
  2. The compiler derives the runtime prop options from that type.
  3. So it demonstrates type-based props declaration in script setup (option A).

Why the other options are wrong

  • B. The type argument provides type information, so it is not runtime-only.
  • C. Global component registration is a separate concern.
  • D. Events are typed with defineEmits, not defineProps.

Tip: Use type-based defineProps for typed props without writing runtime options by hand.

Question 8Medium

What does toRef(state, 'count') return?

  1. A ref linked to the property of the reactive object
  2. A copy of the number at that moment, which no longer follows later changes
  3. A computed
  4. A new reactive object

Answer: A. A ref linked to the property of the reactive object

Explanation

Writing to the ref updates state.count and vice versa.

Step-by-step solution

  1. Pick one property.
  2. Get a linked ref.

Why the other options are wrong

  • B. It stays synced.
  • C. It is a ref.
  • D. It is a ref.

Question 9Hard

What is the effect of toRaw(proxy)?

  1. Creates a deep clone of the reactive object
  2. Makes the plain object reactive again
  3. Freezes the object so it can never change
  4. Returns the original object; mutating it triggers no updates

Answer: D. Returns the original object; mutating it triggers no updates

Explanation

toRaw returns the original object; mutating it does not trigger updates.

Step-by-step solution

  1. ToRaw takes a reactive proxy and returns the original object underneath.
  2. Changes made to that original object do not go through the proxy.
  3. So mutating it triggers no updates (option D).

Why the other options are wrong

  • A. ToRaw returns the same object, not a deep clone.
  • B. ToRaw removes reactivity; it does not add it.
  • C. ToRaw does not freeze the object.

Tip: Use toRaw only for reads or for passing a plain object where reactivity is not needed.

Question 10Hard

Why is calling a composable inside an if or after await in setup risky?

  1. Composables cannot use if statements because hooks must always run unconditionally
  2. Lifecycle hooks and watchers need an active component instance, which is lost after await
  3. It makes the app slower
  4. Vue forbids async

Answer: B. Lifecycle hooks and watchers need an active component instance, which is lost after await

Explanation

Instance context is tracked synchronously; <script setup> handles top-level await specially, but nested composables do not.

Step-by-step solution

  1. Context set during setup.
  2. After await it is gone.

Why the other options are wrong

  • A. Conditions are allowed if sync.
  • C. Not about speed.
  • D. Async functions are allowed.

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