Asking it as a "trick", where you pretend it is a real question and you don't know the answer, could certainly be annoying depending on the professor's mood. So don't try to be tricky.
Hi Professor, if you have a few minutes, I came across an interesting puzzle that I thought you might appreciate. I thought the answer was pretty surprising. [if invited to continue:] Look at this program; can you guess what it would output?
You should make it clear that this is a "riddle" to which you already know the answer. If the professor isn't interested in spending time thinking about it, he can always ask you to tell him the answer, or just say he is too busy right now.
Also, you know your professor better than we do; use your own judgment as to whether he is someone that would find this sort of thing interesting.
Some commenters have raised the question of whether this is an appropriate use of office hours at all. My view is that it is, since it is at least somewhat related to the content of the course (same programming language). It also has the potential to lead to a technical discussion in which you might learn something. I personally would be happy to have this kind of conversation with a student during my office hours.
Of course, it should be low priority. If there are other students waiting with questions that are directly about the course (homework, lecture, etc), then this is not a good time to ask your puzzle; go away and come back another time.
I am assuming that your honest goal in all this is simply to share something that you think the professor will find technically interesting, and/or to learn more about it yourself. If your intention is actually to "test" the professor, or to impress him with your obscure technical knowledge, or make him think you are smart so that you get a better grade, then please don't; that would be rude and disrespectful, and professors tend to be pretty good at seeing through things like that.