I know that perpetual motion machines are not possible and I want to know the reason why will this machine not work. The Car B has a magnetic bulge on top of it and Car B can only move forward. It cannot move backwards as it's wheels will be jammed (hotwheel cars have this feature). The Car A has a strong permanent magnet attached to its top. This magnet will attract Car B but as Car B cannot move backwards, so the force will make Car A move towards Car B. When the front of Car A touches back of Car B, the magnet on Car A due to it's short length will still not be able to touch the magnetic bulge on top of Car B. So Car A will continue to move which will push Car B forward and hence it should keep going. Please tell me why this theory is wrong.
Your setup starts with a certain amount of potential energy: There is an attractive force between the cars, but the cars are not touching.
Car B is not free to roll toward car A (ratchet wheels), so the only motion that is possible when you release Car A is, car A rolls (accelerates) toward car B. The initial potential energy is coverted to the kinetic energy of car A.
Then they collide, inelastically, and they roll off together at some velocity that can be computed by applying the laws of conservation of momentum and conservation of kinetic energy.
And that's the end of the story. In a universe with no friction, they will keep rolling forever, with no change in momentum and, no change in kinetic energy.
In our actual practial universe, there will be friction between the wheels and the road and, in the wheel bearings. The kinetic energy will be converted to heat, the cars will slow to an eventual stop, and the heat will dissipate, increasing the entropy of the universe.
Either way, all of the energy that we're talking about is the energy that you put into the system in the first place by separating the cars from each other, working against the attractive force of the magnet.
No new energy will be added or generated after you release car A.
The only thing wrong with your thinking is the idea that perpetual motion is impossible. It's not. The proof is in the heavens (e.g., like how the Earth has been circling the Sun for billions of years.)
What is impossible is the so-called perpetual motion machine (a.k.a., over-unity device). That would be a machine that not only runs forever, but it also would produce enough new energy to compensate for energy lost to friction, and perhaps also to do some kind of useful work. Nobody has ever built one yet, and nobody who's learned their physics lessons is likely to ever invest in one.
We assume you are starting from contact between cars and zero velocity. Using Newton third law we know the magnetic force on A equals the magnetic force on B. Also, the normal force A imposes on B equals the normal force B imposes on A. Assume A is starting to move to the left. That implies the magnetic force is larger than the normal. But that in turn implies that car B is moving to the right. Contradiction!