God knew the answer to this question. He asked Cain because He wanted to give him the opportunity to confess his sin and start to do right after doing wrong. How futile it was for Cain to lie to God! It was madness for him to think God didn’t know where Abel was, or that he could actually hide his sin from God.
We prevent sin from ruling over us by allowing God to master us first. Without God as our master, we will be slaves to sin.