Rapper Snoop Dogg entered a surprise no-contest plea to gun and drug charges on Wednesday and was given a three-year suspended sentence that will spare him jail time.

The 35-year-old rap star, whose real name is Calvin Broadus, was also sentenced to five years of formal probation and ordered to perform 800 hours of community service by a Los Angeles Superior Court judge.

Broadus was expected in court for an arraignment on charges of sale or transportation of marijuana and possession of a gun by a convicted felon, which could have landed him behind bars for up to four years if convicted at a trial.

He instead entered the no contest plea, apparently as part of an agreement with prosecutors.

Broadus was arrested on Oct. 25 after police at Burbank Airport, near Los Angeles, stopped him for leaving his car too long in a passenger loading area and found a gun and marijuana in his car.

Prosecutors have said they were unable to prove that the gun found in Broadus' car that day belonged to him, but that a later search of the performer's home turned up another firearm.

He is charged with being a convicted felon in possession of a firearm because of a 1990 felony conviction for possession of drugs for sale.

In November, Broadus was charged with possession of a deadly weapon after police found him carrying a collapsible baton through security at Orange County's John Wayne Airport. That case is still pending.

In March, Stockholm police arrested Broadus for suspected narcotics use.