The correct response would be:
when there is a chemical reaction between metals and nonmetals.
This is because chemical reactions, especially those involving metals and nonmetals, can lead to the flow of electrons, which can be harnessed to produce electrical energy. An example of this is in galvanic cells or batteries, where chemical energy is converted to electrical energy during the reaction.