The joke is a pun.  An apiary is a place where bees are kept; a collection of beehives. 'Bee' sounds the same as the letter 'B' which is used to mark a grade one might receive in a class.
I was studying in an apiary class . This resulted in me receiving a bee on my exam .