Press and hold the mouse button while restarting the computer. Even if the computer cannot find a valid system folder (what the flashing question mark is telling you) the disk should eject during the early stages of the restart.
If that does not work, please tell us the exact iMac sub-model you have. This forum covers those made between 1998 and 2005 and covers three broad iMac families with different troubleshooting needs. If yo uare no sure of the sub-model, use this Apple site to find the serial number on teh outside of the computer:
How to find the serial number on your iMac
then enter it into this Apple lookuip page:
https://selfsolve.apple.com/agreementWarrantyDynamic.do
If your Mac is old, that Apple lookup may not work. If so, you can get the same information from this third-party site:
http://www.everymac.com/ultimate-mac-lookup/