Detecting Ultrasonic sensor malfunction within the arduino sketch
First of all, this question is hard to answer because you don't provided a name of ultrasonic sensor that you are using. Also as @Majenko pointed out, the key here is to determine the state that you think sensor is not working correctly.

But if assume that you're using standard trig/echo ultrasonic sensor (HC-SR04 or similar) interface - the only possible solution here is to monitor the output(echo) signal of the sensor. Broken sensor will not respond on trigger pulses, so you'll not get the echo pulses. However, these sensors also do not send a signal if the object is out of range of the sensor.

Thus, it is possible to determine the breakdown by monitoring the output signal from the sensor only if you know that the object will always be in range. Otherwise - you can not determine for certain because of what you did not receive a signal, whether it is a sensor failure, or the object is too far from the sensor.