Can machine learning algorithms be trained to discard nonsense? Afaik, the problem with many machine learning algorithms is that they will often label nonsense into some categories.

What measures can one take to discard nonsense results?

Eg. if you have a bot that is supposed to label stuff as either orange or an apple, how can you gurantee that it won't label a random image of noise as either of the two?

Or label a car as either of the two?

Do you have to train your neural network to have 3 categories, "apple", "orange" and "neither"?
The problem you're describing can perhaps be treated as "anomaly detection". Google the term in quotes, you'll get a ton of references. So the idea would be to first train an anomaly detector to separate the "apples, oranges, and other fruits" from non-fruits (or "nonsense" in OP terminology). Then train a classifier on the fruits.

Update. In light of additional answers -- and too long for a comment.
The problem with declaring a "non-fruit" category highlights the distinction between classification and anomaly detection. In classification, we assume 2 or more coherent classes. Apples, oranges, bananas -- these all have well-defined common characteristics. On the other hand, "non-fruit" is not a coherent category. It can only be meaningfully defined as something like "not fulfilling enough of the fruit characteristics". Anomalous objects are not a coherent class; they are by definition anything that is not sufficiently close to a "normal" object. We do not expect examples of anomalous objects to be informative for training. After all, you can just generate random pixel-images and get tons of anomalous examples for free!

This is why anomaly detection requires techniques distinct from classification.