There is an event held for choosing the arms that are called the “choice-of-arms”. That is conducted a month prior to the passing out parade at IMA and OTA.

The total no of vacancies and the no of cadets passing out will be equal. The vacancies will be cut up among all the arms and services. The process is done on the base of the strength of each arm/service and a host of other factors those are complicated enough to explain.
In each arm, for example, Artillery, the vacancies will be further subdivided amongst the different Battalions. All battalions do not have vacancies every time, it depends on whether the battalion is in the field, whether the battalion is running out of officers etc.
While choosing arms, the cadets write down their preference for three choices (it’s not that simple a procedure, but that’s enough for its answer). Taking into recital a host of factors primarily merit and lady luck, the cadet may or may not get his preferred choice (generally they get).
While giving preference, one cannot choose the exact battalion ones want, just the arm or service. The battalion will be allotted to you by the powers above.Anyone having a parental claim or is in the top five in merit in the entire course will get the Battalion/arm/service of their choice.