Is there a way?
No. It is technically not possible. Many / most source code embedded in Answers is actually code fragments, and not compilable in its original form. (For example, most Java answers don't have imports, class declarations, etc.) If code can't be compiled, it is not possible to identify out-of-date usage reliably and automatically.
Even if it was possible, it would not be advisable ... to automatically remove or replace such code. A lot of people use old versions of programming languages, libraries, etc. It is not a good idea, but it is often necessary. If we (say) ripped out all example code for Java 7 and replaced it with Java 8 code, then a lot of people would not be able to find answers that help them.
In theory, if we could tag answers with a programming language and version, then it would be possible for a user to filter out solutions with "out of date" code. But getting to that state from where StackOverflow currently is would be a lot of work. And most of the work will be people trawling through the millions of existing answers and tagging them. Carefully.
I think a better approach is to educate users to not expect that they can copy-and-paste solutions without thinking. That's not StackOverflow's real purpose ... according to my understanding.