What type and size of screw should I use to mount a 4”x4” metal electrical box to the back of the under the sink cabinet?
For box mounting I use 2 #8 pan/ washer head screws. All the ones I have seen are Phillips or square drive #2, but there may be straight. Inspectors like the square head #2 because usually only electricians use these so it can help you pass inspection. At least 3/4” but 1” is the max I normally buy because the next size is usually 1.5” and that’s too long, especially if no drywall (don’t want to drive a screw through a cable).
Driving that screw is a pretty simple task and you could make due with anything close to suitable - something in the size 8-10 range, at least 1/2" long but not so long it goes through the cabinetry into who knows what, hopefully threads coarse enough to hold well in manufactured wood-like substrates. Ideally a washer head but a pan head will do, a tapered flat head won't sit right and will leave a little sharp bit exposed.
Working inside a box, inside a cabinet, half lying on the floor, is pretty awkward. A slotted screw will be a real pain, a phillips screw will be better but could still be difficult. A screw that stays on the driver well will help a lot. You could use a hex-washer head screw, and a nutdriver with a strong magnet. My preference is a square drive screw, they stay on very well for me. Of course, using a power tool rather than a hand driver will also make things easier.