What is the formula for magnesium iodide?
Magnesium metal forms a doubly charged cation. Iodide forms a singly charged anion.
How do you know what ions these elements form? Look at the Periodic Table. Magnesium comes from Group 2; as a metal it needs to lose 2 electrons to assume a Noble Gas configuration (that of neon); on the other hand iodine needs to gain 1 electron to assume the xenon configuration. The ion formation reflects this tendency.
What is the formula of potassium oxide?