Bird
Raised Fist0
Vueframework~20 mins

Options API vs Composition API decision in Vue - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue API Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the main difference between Options API and Composition API

Which statement best describes the key difference between Vue's Options API and Composition API?

AOptions API is only for Vue 2, and Composition API is only for Vue 3, so they cannot be used together.
BOptions API organizes code by options like data, methods, and computed, while Composition API organizes code by logical features using functions.
COptions API requires class components, while Composition API only supports functional components.
DOptions API uses reactive refs by default, while Composition API does not support reactivity.
Attempts:
2 left
💡 Hint

Think about how code is grouped and reused in each API style.

❓ component_behavior
intermediate
2:00remaining
Behavior difference in reactive state declaration

Consider these two Vue components. What is the main difference in how their reactive state is declared and accessed?

Component A (Options API):
export default {
  data() {
    return { count: 0 };
  },
  methods: {
    increment() {
      this.count++;
    }
  }
}

Component B (Composition API):
import { ref } from 'vue';
export default {
  setup() {
    const count = ref(0);
    function increment() {
      count.value++;
    }
    return { count, increment };
  }
}
AComponent A uses a plain property accessed with 'this.count', Component B uses a ref accessed with 'count.value'.
BComponent A uses refs accessed with 'count.value', Component B uses plain properties accessed with 'this.count'.
CBoth components use refs but Component A returns them from data(), Component B returns them from setup().
DComponent A cannot update count reactively, Component B updates count reactively.
Attempts:
2 left
💡 Hint

Think about how reactive variables are accessed differently in Options API vs Composition API.

📝 Syntax
advanced
2:00remaining
Identify the correct Composition API setup function syntax

Which option shows the correct syntax for a Vue 3 Composition API setup function that returns a reactive variable and a method?

A
setup() {
  const message = ref('Hello');
  function update() {
    message.value = 'Hi';
  }
  return { message, update };
}
B
setup() {
  let message = 'Hello';
  function update() {
    message = 'Hi';
  }
  return { message, update };
}
C
setup() {
  const message = reactive('Hello');
  function update() {
    message = 'Hi';
  }
  return { message, update };
}
D
setup() {
  const message = ref('Hello');
  function update() {
    message = 'Hi';
  }
  return { message, update };
}
Attempts:
2 left
💡 Hint

Remember how to update a ref's value inside a function.

🔧 Debug
advanced
2:00remaining
Debugging reactivity loss in Options API vs Composition API

Why does this Composition API code fail to update the displayed count, while the Options API version works?

Composition API snippet:
setup() {
  let count = 0;
  function increment() {
    count++;
  }
  return { count, increment };
}
ABecause <code>count</code> should be declared inside <code>data()</code> instead of <code>setup()</code>.
BBecause <code>increment</code> is not declared as an arrow function, so it loses context.
CBecause <code>count</code> is a plain variable, not a ref, so Vue cannot track changes to it.
DBecause the <code>return</code> statement is missing parentheses around the object.
Attempts:
2 left
💡 Hint

Think about what makes a variable reactive in Composition API.

❓ state_output
expert
2:00remaining
Predict the rendered output after state changes in mixed API usage

Given this Vue 3 component mixing Options API and Composition API, what will be the final rendered count after calling incrementBoth() once?

export default {
  data() {
    return { countOptions: 0 };
  },
  setup() {
    const countComposition = ref(0);
    function incrementBoth() {
      this.countOptions++;
      countComposition.value++;
    }
    return { countComposition, incrementBoth };
  }
}

Template displays: {{ countOptions }} - {{ countComposition }}

A0 - 0
B1 - 1
C1 - 0
D0 - 1
Attempts:
2 left
💡 Hint

Consider the context of this inside incrementBoth in Composition API.

Practice

(1/5)
1. Which Vue API organizes component code by options like data, methods, and computed?
easy
A. Options API
B. Composition API
C. Both APIs organize code this way
D. Neither API uses options

Solution

  1. Step 1: Understand Options API structure

    The Options API groups code by options such as data, methods, and computed.
  2. Step 2: Compare with Composition API

    The Composition API organizes code inside the setup() function by logical features, not by options.
  3. Final Answer:

    Options API -> Option A
  4. Quick Check:

    Options API groups by options like data and methods [OK]
Hint: Options API groups by options, Composition API uses setup() [OK]
Common Mistakes:
  • Confusing Composition API with Options API structure
  • Thinking both APIs organize code the same way
  • Believing Options API uses setup() function
2. Which of the following is the correct way to declare a reactive state variable using the Composition API in Vue?
easy
A. const count = ref(0); inside setup()
B. data() { return { count: 0 } }
C. methods: { count() { return 0 } }
D. computed: { count: () => 0 }

Solution

  1. Step 1: Identify Composition API reactive declaration

    In Composition API, reactive state uses ref() inside setup(), e.g., const count = ref(0);.
  2. Step 2: Compare other options

    data() { return { count: 0 } } is Options API syntax, A and D are for methods and computed properties, not reactive refs.
  3. Final Answer:

    const count = ref(0); inside setup() -> Option A
  4. Quick Check:

    Use ref() in setup() for reactive state [OK]
Hint: Use ref() inside setup() for reactive state in Composition API [OK]
Common Mistakes:
  • Using data() syntax in Composition API
  • Confusing methods with reactive state
  • Trying to declare reactive state outside setup()
3. Given this Vue component using Composition API:
import { ref } from 'vue';
export default {
  setup() {
    const count = ref(0);
    function increment() {
      count.value++;
    }
    return { count, increment };
  }
}

What will be the value of count after calling increment() twice?
medium
A. 0
B. 1
C. 2
D. undefined

Solution

  1. Step 1: Understand ref and increment function

    count is a ref initialized to 0. Calling increment() increases count.value by 1 each time.
  2. Step 2: Calculate count after two increments

    Starting at 0, after two increments: 0 + 1 + 1 = 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Two increments add 2 to count.value [OK]
Hint: ref.value changes with increments; count.value++ twice = 2 [OK]
Common Mistakes:
  • Forgetting to use .value with ref
  • Assuming count is a plain number, not a ref
  • Thinking increment does not change count
4. Identify the error in this Vue component using Options API:
export default {
  data() {
    count: 0
  },
  methods: {
    increment() {
      this.count++;
    }
  }
}
medium
A. Incorrect method name increment
B. data() should be a property, not a function
C. Using this.count instead of count
D. Missing return statement in data()

Solution

  1. Step 1: Check data() function syntax

    In Options API, data() must return an object. Here, it lacks return keyword.
  2. Step 2: Verify other parts

    Method name and usage of this.count are correct. data() is correctly a function.
  3. Final Answer:

    Missing return statement in data() -> Option D
  4. Quick Check:

    data() must return an object [OK]
Hint: data() must return an object with return keyword [OK]
Common Mistakes:
  • Omitting return in data()
  • Confusing this.count with count
  • Thinking data() can be an object directly
5. You have a large Vue project with many components sharing logic like fetching data and handling loading states. Which API choice is best and why?
hard
A. Options API, because it separates code by options making it easier to read
B. Composition API, because it groups related logic together for better reuse
C. Options API, because it requires less code and is more modern
D. Composition API, because it avoids using setup() and is simpler

Solution

  1. Step 1: Consider project size and logic reuse

    Large projects with shared logic benefit from grouping related code together for reuse and clarity.
  2. Step 2: Compare API strengths

    Composition API allows grouping logic inside setup(), making reuse easier. Options API separates by options, which can scatter related logic.
  3. Final Answer:

    Composition API, because it groups related logic together for better reuse -> Option B
  4. Quick Check:

    Composition API groups logic for reuse in large projects [OK]
Hint: Use Composition API for reusable logic in big projects [OK]
Common Mistakes:
  • Choosing Options API for better reuse in large projects
  • Thinking Composition API avoids setup()
  • Believing Options API is more modern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard