Assume a malicious code exists in a contract (either written intentionally or not). Is there any mechanism to prevent such codes from being executed? Is it even possible to do the monitoring?
From the context, I assume you're concerned about malicious code affecting the machine it runs on. This isn't an issue*, because EVM code runs in a very restrictive interpreter sandbox. Code has to be written using EVM instructions, which are limited to affecting the state of the virtual machine itself, and reading and writing blockchain state; by design, there's no way to access or influence the state of the machine it runs on. This is necessary both for security, and to ensure consensus.
** Barring vulnerabilities in the EVM itself, which are unlikely with common implementations, but perhaps more plausible with a JIT-based implementation.