In the two identically formed sentences below, the word 'which' produces ambiguous interpretations.
"I stayed in a hotel, which was really nice."
I would say that 'which' refers to the hotel being nice, not the stay.
"I failed the exam, which was really surprising."
Here, 'which' refers to the surprise of failing, rather than the exam.
Why and how does the word 'which' refer to different components of the above sentences? And is there any grammatical term or reasoning for the above usage?
Thanks in advance