This is an example of text classification problem, that is generally solved by generating some features and applying machine-learning classification algorithm to them.
For your particular case, question detection is well studied area. One of simplest possible approaches is heuristic one using regular expressions
Following solution is taken from this paper:
A sentence is detected as a question if it fulfills any of the
following: • It ends with a question mark, and is not a URL. • It
contains a phrase that begins with words that fit an interrogative
question pattern. This is a generalization of 5W-1H question words.
For example, the second phrase of “When you are free, can you give me
a call” is a strong indicator that the sentence is a question. • It
fits the pattern of common questions that are not in the interrogative
form. For instance, “Let me know when you will be free” is one such
question.
A more complex solutions are also described and you can find them is mentioned paper of googling "question detection algorithm"
For code snippet detection there are existing solutions that detect programming language, as mentioned in the comments. One example is http://www.rubyinside.com/sourceclassifier-identifying-programming-languages-quickly-1431.html
They probably can be adapted to detect if the specific sample is code or not. Or you can train simple Naive Bayes classifier using one of existing libraries