See explanation below.
Work Step by Step
In the electrolysis of a molten mixture of KI and MgF2, either iodide or fluoride ions can be oxidized at the anode. I is less electronegative than Fluorine. So at anode Iodine gas is produced. Either potassium or magnesium is formed at cathode. Magnesium has higher ionization energy than potassium. Hence at cathode magnesium is formed.