I am a software engineer, and I want to learn how it works. Is it necessary to but a kit or there can be some virtual devices available to test the my example code.
A microcontroller board is generally required. You can write & compile the code independently, but a uC is needed to run it on and interact with the world.
I find this link very helpful when looking for an Arduino simulator: http://lmgtfy.com/?q=Arduino+simulator