Who invented exams?
Formal examinations evolved along with the notion of an educational system that granted some sort of credentials or degrees. The systems of education in antiquity did not have exams, but they did not offer degrees or any form of official credentialing, although ancient teachers' recommendation letters did affect people's chances at getting certain jobs.
The medieval universities were the first degree granting organizations, and examinations (usually conducted orally and publically) were an essential part of the system of degrees and professional qualifications. Graded examinations as we know them date to the early 19th century with the invention of "classified" or "class" honours degrees in Britain that served to distinguish the qualifications of students applying to civil service jobs.
In general, formal graded examinations are a function of schools serving a pre-professional function and providing information about relative merits of students to potential employers.