nerdexam
Oracle

1Z0-900 · Question #69

Given the following class definition with numbered lines: How do you specify to use this mock CDI bean implementation instead of the regular implementation class?

The correct answer is B. Start up the server with the optional -alternative command-line option, specifying any alternative. There appears to be an error in this question's answer key. Based on CDI (Contexts and Dependency Injection) specification, Option A is the correct answer, not B. Why A is correct: In CDI, you enable an alternative bean by annotating the alternative class with @Alternative…

Use CDI Beans

Question

Given the following class definition with numbered lines:

How do you specify to use this mock CDI bean implementation instead of the regular implementation class?

Options

  • AUse the alternatives element in the beans.xml file and specify the class name in the class element
  • BStart up the server with the optional -alternative command-line option, specifying any alternative
  • CReorder the implementation names in the beans.xml file such that the desired alternative is listed
  • DDelete the regular implementation class from the WAR.

How the community answered

(19 responses)
  • B
    84% (16)
  • C
    11% (2)
  • D
    5% (1)

Explanation

There appears to be an error in this question's answer key. Based on CDI (Contexts and Dependency Injection) specification, Option A is the correct answer, not B.

Why A is correct: In CDI, you enable an alternative bean by annotating the alternative class with @Alternative, then declaring it in beans.xml using the <alternatives><class>com.example.MockImpl</class></alternatives> element. This is the standard, portable CDI mechanism for selecting an alternative implementation at deployment time.

Why the other options are wrong:

  • B is incorrect - there is no standard -alternative command-line option in Java EE/Jakarta EE servers. This option appears to be fabricated.
  • C is incorrect - beans.xml is not order-sensitive; reordering entries has no effect on which implementation CDI selects.
  • D is technically a workaround but is destructive and defeats the purpose of the alternatives mechanism, which is designed to let both classes coexist with one selectively enabled.

Memory tip: Think of CDI alternatives as a "feature flag in XML" - you always need two things: the @Alternative annotation on the class, and the <alternatives> entry in beans.xml to activate it per deployment. If you see an answer describing a command-line server flag for CDI alternatives, that's a red flag.

Recommendation: If this is from a practice exam or study guide, the answer key likely contains a typo - treat A as correct.

Topics

#CDI alternatives#bean configuration#deployment configuration#CDI bean selection

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice