Consider this a cute and entertaining story.. 
Today, I became horrified, when my late 2012 iMac shut down ABRUPTLY. Black screen from one second to the next. At the time I was watching a TV show in fullscreen, so I figured it was one of those commercial breaks. I just stupidly sat there for 10-15 seconds, expecting the show to continue..
That's one of the few problems with Mac - they are so silent, you can barely tell the difference between ON and OFF, if the screen is black.. 
Anyway..
I then turned it on again, and it booted up just fine. I checked the Console logs - nothing. I then spent some time googling the problem. After that, I planned to to a hardware test (hold D during boot), then resetting PRAM and SMC. I started the hardware test, and then AGAIN it abruptly shut down. Now I was really worried.. But again it was able to turn on, and boot just fine. I googled some more, without finding anything else. I then decided to repeat the hardware test. This time it completed the test, and reported no errors. This would indicate a periodic problem. Greeat..
When the dust settled (fan test loosened some dust..
), I then reset the PRAM (hold CMD, ALT, P and R during boot). That went fine. The last thing to do, was resetting SMC, which is done by cutting power to the mac for 15-20 secs. I didn't want to reach the back of my iMac, so instead I went under the table to pull out the power plug.
Then it happened..
I barely touched the plug, when it popped out of the extension lead socket!! So.. The plug had loosened itself more and more over time, and was now barely hanging on. I waited 20 seconds, and plugged it in.
"Problem" solved!
THE END.
Today, I became horrified, when my late 2012 iMac shut down ABRUPTLY. Black screen from one second to the next. At the time I was watching a TV show in fullscreen, so I figured it was one of those commercial breaks. I just stupidly sat there for 10-15 seconds, expecting the show to continue..
Anyway..
I then turned it on again, and it booted up just fine. I checked the Console logs - nothing. I then spent some time googling the problem. After that, I planned to to a hardware test (hold D during boot), then resetting PRAM and SMC. I started the hardware test, and then AGAIN it abruptly shut down. Now I was really worried.. But again it was able to turn on, and boot just fine. I googled some more, without finding anything else. I then decided to repeat the hardware test. This time it completed the test, and reported no errors. This would indicate a periodic problem. Greeat..
When the dust settled (fan test loosened some dust..
Then it happened..
I barely touched the plug, when it popped out of the extension lead socket!! So.. The plug had loosened itself more and more over time, and was now barely hanging on. I waited 20 seconds, and plugged it in.
"Problem" solved!
THE END.