When you heat an atom, some of its electrons are "excited* to higher energy levels. When an electron drops from one level to a lower energy level, it emits a quantum of energy. ... The different mix of energy differences for each atom produces different colours. Each metal gives a characteristic flame emission spectrum.
The answer is 2 pairs. The carbon atom has 4 electrons and oxygen atom has 6 electrons. The purpose of shared electron is to get 8 electrons at the outer layer. So the carbon atom needs to share 4 electrons which is 2 pairs with oxygen to be stable.