A circuit court judge in Rockville yesterday upheld a Montgomery County law that gives tenants a chance to buy their apartment building before it is sold to an outsider to be developed into condominiums.

The decision by Judge David L. Cahoon also upheld the county's right to impose a moratorium on condominium conversions, a moot issue since the 120-day moratiorium imposed in July expired last month.

Both laws were challenged by the Apartment and Office Building Association, a group of landlords that plans to appeal Cahoon's decision.

The ruling, county spokesman Charles Maer said, "creates a favorable framework for the housing program for the county now."

The laws were passed in July when county officials became alarmed at the rapid rate at which rental apartments were being converted to condominiums. Officials feared that the trend was displacing moderate-and low-income families and leaving them few rental alternatives.

According to county housing figures, abut 2,559 units were converted in Montgomery County between January and July.

Cahoon found the two bills adopted by the county last summer were valid as "emergency legislation" and were passed in accordance with the county's charter.

They also did not violate state law or federal constitutional rights, he said.

The judge, however, struck down criminal penalties that had been attached to the condo conversion law.