You could attach Javascript Blur event to the page body, and use it to send a cancel to the server.
That way, when ever the page looses the focus, you can cancel the exam.
One way to do this is like this:
<body onblur="javascript:window.opener='x';window.close();">
Which works in Chrome to close the window without prompting.