Yes.
Could he have NOT sinned at that time? Rememberhe was a man after Gods own heart.
He had the ability to Not Sin. He chose to Sin.
God foreknew that David would sin, so there was certainly 0% statistical chance that David would not (God cannot be wrong in His foreknowledge, of course); but David did have the choice.
So—if David had the choice, don’t our hypothetical parents who murder their children also have that choice?