The correct response is:
when there is a chemical reaction between metals and nonmetals.
This is because chemical energy can be transformed into electrical energy in processes such as those occurring in batteries or fuel cells, where reactions between metals and nonmetals release energy in the form of electricity.