I really don’t know how things work in the Federation Starfleet, but I do have some personal experience of how things work in the United States Coast Guard. I submit that the US Navy shares some of these methods but I can’t say for certain.
During the summer between their 3rd and 4th years, midshipmen from the CG Academy are placed on TDY (Temporary Duty) to Coast Guard Cutters where they serve as crewmembers. Enlisted crew members. Prior to the middies arriving, there’s a all hands briefing where the line is drawn, so to speak. The middies are to be treated as any newly reportid Seaman/Fireman Apprentice fresh from Recruit training. Which means if there’s something dirty that needs to be cleaned, heavy that has to be moved, or has to be dealt with in the bilges, you find a middie to do it. Or at least assist. And keep in mind that next year they’ll be Commissioned Officers.* Unlike the situation the Commandant of the Mobile Infantry Officer’s School related to Cadet Juan Rico and his classmates in Heinlein’s “Star Troopers”, the CGA midshipmen are NOT given temporary commissions. They are at the absolute bottom of the chain of command.
If, by some huge mischance the cutter lost it’s officers, the senior warrant officer would take temporary command of the cutter. And if the warrants were also gone, the senior CPO would take command. There is no way a midshipman could get command.
But this was real life. I can’t see how it would relate to Starfleet. Just sayin’.
*I had a friend who was an EM3 (Electrician’s Mate 3rd Class). During a Midshipman Cruise, the only female middie was assigned to assist her. Since the middie had no EM type training, mostly she carried stuff and passed the proper tools to my friend. At one point, my friend had to do something which involved going into the bilges. So of course, she tolod the middie to go in there to do what was necessary. The middie didn’t want to go into the bilges (I really can’t blame her, nobody wants to do that) and whined about it. So my friend ordered her in there. Well, the task got completed and eventually the cruise was over and middie went back to the CGA and next spring graduated and received her commission. And orders back to the ship my friend was on as the Assitant Engineering Officer. And when she spotted my friend, she thought she’d get some revenge.
Once my friend recognized what was going on, she had a talk with the Engineer (who was a pretty nice and funny** guy) who promptly clamped down hard on the Ensign and her plan of revenge.
** When the Engineer (the officer responsible for, among other things, the watertight integrity of the ship) comes up on deck, puts on a Mae West, climbs into a lifeboat, and spends a couple hours reading a book, some people get nervous. 