A gerund is a verbal used as a noun. It ends in -ing. ur gerund phrase in this sentence is " by preparing "....and it is functioning as the object of the preposition.
In the prepositional phrase above ,<em>preparing</em> -in italics- is the gerund. A preposition is always followed by a noun , so if the idea comes from a verb , this verb has to be nominalised. A verb is nominalised through its gerundial form. <em>By preparing</em> can be substituded <em>by this way // by this means</em> ,<em> Justin was able to</em>.... Notice that the gerund has been replaced by a noun phrase, and if we ask: "How was Justin able to pass the exam ...? "; the answer will be: "By preparing...." . <em>Preparing </em>explains the means Justin used; he prepared himself ,probably hard. All this verbal structure can be nominalised through a gerund. The gerung is functioning as a complement to the preposition.