The U.S. merchandise trade deficit hit a record $33.42 billion in the second quarter, as the result of declining exports of agricultural products and manufactured goods and a strong rise in Japanese auto imports, the Commerce Department reported yesterday.

The second-quarter data surpassed the previous record of $32.51 billion in the third quarter last year and exceeded the $29.6 billion deficit in the first three months of the year, Commerce said.

The trade figures are calculated on a balance-of-payments basis, meaning that they exclude military trade of U.S. defense agencies and reflect adjustments for timing, coverage and valuation to trade data that are included in the monthly merchandise trade figures, Commerce said.

The major industrial nations have fared well this year, with their exports to the United States rising throughout the first half of the year. Debt-strapped Latin American nations, however, sold fewer goods to the United States during the first half of the year.

Relaxation of import restraints on Japanese automobiles in April contributed to the sharp rise in sales here of Japanese cars, which helped push that country's surplus with the United States to record levels. Since the first quarter, new-car imports from Japan rose 25 percent, from $3.123 billion in the first quarter to $3.896 billion in the second quarter.

Japan announced this spring that it planned to increase its new-car shipments here by 25 percent.

Congress has been clamoring for more import restrictions to help stem the influx of foreign goods. The fight over protectionism is expected to heat up in the fall.

Commerce Secretary Malcolm Baldrige yesterday renewed the Reagan administration's call for Congress to pass a tough deficit-reduction package to help lower interest rates -- leading to a lower-valued dollar and improved trade balance -- rather than approving protectionist legislation.

"Recent declines in the value of the dollar will help improve the trade balance only in the longer term because buying patterns respond slowly to shifts in exchange rates," Baldrige said. "Reduced federal budget deficits could help to bring down the dollar further and increase the prospects for growth in export and import-competing industries. The budget resolution recently passed by the Congress is only a first step, and further cuts in spending must be enacted to bring the deficit under control."

Senate Majority Leader Robert J. Dole (R-Kan.), in a speech yesterday, ruled out import surcharges as a way to correct the trade deficit problem, saying that it was not a good idea "from a policy standpoint." However, Dole said the United States must push for "mutual trade" with other countries. "I don't support import surcharges, but we have to come up with something," he said.

Imports rose 1 percent during the April-June period to $86.3 billion, but the rise was all in price and not in volume, Commerce said. Additionally, petroleum imports, whose prices have declined in recent months, surged strongly while other imports declined.

Commerce officials said the decline in nonpetroleum imports in the second quarter was not a sign that the trade balance would improve in coming months, but it reflected a slowdown in U.S. economic growth and purchases of foreign goods. "We would like to see the trade balance improve while the economy is growing," said Robert Ortner, Commerce Department chief economist.

If the economy rebounds at a 5 percent rate, as the Reagan administration has forecast, "We'll see a pickup in imports of nonpetroleum products. We still have some larger trade deficits ahead of us," Ortner said.

Exports declined 5 percent during the second quarter, with all of the change in volume, not price, Commerce said. Agricultural and other exports declined.

Third World countries did not fare well in selling their exports to the United States. Nonpetroleum imports from non-OPEC developing countries dropped $1.9 billion, or 9 percent, in the second quarter, Commerce reported. A drop in imports from Latin American nations, which are struggling to increase their exports to pay their huge foreign debts, accounted for one-fourth of the decline, Commerce said.

On the other hand, nonpetroleum imports rose by $500 million from both Canada and Japan and by $200 million from Western Europe.

Petroleum imports rose 17 percent as the average number of barrels imported each day rose from 4.61 million to 5.36 million, Commerce said. The average price per barrel rose from $26.86 to $26.97.

The increase in volume was largely for inventories, which rose 6 percent while consumption declined 5 percent. "The increase in average price did not yet reflect the downward movement in the spot market, where oil prices were decreasing for much of the quarter," Commerce said.

The decline in exports was concentrated in machinery, chemicals and petroleum and consumer goods.

Exports decreased 8 percent to Western Europe, 18 percent to Japan and 11 percent to the newly industrialized countries of South Korea, Taiwan, Hong Kong and Singapore. Exports to Canada rose 7 percent during the quarter.

For the first half of the year, the trade deficit ran at a $126 billion annual rate, compared with a deficit of $108.3 billion for all of last year, Commerce said. The deficit with Japan increased to a $45.9 billion annual rate, and that with Western Europe rose to a $21.3 billion annual rate. The deficit with Latin America declined to a $15.7 billion annual rate.