The economy appears to be expanding at a 3.1 percent annual rate in the second quarter ending this month, after showing virtually no growth from January through March, the government reported yesterday.

The apparent acceleration may not be strong enough, however, to prevent the federal budget deficit from rising beyond administration estimates, economists said.

The Commerce Department said its "flash" estimate of gross national product in the current quarter -- based on data for one or two months -- shows the economy growing at an annual rate of 3.1 percent, to $3.9 trillion. At the same time, the department revised downward its earlier report of first-quarter results to show GNP growth of 0.3 percent rather than 0.7 percent.

In a more positive vein, the Labor Department reported yesterday that prices remained stable in May, and analysts see little likelihood of an inflationary surge soon. The department said the consumer price index rose 0.2 percent last month as food and transportation costs declined. Gasoline price rises were small, and the costs of new and used cars declined, the department said.

Reagan administration officials had been awaiting the flash estimate of the gross national product for a sign of how much their estimates of economic growth -- and hence, federal budget deficits -- may have to be revised.

Government economists said yesterday that if the flash estimate for the second quarter holds up, growth will have to accelerate to a 6 percent annual rate in the third and fourth quarters to reach the administration's 4 percent growth target for the year.

If the economy grows at about a 3 percent pace for the year, as many private forecasters have suggested, the federal budget deficit could swell by about $70 billion by 1988, according to administration calculations.

White House spokesman Larry Speakes said the GNP estimate and consumer price index "point to a renewal period of stable growth with low inflation" and that the growth figures "indicate we're headed toward a solid second-quarter performance."

"We feel that with the drop in the prime rate to 9.5 percent, along with the rise in the nation's money supply and a slowdown in inventory accumulation, GNP growth can continue at a healthy pace," Speakes said.

Commerce Secretary Malcolm Baldrige said that "the worst of the slowdown probably is behind us, and we should be back on a higher growth path by summer's end."

But he added that "general weakness was still evident in manufacturing, mining and agriculture -- sectors hard hit by foreign competition."

Economists were divided on whether the 3.1 percent flash estimate suggested that a significant rebound was under way, or simply that the economy was continuing its recent pattern of speeding up in one quarter and slowing down in the next.

The economy grew 1.6 percent in the third quarter last year, which was followed by a more robust 4.3 percent expansion in the fourth quarter and the flat 0.3 percent performance in the first quarter this year.

Economists said the massive influx of imports will continue to hinder domestic production and will place pressure on corporate profits, which declined 2.9 percent after taxes in the first quarter.

A major reason for the improvement in output in the second quarter was that the trade picture did not deteriorate as badly as it had in the first quarter.

"The good news is that the bad news wasn't as bad as it could have been," said Roger Brinner, chief economist for Data Resources Inc., referring to the trade performance. "I really don't think we're in for a conventional recession. But I also can't see a bloom in the second half."

The economy "is following the same uneven pattern as in the third quarter last year, and it is likely to continue in the second half this year," said David Jones, chief economist for Aubrey G. Lanston financial analysts. "We get a quarter of weakness, and then it's followed by a quarter of somewhat stronger growth. The point here is the villain in the economy still is the deepening trade deficit. There's no indication that's coming to a halt," he said, particularly with the Japanese shipping in more automobiles than under the old voluntary restraint agreement.

The merchandise trade deficit this year is expected to exceed last year's record of $123 billion. For the first time since 1914, the United States appears to have become a net debtor to the rest of the world, a distinction usually associated with impoverished Third World nations that owe more to foreigners than foreigners owe to them.

The flash estimate indicates that real final sales, which declined in the first quarter, are increasing in the second, and that inventory investment by businesses, which rose modestly in the first quarter, is expected to decline in the second.

Consumer spending and business fixed investment are expected to increase substantially in the current quarter, while residential investment and government purchases are rising modestly, Commerce said.

Many economists point toward about eight months of declining interest rates as a reason for expecting growth to speed up in the second half of the year.

Domestic automobile assemblies are expected to provide a large boost to the economy next month, "after the second quarter cutback which retarded GNP growth," Baldrige said.

Inflation has remained stable at about 4 percent or less since the economic recovery began. Rapidly rising inflation has traditionally been a precursor to recessions.

The 0.2 percent rise in the consumer price index in May followed increases of 0.5 and 0.4 percent, respectively, in March and April. Consumer prices have increased 3.7 percent in the last 12 months.

The index stood at 321.3 at the end of May, which means that goods costing $10 in 1967 now cost $32.13. Another consumer price index, used for indexing Social Security and some other federal payments, was 317.8 last month.

Grocery store food prices declined for the third consecutive month, dropping 0.4 percent. The indexes for meats, poultry, fish, eggs and fruits and vegetables declined. Beef prices, which dropped 2.6 percent in May, were 5.9 percent below their levels at the end of last year, Labor said.

Gasoline prices rose 0.5 percent, but at a slower pace than in April and March. Gasoline prices rose 7.4 percent in the last three months but still are 11.2 percent below their peak level of March 1981.

Renters' and homeowners' costs rose 1.0 percent although fuel oil prices declined 0.5 percent and charges for gas and electricity rose 0.5 and 0.1 percents.

The GNP implicit price deflator, which measures changes in prices and the composition of goods and services, is expected to increase 3.2 percent in the second quarter compared with 5.4 percent in the first, Commerce said. Another inflation measure, the GNP fixed-weighted price index, which reflects only changes in prices, is rising 3.9 percent in the second quarter compared with 4.3 percent in the first three months.