President Carter yesterday challenged the leaders of the Soviet Union to "choose either confrontation or cooperation," and warned that unrestrained rivalry could lead to "graver tensions" between the two superpowers.

In an address during the 128th commencement exercises of the U.S. Naval Academy, Carter balanced some of the toughest anti-Soviet statements of any recent president with an optimistic forecast for a new strategic arms limitation talks (SALT) agreement and a renewed commitment to seek a "broadly defined and truly reciprocal detente."

The president gave an upbeat assessment of U.S. military strength and declared that this country and its allies "must and will be able to meet any foreseeable challenge" from either nuclear or conventional forces of the Soviet bloc. He said the United States can maintain its immense military power "without excessive sacrifice by the people of our country."

Carter gave major emphasis to his concern about "persistent and increasing" Soviet and Cuban military involvement in Africa, and charged that troops of those nations seem to be stationed permanently on that continent. He charged that the Soviet Union seems to view detente as "a continuing aggressive struggle for political advantage" through the use of military power and arms.

Carter's somber portrayal of Soviet methods and motivations and of the state of U.S.-Soviet relations strongly suggested that from his viewpoint the future of detente depends on choices to be made by the Soviet leaders within the near future.

While the United States would prefer that the Soviets choose cooperation rather than confrontation, "it is adequately prepared to meet either choice," he said.

The first reaction from Moscow was swift and sharp. A dispatch of the Soviet news agency, Tass, called his remarks "strange," and said it was the United States rather than the Soviet Union which is the obstacle to peaceful cooperation.

Carter's man-faceted speech, his most comprehensive assessment of Soviet-American relations, included these points:

The United States remains committed to detente and achieving a new SALT agreement. "I am glad to report that the prospects for a SALT II agreement are good," the president said.

The United States does not want Soviet activities in Africa and elsewhere to impede the SALT negotiations. But Carter warned in strong terms that there is an inevitable linkage between the two. In a democratic society, he said, "tensions, sharp disputes or threats to peace will complicate the quest for a successful agreement."

In contrast to American democracy, the Soviet form of government "is becoming increasingly unattractive to other nations." The Soviet Union, the president said, "attempts to export a totalitarian and repressive form of government, resulting in a closed society."

The United States enjoys numerous advantages over the Soviet Union, including a sounder economy, superior scientific and technological capabilities, stronger alliances and military power "second to none."

The speech was delivered to the assembled Naval Academy midshipmen and their families in the Navy-Marine Corps Memorial Stadium here. It was clearly one ofCarter's most important foreign policy addresses both because of its subject matter and its timing.

In recent weeks, there has been a notable renewal of Soviet-American tension as the Carter administration responded to Soviet and Cuban activity in Africa with increasingly strong words - and the dispatch of U.S. military transport planes to airlift European and African troops to Zaire.

This has given rise to fears, shared by some in the administration, that the prospects for a new strategic arms accord as well as relations generally between the superpowers, are endangered.

Perhaps the most unusual aspect of the address was Carter's harsh description of the internal as well as external workings of the Soviet system. In terms rarely heard in presidential foregin policy declarations of recent years, Carter said that the Soviet abuse of basic human rights at home in violation of the helsinki accords has earned "the condemnation of people everywhere who love freedom."

"The Soviet system cannot tolerate freely expressed ideas, notions of loyal opposition and the free movement of peoples," Carter said.

The president said that Soviet economic growth is slow, that its standard of living compares unfavorably with that of other industrialized nations and that the Soviet Union is unable to feed its own people in normal times.

At the same time, he said that the Russians are continuing an "excessive" military buildup "far beyond any legitimate requirement for the defense of themselves or their allies."

Speaking of the choice he said the Soviets must make in international affairs, Carter said that "a competition without restraint and without shared rules will escalate into graver tensions, and our relationship as a whole will suffer. I do not wish this to happen - I do not believe [Soviet President Leonid I.] Brezhnev desires it either - and this is why it is time for us to speak frankly and to face the problem squarely."

In a passage a White House official said was particularly significant, Carter called on the Russians to join "in seeking a peaceful and speedy transition to majority rule in Rhodesia and Namibia" and in efforts to resolve conflicts in Angola and the Ethiopian region of Eritrea. He also listed a series of negotiations with the Russians - from strategic arms to deployments in the Indian Ocean - and said that these should continue.

The president coupled his tough message to Soviet leaders with some reassuring words for the American public. He cautioned against "excessive swings in the public mood - from euphoria when things are going well to despair when they are not, from an exaggerated sense of compatibility with the Soviet Union to open expressions of hostility."

Carter also said, in a section of the speech clearly aimed at critics of administration defense policies, that it is possible to exaggerate Soviet military power.

"False or excessive estimates of Soviet strength or of American weakness contribute to the effectiveness of the Soviet propaganda efforts," he said.

Carter, the first graduate of the naval Academy to become president, also took the opportunity to defend his policies toward the Navy in which he served. He said recent "alarming news reports" about administration budget proposals for the navy "ignored the fact that we have the highest defense budget in history and that the largest portion this will go to the Navy."

The remark prompted applause from the midshipmen and navy officers in the stadium, one of four times the 31-minute speech was interrupted by applause.

But esentially, Carter's audience was subdued. Arrayed behind the president on the stadium field were the top Navy brass, Defense Secretary Harold Brown and national security adviser Zbigniew Brzezinski and a delegation of Maryland Democratic politicians headed by Acting Gov. Blair Lee III.

A total of 944 new officers were commissioned in the navy and Marine Corps at the conclusion of the commencement exercises.

For Carter, who 32 years ago sat through a similar ceremony with his fellow members of the class of 1946, the return to Annapolis was filled with nostalgia. He was accompanied by his wife, Rosalynn. Before the speech, the president and Mrs. Carter toured the academy grounds, including the room in Bancroft Hall where Carter lived as a midshipman.

"I felt at home," the president said of his return to the Naval Academy.