Dwarfs riding minecarts will not leave the cart unless
- the cart stops
- they are thrown out by a collision
- (example from devblog) they dodge outside of the cart
There might be other conditions, but I'm not aware of them - dwarfs can starve/dehydrate while in a cart, they won't jump off.
Riding dwarfs will have the job 'push vehicle' and will abort it when disturbed by nearby unfriendly units. This does nothing to the cart itself but can cause the dwarf to pick up jobs in the fortress which they cannot actually perform as long as they haven't left the cart.
Dwarfs can interact with hostiles/wild animals when passing close enough to them. I've had civilian dwarfs punch and shoot horned owl people that got too close to the track (in the first case, the HOP was directly adjacent to the cart, of course). The dwarfs did not leave the cart in those examples.
It should be possible to use ridden minecarts as archer patrol - set up the carts so only the archer squad will push/ride them, activate them once inside the carts to prevent them from messing up fort jobs. Make sure you can let them back out when they get too hungry/thirsty/tired/wounded or run out of ammo.
Have you tested launching carts (ridden or not) already? Getting them to land safely (for the dwarf) isn't easy, and you need a complete acceleration ramp/launch ramp setup to get a single flight trajectory. It's a lot of work giving you access to a tiny part of your embark's airspace. If you want archers raining death from above, an elevated track will be much safer, cover more space and cost less effort.
To attack in melee from a minecart, a dwarf would have to pass directly next to the opponent. And the opponent would presumably be able to strike back or even strike first. At highest roller speed, passing by a stationary opponent, the cart would only spend six game steps within reach; a normal-speed militiadwarf would have about 60% chance of actually attacking in this time. The chance of an attack would be a lot smaller when you used impulse ramps to accelerate the cart even more. Using very heavy carts and letting them run through the opponent directly would more reliably attack them, and you wouldn't need a dwarf to ride such a cart.
For most of your questions i'd suggest you try it out and see what happens. That's what dwarven science is all about.