Yes in some cases a heart attack can be detected through use of a stethoscope by using the stethoscope to listen closely to the sounds of the heart the doctor can then hear a change in the heart which could be a sound that means the heart is not working properly and that a heart attack is about to happen.
If the doctor detects any bad sounds from the heart and the heart is not sounding like it should and not working properly then the doctor can order some other tests such as an EKG to detect the heart attack.
A stethoscope cannot always detect a heart attack but it's a good way to possibly tell if you're about to have a heart attack due to being able to listen to abnormalities in the hearts sound.
The doctor knows what to listen for and how the heart should sound when healthy and working properly and if not then something is not right.