Deal with academic failure as an undergraduate There are many students (subject being basic sciences) in my university (considered a good university in India) who simply do not care about the subject in a course but have stellar grades. They aren't interested in the subject per se, just the grades. These students never doubt anything that the professor says or whatever is written in lecture notes. They never ask questions. They would happily write in exams what they have memorised without understanding it 'deeply'. When I discuss anything with them about a particular topic which I don't understand, they simply don't care about it and tell me to not think about it 'deeply', and just memorise the stuff. But these students have stellar grades.
A lot of students, almost all that I know at my university, engage in malpractices during the exam, especially in the current online mode when there is no supervision. Some students sit in groups of 6-7 students during an online exam and discuss the questions before submitting, i.e. they solve the exam paper collectively. They often get exactly similar marks as well. Many instructors turn a blind eye towards this.
I sometimes feel jealous of the other students and get agitated at the university system as well. The 'other' students get good opportunities for research internships when they have explicitly mentioned to me that they don't care about research and are doing internships just for the sake of improving their resume.
I, on the other hand, have extremely poor grades. I've failed a 3 courses too. I spend a lot of time thinking about the stuff. I ask questions to the professors, which are received encouragingly. Some professors have explicitly mentioned that my questions are good and it shows that I have thought about the material well. I ask questions and answer questions on Stack Exchange as well, and they are received warmly as well. I am interested in pursuing research after my undergraduate studies but no good university is going to accept me as a grad student considering my poor grades.
I am not saying that my failure is entirely the fault of others. I recognise vices in myself too.
What should I do in order to improve my situation?
Allocate time specifically to prepare for your tests
This might seem like a waste of time, since you would rather spend it studying the course material more deeply. However, this is a trade-off you should be willing to make in pursuit of your long-term goal of entering into research. If 100% of your studying time is currently spent on learning the course material deeply, you may need to reduce that to 75–80%, and spend the rest of the time doing test preparation. I understand that test preparation can be extremely boring and feel completely pointless. But, as you've noticed yourself, you need to score a minimum amount of marks to stay afloat in the system. There is no need to judge yourself by your course marks, though; feel free to think of it as just an arbitrary number that you need to display in order to apply to graduate programs.
Furthermore, to the best of my knowledge, a few failing grades will not disqualify you from applying to the graduate programs of any university. I believe you have options to take the failed courses again, or at least their final exams, so make use of that. If you plan to apply to graduate programs in India, in particular, as long as your overall grades lie above the cutoff and you perform well in the interviews, you should be fine.
Finally, your attitude towards your education is commendable, but at some point the effort involved in going through the system may end up not being worth it. Several talented classmates of mine found this to be true for themselves, and chose to take other paths. You will need to choose your path, too, and possibly very soon. But try your best to fulfil at least the bare minimum requirements before deciding that this system is not for you.